Hi i have a 7milion records db table for testing query speed.
I tested up my 2 queries which are the same query with different limit parametres:
query 1 –
SELECT *
FROM table
LIMIT 20, 50;
query 2 –
SELECT *
FROM table
LIMIT 6000000, 6000030;
query exec times are:
- query 1 – 0.006 sec
- query 2 – 5.500 sec
In both of these queries, I am fetching same number of records, but in the second case it’s taking more time. Can someone please explain the reasons behind this?
Without looking into it too closely, my assumption is that this occurs because the first query only has to read to the 50th record to return results, whereas the second query has to read six million before returning results. Basically, the first query just shorts out quicker.
I would assume that this has an incredible amount to do with the makeup of the table – field types and keys, etc.
If a record is made up of fixed-length fields (e.g. CHAR vs. VARCHAR), then the DBMS can just calculate where the nth record starts and jumps there. If its variable length, then you would have to read the records to determine where the nth record starts. Similarly, I’d further assume that tables which have appropriate primary keys would be quicker to query than those without such keys.