简体   繁体   English

MySql查询的执行永远需要PHP来完成,但是从phpMyAdmin运行时很快就会完成

[英]MySql query execution takes for ever from PHP but finishes soon when run from phpMyAdmin

I have a sql querying a MySql table having millions of records. 我有一个SQL查询具有数百万条记录的MySql表。 This gets executed in phpMyAdmin in around 2 secs but when run from PHP script, it doesn't complete executing. 这将在phpMyAdmin中执行大约2秒钟,但是从PHP脚本运行时,它无法完成执行。

select 
  concat(p1.`Date`," ",p1.`Time`) as har_date_from,
  concat(p2.`Date`," ",p2.`Time`) as har_date_to,
 (select concat(p3.`Date`," ",p3.`Time`) from 
   power_logger p3 
   where p3.slno between 1851219 and 2042099
   and p3.meter_id="logger1"
   and str_to_date(concat(p3.`Date`," ",p3.`Time`),"%d/%m/%Y %H:%i:%s") >=
       str_to_date(concat(p1.`Date`," ",p1.`Time`),"%d/%m/%Y %H:%i:%s")

   order by p3.slno limit 1) as cur_date_from,
     (select concat(p4.`Date`," ",p4.`Time`) from
        power_logger p4 
        where p4.slno between 1851219 
        and 2042099
        and p4.meter_id="logger1" 
        and str_to_date(concat(p4.`Date`," ",p4.`Time`),"%d/%m/%Y %H:%i:%s") >=
            str_to_date(concat(p2.`Date`," ",p2.`Time`),"%d/%m/%Y %H:%i:%s")
        order by p4.slno 
        limit 1
      )
      as cur_date_to, 
      p1.THD_A_N_Avg-p2.THD_A_N_Avg as thd_diff
      from power_logger p1
    join 
      power_logger p2 
        on p2.slno=p1.slno+1 
        and p1.meter_id="fluke1" 
        and p2.meter_id=p1.meter_id
        and p1.slno between 2058609 and 2062310 
        and p1.THD_A_N_Avg-p2.THD_A_N_Avg>=2.0000

php script: $query=/*The query above passed as string*/ $mysql=mysql_connect('localhost','username','pwd') or die(mysql_error()); mysql_select_db('dbname',$mysql); $rows=mysql_query($query,$mysql) or die(mysql_error()); php脚本: $query=/*The query above passed as string*/ $mysql=mysql_connect('localhost','username','pwd') or die(mysql_error()); mysql_select_db('dbname',$mysql); $rows=mysql_query($query,$mysql) or die(mysql_error()); $query=/*The query above passed as string*/ $mysql=mysql_connect('localhost','username','pwd') or die(mysql_error()); mysql_select_db('dbname',$mysql); $rows=mysql_query($query,$mysql) or die(mysql_error());

There are no issues in mysql connectivity and related stuffs, as I run a lot of other queries successfully. mysql连接和相关内容没有问题,因为我成功运行了许多其他查询。 I have set indexes on meter_id and Date,Time together. 我一起在meter_id和Date,Time上设置了索引。 slno is the auto increment value. slno是自动增量值。 I know there are similar questions asked as I found a lot from my research but none of them really helped me. 我从研究中发现了很多类似的问题,但没有一个真正对我有帮助。 Thanks in advance if anybody could help me out to find a solution. 预先感谢任何人可以帮助我找到解决方案。

Query Description:This queries the power_logger table containing millions of records and THD_A_N_AVG, meter_id,slno,Date and Time are among the columns of the table. 查询描述:该查询查询包含数百万条记录的power_logger表,并且THD_A_N_AVG,meter_id,slno,Date和Time在表的列中。 This selects The date and time from two consecutive rows with in a range of slnos where difference between THD_A_N_AVG is greater than or equal to 2. When those dates are fetched, it even has to fetch the date and time with in a different range of slnos where the date and time are the closest to the once fetched earlier thus forming har_date_from,har_date_to, cur_date_from,cur_date_to. 这将从连续范围为slno的两个连续行中选择日期和时间,其中THD_A_N_AVG之间的差异大于或等于2。当获取这些日期时,它甚至必须以不同范围的slno来获取日期和时间。日期和时间最接近先前获取的日期和时间,因此形成har_date_from,har_date_to,cur_date_from,cur_date_to。 What messes up here is the nested select. 搞砸的是嵌套选择。

Usually PHPMyAdmin automatically adds "LIMIT 0, 30" at the end of the query, so you only load 30 rows at once. 通常,PHPMyAdmin在查询末尾会自动添加“ LIMIT 0,30”,因此您一次只能加载30行。 In your code you are trying to load everything at once, that's why it's taking so long. 在您的代码中,您尝试一次加载所有内容,这就是为什么要花这么长时间的原因。

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

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