简体   繁体   English

如何优化查询以更快地运行(查询内的查询)

[英]How can I optimize query to run faster (Query inside a Query)

This query will take around 5 seconds to complete.此查询大约需要 5 秒钟才能完成。 I mean when I refresh or navigate to the page it will take 5 seconds to complete the browser loading and to display the total counts.我的意思是当我刷新或导航到页面时,完成浏览器加载并显示总计数需要 5 秒钟。

Here is what I want to achieve, FIRST is to get the MAX value of the systemID (ID) based on the empID.这是我想要实现的,首先是根据empID获取systemID(ID)的MAX值。 But before the First query ends I made another query the will get the row where the empID has a JUMP value on the eStatus col.但是在第一个查询结束之前,我进行了另一个查询,它将获取 eStatus 列上 empID 具有 JUMP 值的行。

This is to compare the year difference of the latest data startDate of the empID and to his previous JUMP endDate.这是为了比较 empID 的最新数据 startDate 和他之前的 JUMP endDate 的年份差异。

Here is my table这是我的桌子

---|-------|-----------|------------|---------
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 |

the First query will get the max ID by group of empID.第一个查询将按 empID 组获取最大 ID。

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

the Second query will get the empID row that has a JUMP data on the eStatus Col第二个查询将获取 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

Now I can compute the date difference from startDate of 1st query and enddate of 2nd query.现在我可以计算第一个查询的 startDate 和第二个查询的 enddate 的日期差异。 If it is greater than 2 the it will count to my final count.如果它大于 2,它将计入我的最终计数。 THANK YOU SO MUCH IN ADVANCE and KEEP SAFE.非常感谢您提前并保持安全。 Here is my code:这是我的代码:

<?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;

?>

if I understand correctly, what you want is to select all data that have eStatus value jump .如果我理解正确,您想要的是选择所有具有eStatusjump

why not simply SELECT empID,startDate FROM tablerecord WHERE empID IN(SELECT MAX(ID) FROM tablerecord) AND eStatus = 'JUMP' ?为什么不简单地SELECT empID,startDate FROM tablerecord WHERE empID IN(SELECT MAX(ID) FROM tablerecord) AND eStatus = 'JUMP'

or, if you want the unique value of empID , SELECT empID,startDate FROM tablerecord WHERE empID IN(SELECT DISTINCT(ID) FROM tablerecord) AND eStatus = 'JUMP'或者,如果您想要empID的唯一值, SELECT empID,startDate FROM tablerecord WHERE empID IN(SELECT DISTINCT(ID) FROM tablerecord) AND eStatus = 'JUMP'

all my query above will not produce any result you might want to try multiple select in your query for a crude way to get it all in one go我上面的所有查询都不会产生任何结果,您可能想在查询中尝试多选,以一种粗略的方式一次性完成所有查询

select * from tablerecord where empID in(SELECT empID FROM tablerecord WHERE ID IN (SELECT MAX(ID) FROM tablerecord group by empID)) and eStatus = 'JUMP'

in this way, you only query the DB one time and the rest can be done with your PHP code这样,您只需查询一次数据库,其余的可以用您的 PHP 代码完成

I would use this query to get all needed data :我会使用这个查询来获取所有需要的数据:

# 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
)

And then some PHP to run through, comparing each pairs然后运行一些 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;
        }
}

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

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