Note that there are some explanatory texts on larger screens.

plurals
  1. POSlow SQL query because of ORDER BY not using index
    primarykey
    data
    text
    <p>I've got this query:</p> <pre><code>SELECT cl.title, cl.URL, cl.ID AS ad_id, cl.cat_id, cl.price, cs.name AS cat_name, pix.file_name, area.area_name FROM classifieds cl FORCE INDEX (advertiser_id) INNER JOIN classifieds_pix pix ON cl.ID = pix.classified_id INNER JOIN cat_names_sub cs ON cl.cat_id = cs.ID INNER JOIN zip_codes zip ON cl.zip_id = zip.zip_id INNER JOIN area_names area ON zip.area_id = area.id WHERE cl.confirmed = 1 AND cl.price != '' AND cl.country = 'de' GROUP BY cl.advertiser_id ORDER BY cl.timestamp DESC LIMIT 5 </code></pre> <p>It takes > 1 sec when <code>classifieds</code> contains 168k rows, which is too long. <code>FORCE INDEX (advertiser_id)</code> allowed me to bring it down to 0.00x secs without the <code>ORDER BY</code> clause. The <code>timestamp</code> column is indexed, too, and I tried adding <code>FORCE INDEX (timestamp)</code>, but it didn't help. </p> <p><code>EXPLAIN</code> says <code>Using where; Using temporary; Using filesort</code> on the first <code>SELECT</code> from the <code>classifieds</code> table - which obviously causes the performance issue.</p> <p>Can you help me out on this one?</p> <p>Thanks in advance!</p> <p>PS: The purpose of this query is to get the 5 latest classifieds (including some additional information such as picture, category, zip code and area name). Furthermore, only one classified should be shown per advertiser. Can this be so hard?</p> <p>PPS: I tried to pin the problem down as much as possible and ended up with this query:</p> <pre><code>SELECT cl.title FROM classifieds cl GROUP BY cl.advertiser_id ORDER BY cl.timestamp DESC LIMIT 5 </code></pre> <p>It takes incredible 23 secs! With <code>FORCE INDEX (advertiser_id)</code> I can take it to 1 sec. If I remove either the GROUP BY or the ORDER BY, it goes down to 0.0003 secs.</p> <p>Something's gotta be wrong with my tables/indexes? I should not need <code>FORCE INDEX</code> (btw: <code>USE INDEX</code> doesn't work - I need to force it!) and it shouldn't take that long!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload