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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.