简体   繁体   English

PHP-提取数据问题,获取时间太长

[英]PHP - Fetching data problem, taking too long to be fetched

Good day, I'm having a problem fetching my data, it takes so long, literally takes 10 seconds for the data to be fetched into the grid, anyone solved this problem? 美好的一天,我在获取数据时遇到了问题,它花了这么长时间,从字面上将数据提取到网格需要10秒,有人解决了这个问题吗? please advised, thank you! 请指教,谢谢!

See code below 见下面的代码

$stVal = $_GET['search'];
$stLimit = $_GET['limit'];
$sql = "";
if($_GET['gdate']==''){
    $gridDate = date('Y-m-d', strtotime($today.'-1 months'));
    $gridfDate = date('Y-m-01', strtotime($today.'-1 months'));
}else{
    $date = date('Y-m-d', strtotime($_GET['gdate']));
    $gridDate = date('Y-m-d', strtotime($date.'-1 months'));
    $gridfDate = date('Y-m-01', strtotime($date.'-1 months'));
}

On this for loop, when I remove the sql statement and put it outside, then the table is shown immidiately, but IT IS NOT FETCHED CORRECTLY 在此for循环中,当我删除sql语句并将其放在外面时,会立即显示该表,但未正确检查它

for($x=0;$x<18;$x++){
        $thisDate = date('Y-m-d', strtotime($gridDate.'+'.$x.' months'));
        $thisDate2 = date('Y-m-d', strtotime($gridfDate.'+'.$x.' months'));
        $mheader[$x] = date('Y', strtotime($gridDate.'+'.$x.' months'));
        $dheader[$x] = date('M', strtotime($gridDate.'+'.$x.' months'));
        $date = $thisDate AND $thisDate2;

        //See code below for this break line of codes

       $sql .=",(SELECT CASE WHEN COUNT(tc.`name`) = 0 THEN '<td data-attribute=\"".$thisDate2."\"></td>' WHEN DATE_FORMAT(min(pd.`duedate`), '%Y-%m') <= DATE_FORMAT('$thisDate', '%Y-%m') AND DATE_FORMAT(max(pd.`duedate`), '%Y-%m') >= DATE_FORMAT('$thisDate', '%Y-%m') THEN '<td class=\"paid\" data-attribute=\"".$thisDate2."\">Paid Month</td>' ELSE CONCAT('<td data-attribute=\"".$thisDate2."\" class=\"', CASE WHEN min(p.`duedate`) < '$dateposted' THEN 'occ' WHEN str_to_date(t.`due_date`, '%M %d,%Y') < '$dateposted' AND t.`months` = 0 THEN 'fins' ELSE 'ten' END,'\">', tc.`name`,' #', tc.`transID`,'</td>') END AS gdisp FROM `transaction_con` tc LEFT JOIN `transaction` t ON t.`id` = tc.`transID` LEFT JOIN (select sum(pde.amount) as total, pde.net, pde.pay_type, pde.duedate, pde.transID, pde.item, pt.status from payments_depot pde left join payment_type pt on pt.id = pde.pay_type where pde.`item` = 'House Rate' AND pt.status between 1 and 2 group by pde.paymentid HAVING total = net) pd ON t.`id` = pd.`transID` LEFT JOIN payment_type pt ON pt.`id` = pd.`pay_type` LEFT JOIN payments p ON p.transID = t.id WHERE tc.`apartmentID` = a.`id` AND DATE_FORMAT(tc.`date`, '%m-%Y') = DATE_FORMAT('$thisDate', '%m-%Y')) AS '$thisDate'";


}
$gData = $gen->gridData($sql,$stVal,$stLimit);
$tableID = 'gridTable';
$merge = 'th,td';

echo json_encode(array(
    'datenow' => date('M', strtotime($dateposted)),
    'tableID' => $tableID,
    'merge' => $merge,
    'dates' => $thisDate,
    'mheader' => $mheader,
    'dheader' => $dheader,
    'date' => $date,
    'gridData' => $gData
)); 

SQL Statement code break lines SQL语句代码换行

