Andy-H Posted February 11, 2012 Share Posted February 11, 2012 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 More sharing options...
fenway Posted February 12, 2012 Share Posted February 12, 2012 Yes, there is a more efficient way -- first grab the latest row, then join back. Link to comment https://forums.phpfreaks.com/topic/256867-slow-query/#findComment-1317262 Share on other sites More sharing options...
Andy-H Posted February 13, 2012 Author Share Posted February 13, 2012 So select from newlocations and join trackers? Link to comment https://forums.phpfreaks.com/topic/256867-slow-query/#findComment-1317628 Share on other sites More sharing options...
fenway Posted February 13, 2012 Share Posted February 13, 2012 First, forget about the joins -- write the query that will find the most recent values that you want. Link to comment https://forums.phpfreaks.com/topic/256867-slow-query/#findComment-1317710 Share on other sites More sharing options...
Andy-H Posted February 13, 2012 Author Share Posted February 13, 2012 $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 More sharing options...
fenway Posted February 13, 2012 Share Posted February 13, 2012 I see a lot of PHP code -- what I meant was produce an SQL query that will find the most recent one. Then the join will be more obvious. Link to comment https://forums.phpfreaks.com/topic/256867-slow-query/#findComment-1317847 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.