0
$QRYS1 = QUERY;
$QRYS2 = QUERY;
$QRYS3 = QUERY;
$QRYS4 = QUERY;

foreach($QRYS1 as $QRY1)
{
 foreach($QRYS2 as $QRY2)
 {
   if(bla)
   {
   foreach($QRYS3 as $QRY3)
   {
     foreach($QRYS4 as $QRY4)
     {

     }
   }
  }
 }
}

The above style of code makes data processing very slow. Is there anyway I can make the processing fast?

I am getting the data doing joining and counting with multiple tables and its all ARRAY().

4
  • Without knowing more about your database, your data and several other things it would be impossible to answer. It is very likely that you can issue one query properly joined, for instance, rather than 4 discrete queries. Commented Jan 22, 2016 at 13:36
  • $QRYS* = QUERY; What is the meaning? It's array data OR MySQL Query? Commented Jan 22, 2016 at 13:38
  • well my DB is MySQL and as for data they are simple but when it comes to joining and doing counting where process becomes very slow. Commented Jan 22, 2016 at 13:40
  • @jay: for example, I got 400 user who are grouped in 15 departments and each user can be linked to 6 different products and each departments can be linked to 6 different products, in this way I got 50 companies with 1500 users and each companies has 10 to 20 departments and all of them can be linked to Product list (6 different) Commented Jan 22, 2016 at 13:44

2 Answers 2

4

fetching table using for-each loop will slow down your page. You can try joining of tables and fetch those fields which you want.

Sign up to request clarification or add additional context in comments.

3 Comments

Joins and groups are probably going to be necessary here
I have given a sample of my SQL below
Manage Index in your table structure for fast query processing.
0
<?php

$TotalCountofDepartmentforSelectedComp_id = Userlog::where('userlogs.company_id', '=', $comp_id)
    ->where_Between('userlogs.created_at', $startDate, $endDate)
    ->group_by('userlogs.department_id')
    ->get(['userlogs.department_id', DB::raw('count(userlogs.user_id) as totaldepartmentcount')]);

$ProductListforSelectedComp_id = Product::join('userlogs', 'userlogs.product_id', '=', 'products.id')
    ->where('userlogs.company_id', '=', $comp_id)
    ->distinct()
    ->get(['products.id', 'products.name']);


$TotalCountCompanyProductWiseinforSelectedComp_id = Product::join('userlogs', 'userlogs.product_id', '=', 'products.id')
    ->where('userlogs.company_id', '=', $comp_id)
    ->where_Between('userlogs.created_at', $startDate, $endDate)
    ->group_by('products.id')
    ->get(['products.id', DB::raw('count(userlogs.user_id) as totalcompanyproductcountuserwise')]);

$DepartmentListsforSelectedComp_id = Department::where('company_id', '=', $comp_id)->get(
    ['departments.id', 'departments.name']
);


$TotalCountCompanyDepartmentProductWiseinforSelectedComp_id = DB::query(
    'SELECT T1.id, T2.department_id,
            COALESCE(T2.totaldepartmentproductcountuserwise, 0) AS totaldepartmentproductcountuserwise
       FROM (
         SELECT DISTINCT products.id FROM products
           INNER JOIN userlogs ON userlogs.product_id = products.id
           WHERE company_id = ?
         ) AS T1
       LEFT JOIN (
         SELECT department_id, product_id, COUNT(*) AS totaldepartmentproductcountuserwise
           FROM userlogs
           WHERE company_id = ?
             AND created_at BETWEEN ? AND ?
           GROUP BY department_id, product_id
         ) AS T2 ON T2.product_id = T1.id',
    array($comp_id, $comp_id, $startDate, $endDate)); 

1 Comment

its Laravel! The above code is as much as I can go with advance SQL, still learning, please help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.