php - 如何优化查询以更快地运行(查询中的查询)
问题描述
此查询大约需要 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;
?>
解决方案
我会使用这个查询来获取所有需要的数据:
# 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;
}
}
推荐阅读
- python - Pandas hist subplots - 为每个直方图的颜色添加颜色条
- python - 如何从 OpenCV 点访问 X 和 Y 坐标?
- java - 如何向 java spring 服务器发出 http 请求?
- html - CSS - “包装” SVG 图像动画
- dictionary - 如何从 Terraform 中的两个不相交的数据结构生成地图?
- html - 即使 SQLALCHEMY_POOL_RECYCLE = 600 响应时间为 5 分钟后,PythonAnywhere 中出现错误 504-loadbalancer (Gateway Time-out)
- c - CMake,MacOS下三大桌面平台Win/Mac/Linux的交叉编译
- c# - 在此 Entity Framework 6.x 迁移期间如何更新基础数据?
- r - ggplot2中的分组条形图
- linux - qemu 新手,如何识别或创建可启动映像?