首页 > 解决方案 > 如何优化查询以更快地运行(查询中的查询)

问题描述

此查询大约需要 5 秒才能完成。我的意思是当我刷新或导航到页面时,完成浏览器加载并显示总计数需要 5 秒钟。

这是我想要实现的,FIRST是根据empID获取systemID(ID)的MAX值。但在第一个查询结束之前,我进行了另一个查询,将获取 empID 在 eStatus col 上具有 JUMP 值的行。

这是为了比较empID的最新数据startDate和他之前的JUMP endDate的年份差异。

这是我的桌子

---|-------|-----------|------------|---------
ID | empID | startDate |   endDate  | eStatus
---|-------|-----------|------------|---------
1  |   10  | 2001-1-31 | 2001-12-31 |
2  |   10  | 2002-1-31 | 2002-12-31 | 
3  |   22  | 2001-1-31 | 2001-12-31 |
4  |   10  | 2003-1-31 | 2003-12-31 | JUMP
5  |   10  | 2004-1-31 | 2004-12-31 |
6  |   22  | 2002-1-31 | 2002-12-31 | JUMP
7  |   10  | 2005-1-31 | 2005-12-31 |
8  |   22  | 2003-1-31 | 2003-12-31 |
9  |   22  | 2004-1-31 | 2004-12-31 |
10 |   10  | 2006-1-31 | 2006-12-31 | JUMP
11 |   10  | 2007-1-31 | 2007-12-31 |
12 |   10  | 2008-1-31 | 2008-12-31 |
13 |   10  | 2009-1-31 | 2009-12-31 | JUMP
14 |   10  | 2010-1-31 | 2010-12-31 |
15 |   10  | 2011-1-31 | 2011-12-31 |

第一个查询将按 empID 组获取最大 ID。

---|-------|-----------|------------|---------
ID | empID | startDate |   endDate  | eStatus
---|-------|-----------|------------|---------
15 |   10  | 2011-1-31 | 2011-12-31 |
9  |   22  | 2004-1-31 | 2004-12-31 | 

第二个查询将获取在 eStatus Col 上有 JUMP 数据的 empID 行

---|-------|-----------|------------|---------
ID | empID | startDate |   endDate  | eStatus
---|-------|-----------|------------|---------
4  |   10  | 2003-1-31 | 2003-12-31 | JUMP
6  |   22  | 2002-1-31 | 2002-12-31 | JUMP
10 |   10  | 2006-1-31 | 2006-12-31 | JUMP
13 |   10  | 2009-1-31 | 2009-12-31 | JUMP
6  |   22  | 2002-1-31 | 2002-12-31 | JUMP

现在我可以计算第一个查询的 startDate 和第二个查询的 enddate 的日期差。如果它大于 2,它将计入我的最终计数。非常感谢您并保持安全。这是我的代码:

<?php
$counterA= 0;
$counterB= 0;
$finalCount = 0;
                  
  $result = mysqli_query($con,"SELECT empID,endDate FROM tablerecord 
  WHERE ID IN (SELECT MAX(ID) FROM tablerecord GROUP BY empID)");
  while ($row=mysqli_fetch_assoc($result )) {
    $counterA++; //count how many result based on the above query
    $emp_max = $row['empID']; //Get emp ID based on max ID
    endDate_result = $row['endDate']; //Get endDate based on max ID
        
    $resultPrevious=mysqli_query($con,"SELECT empID,startDate FROM tablerecord
    WHERE empID = '$emp_max' AND eStatus = 'JUMP' ");
    while ($rowPrevious=mysqli_fetch_assoc($resultPrevious)) {
                      
        $counterB++; //count how many result based on the above query
                                            
        $dateA=date_create($rowPrevious['startDate']);
        $dateB=date_create($endDate_result);
                            
        $diff2=date_diff($dateA,$dateB);
        $numLenght = $diff2->y;
                      
                      if ($numLenght > 2) {
                        $finalCount++;
                      }
    }
   }
   echo $counterA;
   echo "<br>";
   echo $counterB;
   echo "<br>";
   echo $finalCount;

?>

标签: phpmysqlmysqli

解决方案


我会使用这个查询来获取所有需要的数据:

# Get first and last jump for each empID
SELECT * 
FROM tablerecord t1
WHERE t1.ID = (
    SELECT MIN(t2.ID)
    FROM tablerecord t2
    WHERE t1.empID==t2.empID
) or t1.ID = (
    SELECT MAX(t3.ID)
    FROM tablerecord t3
    WHERE t1.empID==t3.empID
)

然后运行一些 PHP,比较每一对

$result = mysqli_query($con,"the_previous_query....");
$row_to_compare_with =false;
while ($row=mysqli_fetch_assoc($result )) {
        if ($row_to_compare_with == false) {
          $row_to_compare_with = $row;
        } else {
           // Compare $row_to_compare_with with $row, both having same empId
           // do your thing here...
           $row_to_compare_with = false;
        }
}

推荐阅读