我试图弄清楚为什么我的查询要花这么长时间。每个查询通常要花费60-90秒左右的时间,并且执行多次。
旧查询:
"select * from $table where type = '".$type."' and transdate >= '".$begdate."' and
transdate<='".$enddate."' and customerin (select customer from master
where targetaccount='Y') ";
新查询:
$q = "SELECT * FROM activity INNER JOIN custactivities ON
activity.activityID = custactivities.activityID WHERE type = '$type' AND
transdate BETWEEN '$begdate' AND '$enddate' AND custnum IN
(SELECT customer FROM master WHERE targetaccount = 'Y')
GROUP BY custactivities.customer";
原始查询大约需要0.15秒,新查询每个查询大约需要60-90秒。
它确实有一些PHP,但是查询仍然是问题所在。
任何意见是极大的赞赏。
如果没有索引,请在以下列中添加索引
activityID on both table
type - if it's numeric, don't use the '
transdate
targetaccount - if you can, use numeric, or enum definition for that column for better indexing
并且如果主表在使用targetaccount ='Y'的情况下不是太大,则可以将其分离到另一个查询,使用php进行提取,并使用爆破式将数组插入查询中,而不是使用子查询。
切勿使用ChrisS提到的*。
这是php和mysql的一些代码片段
$cust = array();
$sql = 'SELECT customer FROM master WHERE targetaccount = "Y"';
$query = mysql_query();
if ($query) {
while ($row = mysql_fetch_row($query)) {
$cust[] = $row[0]
}
}
$sql = "select * from $table where type = '".$type."' and transdate >= '".$begdate."' and
transdate<='".$enddate."'" . (count($cust) ? ' AND custnum IN ('.implode(', ', $cust).')' : '');
我还没有测试过,但我想现在您明白了。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句