针对DATABASE问题的查询效率

时间:2013-07-03 05:11:28

标签: php mysql join

我试图用4个查询来查询我的数据库,每个查询都会给我我需要的行数但我面对的但是我注意到当我在服务器上执行查询时强大,一切都很快(相对来说) ),但如果我把它作为一个相对简单的服务器,我应该得到最长的工作时间(30秒,限制执行时间(PHP))。
我的while循环使用MokedCcode列表,每个MokedCcode进入查询并转到下一个MokedCcode。
注意:
1)我知道它将与MokedCcode-s的数量成正比 2)我需要增加执行时间限制吗?

1)有更有效的方法来进行这些查询吗?也许我没有正确使用mysql功能 例如第一个名为$ emerg的查询,这个查询需要给出日期之间的行数,其中WCODE的优先级为1,并且它必须与两个表(t和e)上的MokedCcode匹配。

$emerg = mysql_num_rows(mysql_query("SELECT  t.*,e.DISCODE,e.AREA,e.COLOR,e.PRIORITY FROM    $tbl_name AS t   
 LEFT JOIN eventcodes AS e ON t.MokedCcode = e.MokedCcode AND t.WCODE=e.WCODE  
 WHERE (t.MokedCcode='$MokedCcode' ) AND e.PRIORITY='1' 
 AND t.ndate BETWEEN '$start' AND '$end' ORDER By `id` DESC "));


另外我添加了3个查询,我想得到一些建议如何让它更快或者我没有任何选择并保持这样。

其他3个问题:

    $regular = mysql_num_rows(mysql_query("SELECT  t.*,e.DISCODE,e.AREA,e.COLOR,e.PRIORITY FROM $tbl_name AS t    
    LEFT JOIN eventcodes AS e ON t.MokedCcode = e.MokedCcode AND t.WCODE=e.WCODE  
    WHERE (t.MokedCcode='$MokedCcode' ) AND e.PRIORITY!='1' 
    AND t.ndate BETWEEN '$start' AND '$end' ORDER By `id` DESC "));

    $regHandled = mysql_num_rows(mysql_query("SELECT  t.*,e.DISCODE,e.AREA,e.COLOR,e.PRIORITY FROM $tbl_name AS t  
    LEFT JOIN eventcodes AS e ON t.MokedCcode = e.MokedCcode AND t.WCODE=e.WCODE 
    WHERE (t.MokedCcode='$MokedCcode' ) AND e.PRIORITY!='1' 
    AND t.EventHandling!='0' AND t.ndate BETWEEN '$start' AND '$end' ORDER By `id` DESC "));

    $emergHandled = mysql_num_rows(mysql_query("SELECT  t.*,e.DISCODE,e.AREA,e.COLOR,e.PRIORITY FROM $tbl_name AS t  
    LEFT JOIN eventcodes AS e ON t.MokedCcode = e.MokedCcode AND t.WCODE=e.WCODE  
    WHERE (t.MokedCcode='$MokedCcode' ) AND e.PRIORITY='1'
    AND t.EventHandling!='0' AND t.ndate BETWEEN '$start' AND '$end' ORDER By `id` DESC "));

1 个答案:

答案 0 :(得分:0)

我并不完全明白你想要达到的目标。

如果您只想要计数为什么要选择所有行?你不能使用COUNT() MySQL函数?将所有数据都写入脚本然后计算行数总是很慢。

即使您要选择所有这些列,也请尝试使用t.field1, t.field2, ...代替t.*