Jump to content

Why left join make the query so slow?


ayok

Recommended Posts

Hi,

I have this query

$sql  = "SELECT";
      $sql .= "    factuur.id, DATE_FORMAT(factuur.factuurdatum, '%d-%m-%Y') AS factuurdatum, factuur.prefix, factuur.volgnr,";
      $sql .= "    SUM(bestelling.bedrag) AS bedrag";
      $sql .= " FROM {$db['factuur']} AS factuur";
      $sql .= " LEFT JOIN {$db['bestelling']} AS bestelling ON bestelling.factuur_id=factuur.id";
      $sql .= " WHERE factuur.uid=" . $uid;
      $sql .= " GROUP BY factuur.id";
      $sql .= " ORDER BY factuur.factuurdatum DESC";

And it works sooo slow when I got hundreds of records in "bestelling" table.

Then I change the query into this

$sql  = "SELECT";
      $sql .= "    factuur.id, DATE_FORMAT(factuur.factuurdatum, '%d-%m-%Y') AS factuurdatum, factuur.prefix, factuur.volgnr,";
      $sql .= "    SUM(bestelling.bedrag) AS bedrag";
      $sql .= " FROM {$db['factuur']} AS factuur";
      $sql .= ", {$db['bestelling']} AS bestelling WHERE bestelling.factuur_id=factuur.id";
      $sql .= " AND factuur.uid=" . $uid;
      $sql .= " GROUP BY factuur.id";
      $sql .= " ORDER BY factuur.factuurdatum DESC";

It goes a lot faster. But why? Should I stop using left join? What's the different between these queries? Will I get different result??

 

Thank you,

ayok

Link to comment
https://forums.phpfreaks.com/topic/246611-why-left-join-make-the-query-so-slow/
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.