Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So the question you ask doesn't quite make sense here.</p> <p>What you should really be asking is, "Does MySQL have to find all rows that match the WHERE clause in the query, or can it apply the LIMIT and only have to read at most N number of rows?"</p> <p>There are two possible cases here, and one extra thing to consider, and a problem with your original query.</p> <ol> <li>If you're using LIMIT you should be using ORDER BY. MySQL has no "natural" order, which means the rows you actually get back from that query is not deterministic. This is a bad thing.</li> </ol> <p>So now that you're using ORDER BY there are two possibilities.</p> <ol> <li><p>MySQL can use an index to satisfy the ORDER BY clause. In this case, it can scan the table in index order looking for matches until the LIMIT is satisfied and then it will stop reading there. This is efficient.</p></li> <li><p>MySQL <em>can't</em> use an index. Then it will have to find all possible matches and sort the entire result set before applying the LIMIT. This is generally pretty slow if there are many rows.</p></li> </ol> <p>The additional thing to consider is that when you use an offset with limit, mysql has to read all rows up to the starting position before it will return results. This becomes very slow when the offset value is very large.</p> <p>This slide deck on efficient pagination in MySQL explains a lot: <a href="http://www.scribd.com/doc/14683263/Efficient-Pagination-Using-MySQL" rel="nofollow">http://www.scribd.com/doc/14683263/Efficient-Pagination-Using-MySQL</a></p>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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