ALSO this code, I think is the occurrence of the problem, when I removed this sql code and bring outside the for loop then the data is not fetched correctly, but the table is shown immediately. 同样,此代码,我认为是问题的发生,当我删除此sql代码并带到for循环之外时,则无法正确获取数据,但表将立即显示。

     (SELECT CASE WHEN COUNT(tc.`name`) = 0 THEN '<td data-attribute=\"".$thisDate2."\"></td>' 
     WHEN DATE_FORMAT(min(pd.`duedate`), '%Y-%m') <= DATE_FORMAT('$thisDate', '%Y-%m') AND DATE_FORMAT(max(pd.`duedate`), '%Y-%m') >= DATE_FORMAT('$thisDate', '%Y-%m') THEN 

     '<td class=\"paid\" data-attribute=\"".$thisDate2."\">Paid Month</td>' 

     ELSE CONCAT('<td data-attribute=\"".$thisDate2."\" class=\"', CASE WHEN min(p.`duedate`) < '$dateposted' THEN 'occ' WHEN str_to_date(t.`due_date`, '%M %d,%Y') < '$dateposted' AND t.`months` = 0 THEN 'fins' ELSE 'ten' END,'\">', tc.`name`,' #', tc.`transID`,'</td>') 

     END AS gdisp FROM `transaction_con` tc LEFT JOIN `transaction` t ON t.`id` = tc.`transID` LEFT JOIN (select sum(pde.amount) as total, pde.net, pde.pay_type, pde.duedate, pde.transID, pde.item, pt.status from payments_depot pde left join payment_type pt on pt.id = pde.pay_type where pde.`item` = 'House Rate' 

        AND pt.status between 1 and 2 group by pde.paymentid HAVING total = net) pd ON t.`id` = pd.`transID` 

     LEFT JOIN payment_type pt ON pt.`id` = pd.`pay_type` LEFT JOIN payments p ON p.transID = t.id 

         WHERE tc.`apartmentID` = a.`id` AND DATE_FORMAT(tc.`date`, '%m-%Y') = DATE_FORMAT('$thisDate', '%m-%Y')) AS '$thisDate'";

You seem to be calling all data from a certain month for 18 months, with 18 different queries. 您似乎正在使用18个不同的查询来调用某个月内18个月内的所有数据。 I would suggest getting all data from the 18 month period, and then parsing it per month after fetching the data. 我建议获取18个月内的所有数据,然后在获取数据后每月进行解析。 This way you are not asking the database to look up the same data 18 times. 这样,您就不会要求数据库查找相同数据18次。

Keep your SQL outside of the for loop, and just call the data based on start and enddate of the 18 month window. 将您的SQL保留在for循环之外,仅根据18个月窗口的开始和结束日期调用数据。

EDIT: I wrote some code on how I would tackle your problem. 编辑:我写了一些有关如何解决您的问题的代码。 I'm staying away from the horror that is the sql query. 我远离恐怖的sql查询。 I can't even begin to understand how and why it is what it is. 我什至无法开始理解它的本质和方式。

//Write a query that calls for all 18 months of data.
$sql = "SELECT Data for all 18 months";

//Call your Database
$sqlData = queryDatabase($sql, $stVal, $stLimit);

//Parse the Data into 18 seperate months
$gData = ParseData($sqlData);

$tableID = 'gridTable';
$merge = 'th,td';

echo json_encode(array(
    'datenow' => date('M', strtotime($dateposted)),
    'tableID' => $tableID,
    'merge' => $merge,
    'dates' => $thisDate,
    'mheader' => $mheader,
    'dheader' => $dheader,
    'date' => $date,
    'gridData' => $gData
));

//This function slices your sql data into pieces divided by month.
function ParseData($sqlData){
  $dataInMonths = [];
  //This depends on how you select your data from the DB, I'm going to use assoc 
  //arrays right now.
  $i = -1;
  foreach($sqlData as $dataRow){
    $currentDate = $dataRow['date'];
    $currentTime = strtotime($currentDate);
    $currentMonth = date("F-Y", $currentTime);

    if($currentMonth != $previousMonth){
      $i++;
      $dataInMonths[$i] = [];
    }
    $dataInMonths[$i][] = $dataRow;

    $previousMonth = $currentMonth;
  }

  //Now your data is an array of 18 items, each with a month of their own data. 
  //You can now continue to parse it to fit within your grid.

  return $dataInMonths;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM