我正在努力加快我创建的网页。我知道问题是我在查询中有查询。我觉得必须有一种更快的方法来完成相同的结果,但我的想法已经不多了。 (我第一次尝试这个页面需要45秒才能加载页面,现在我已经下载到6页左右)
我要做的是从表中提取运行速率信息。我需要从runrate表中提取正确的启动和运行结束,但我最初需要的是工作中心ID。
我觉得如果表格设置好一点那么它可能会非常困难,但这是我继承的,因此我有点卡住了。我需要从每个工作中心(大约15个)提取一个月的数据,每天可以运行多达4-5个数据......需要处理相当多的数据。
这是PHP代码:
$qtotalStartup = mysql_query("
SELECT startup.recordID, startup.date, startup.time, runrate.rate AS temRate, runrate.formID
FROM jos_a_inproc_startup startup JOIN jos_a_runrate runrate ON startup.recordID = runrate.recordID
WHERE startup.workcenterId = $id AND runrate.rate > 0 AND runrate.formID = 1 AND startup.date > DATE_SUB(NOW(), INTERVAL 1 MONTH)") or die(mysql_error());
$totalStartCtr = mysql_num_rows($qtotalStartup);
if ($totalStartCtr > 0) {
while($rtotalStartup = mysql_fetch_assoc($qtotalStartup)) {
$hours = 0;
$goalRate = 0;
$sumHrRR = 0;
$startDate = 0;
$startTime = 0;
$startupNum = $rtotalStartup['recordID'];
$goalRate = $rtotalStartup['temRate'];
$startDate = $rtotalStartup['date'];
$startTime = $rtotalStartup['time'];
$startTime = strtotime($startDate . ' ' . $startTime);
//now that we have all of the startup form info, we can move to the end of run information
//this query will retrieve the correct date, time, and ending run rate for us to use with our calculations.
$qtotalEOR = mysql_query("
SELECT eor.recordID AS eorRec, eor.date, eor.time, eor.startupid, runrate1.rate AS tempRate, runrate1.formID
FROM jos_a_inproc_eor eor JOIN jos_a_runrate runrate1 ON eor.recordID = runrate1.recordID
WHERE eor.startupid = $startupNum AND runrate1.rate > 0 AND runrate1.formID = 3") or die(mysql_error());
$totalEORCtr = mysql_num_rows($qtotalEOR);
if ($totalEORCtr > 0) {
while($rtotalEOR = mysql_fetch_assoc($qtotalEOR)) {
//reset the accumulator to 0 so we don't get extra 'bad' data.
$sumHrRR = 0;
$newGoalRate = 0;
$lastestDate = 0;
$latestTime = 0;
$eorNum = $rtotalEOR['eorRec'];
$latestDate = $rtotalEOR['date'];
$latestTime = $rtotalEOR['time'];
$latestTime = strtotime($latestDate . ' ' . $latestTime);
$sumHrRR= $rtotalEOR['tempRate'];
任何想法都将不胜感激。我知道在没有更多信息的情况下可能很难理解我想要了解的内容,所以如果您需要了解其他信息,请告诉我。感谢。
答案 0 :(得分:1)
Maby尝试使用多个JOINS
,如下所示:
SELECT startup.recordID, startup.date, startup.time,
runrate.rate AS temRate, runrate.formID
-- stuff from second query
eor.recordID AS eorRec, eor.date AS eor_date,
eor.time AS eor_time, eor.startupid AS eor_startupid,
runrate1.rate AS eor_tempRate,
runrate1.formID AS runrate1_formID
FROM jos_a_inproc_startup startup
JOIN jos_a_runrate runrate ON startup.recordID = runrate.recordID
-- second query LEFT JOIN
LEFT JOIN jos_a_inproc_eor eor
ON eor.startupid = startup.recordID
LEFT JOIN jos_a_runrate runrate1
ON eor.recordID = runrate1.recordID
AND runrate1.rate > 0
AND runrate1.formID = 3
WHERE startup.workcenterId = $id
AND runrate.rate > 0
AND runrate.formID = 1
AND startup.date > DATE_SUB(NOW(), INTERVAL 1 MONTH)
我不知道我是否正确,但我认为你也在使用PHP内部的结果进行一些聚合工作。您可以使用类似sum()
或avg()
和GROUP BY
的数据库在数据库中执行此操作。将较小的结果集从数据库传输到服务器以及在PHP内部循环和聚合的时间,您将节省一些时间。此外,大多数情况下使用JOIN
比使用循环中的查询甚至查询中的子查询要快得多。
您还应检查是否在搜索的列上设置了索引。还可以使用EXPLAIN
检查查询的执行方式。
答案 1 :(得分:0)
你可以使用Mem-Cache技术使其更快,并尝试使你的查询更简单...不要检索你不在脚本中使用的值..
答案 2 :(得分:0)
您通常将多少条记录作为输出处理?桌子有多大?你有没有看过索引?你最近分析了它们(重建过它们)吗?
另外,您是使用deflate将数据发送回浏览器吗?参见:
答案 3 :(得分:0)
好吧,您可以尝试使用multiple INNER JOINs(see)并在查询中只有一个查询而不是一个查询,这会极大地影响性能。你可以试试这样的东西,并稍微调整一下:
SELECT
startup.recordID AS startupRecordID,
startup.date AS startupDate,
startup.time AS startupTime,
runrate.rate,
runrate.formID,
eor.recordID AS eorRecordID,
eor.date AS eorDate,
eor.time AS eorTime,
eor.startupid AS eorStartupID
FROM jos_a_inproc_startup startup
INNER JOIN jos_a_runrate runrate
ON startup.recordID = runrate.recordID
INNER JOIN jos_a_inproc_eor eor
ON startup.recordID = eor.startupid
WHERE
startup.workcenterId = $id
AND runrate.rate > 0
AND runrate.formID = 1
AND startup.date > DATE_SUB(NOW(), INTERVAL 1 MONTH)