Jump to content

Slow query


Andy-H

Recommended Posts

I have this query which is taking 30 seconds to execute

 

 


            $query = "SELECT t.deviceid AS device, t.localname AS name, rfid.driver, 
                     nl.lat, nl.lon, nl.speed, nl.heading, nl.inputi AS ignition, nl.thetimestamp AS time
                     FROM trackers t
                     INNER JOIN ( newlocations nl ) ON ( t.unitid = nl.device )
                     LEFT JOIN  ( rfid )            ON ( nl.ex1   = rfid.tagID )
                     WHERE t.custid = ". $this->_user->companyID ." GROUP BY t.deviceid
                     ORDER BY nl.id, t.localname DESC";

 

 

newlocations has around 3 million rows, but I only want to return the latest row joined with the other data, is there any way to do so efficiently? Or will I just have to run 2-3 querys?

 

 

Cheers, Andy

Link to comment
https://forums.phpfreaks.com/topic/256867-slow-query/
Share on other sites


            $query = "SELECT t.deviceid AS device, t.unitid, t.localname AS name 
                     FROM trackers t
                     WHERE t.custid = ". $this->_user->companyID ." 
                     ORDER BY t.localname ASC";
            $result = mysql_query($query);
            if ( !mysql_num_rows($result) )
               throw new APIException('FAIL', 'No devices attached to your company');
            while ( $row = mysql_fetch_assoc($result) )
            {
               $query = "SELECT nl.lat, nl.lon, nl.speed, ROUND(nl.heading) AS heading, nl.inputi AS ignition, nl.thetimestamp AS time, nl.ex1
                        FROM newlocations nl 
                        WHERE nl.device = '". $row['unitid'] ."' 
                        ORDER BY nl.thetimestamp DESC LIMIT 1";
               unset($row['unitid']);
               $res = mysql_query($query);
               while ( $loc = mysql_fetch_assoc($res) )
               {
                  if ( !empty($loc['ex1']) )
                  {
                     $query         = "SELECT driver FROM rfid WHERE tagID = '". $loc['ex1'] ."' LIMIT 1";
                     $r             = mysql_query($query);
                     if ( mysql_num_rows($r) )
                        $loc['driver'] = mysql_result($r, 0, 0);
                  }

Link to comment
https://forums.phpfreaks.com/topic/256867-slow-query/#findComment-1317842
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.