MySQL Forums
Forum List  »  General

Re: Jet Profiler says I'm slow....
Posted by: Rick James
Date: August 31, 2012 10:25AM

> SELECT * FROM items WHERE CAT='sports' ORDER BY DATUM DESC LIMIT 31900,20

Begs for a "compound" index:
INDEX(CAT, DATUM)

Even then, it will have to scan 31920 rows to get the answer. Or it may choose to do a full table scan, sort the results (on DATUM), then scan 31920 rows.

OFFSET is the main villain. Why are you doing it? It smells like "pagination". But do you really need to look at page 3191?

If you had PRIMARY KEY(ID), INDEX(CAT, DATUM, ID), then this would work somewhat faster:
SELECT  b.*
    FROM  
      ( SELECT  ID
            FROM  items
            WHERE  CAT='sports'
            ORDER BY  DATUM DESC
            LIMIT  31900,20 ) a
    JOIN  items b ON b.ID = a.ID;
The trick in that is to turn a table scan into an index scan.

Please show us the output of
SHOW CREATE TABLE items \G
SHOW TABLE STATUS LIKE 'items' \G

The real solution to "pagination via offset" is to turn it into remembering where you "left off".

Options: ReplyQuote


Subject
Written By
Posted
Re: Jet Profiler says I'm slow....
August 31, 2012 10:25AM
September 01, 2012 08:38AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.