3

i would like to know if limit and offset are executed after the row were selected:

SELECT * FROM users WHERE id > 4 LIMIT 0,90 ORDER BY datetime DESC; 

Does this query first selects all the users's rows then apply the LIMIT, or does this query first apply the LIMIT then selects the users's rows?

1
  • 2
    using LIMIT without and ORDER BY means you will not get predictable results. If you're using LIMIT you really should define what order you want the results back. Otherwise MySQL makes no guarantees about what rows you will actually receive. Commented Nov 3, 2012 at 9:30

4 Answers 4

4

The FROM clause is the first to be executed in the query, then the WHERE clause. After that the LIMIT clause is applied.

So, the following are the Logical query processing steps for the query you posted:

  • FROM clause return all users.
  • WHERE clause is applied. Only the users with id > 4 get passed to the next step.
  • Then LIMIT.
Sign up to request clarification or add additional context in comments.

Comments

2

So the question you ask doesn't quite make sense here.

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?"

There are two possible cases here, and one extra thing to consider, and a problem with your original query.

  1. 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.

So now that you're using ORDER BY there are two possibilities.

  1. 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.

  2. MySQL can't 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.

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.

This slide deck on efficient pagination in MySQL explains a lot: http://www.scribd.com/doc/14683263/Efficient-Pagination-Using-MySQL

1 Comment

you can get a deterministic LIMIT equivalent using SQL:2011 features. search for WITH TIES.
1

From http://dev.mysql.com/doc/refman/5.0/en/limit-optimization.html

If you use LIMIT row_count with ORDER BY, MySQL ends the sorting as soon as it has found the first row_count rows of the sorted result, rather than sorting the entire result. If ordering is done by using an index, this is very fast. If a filesort must be done, all rows that match the query without the LIMIT clause must be selected, and most or all of them must be sorted, before it can be ascertained that the first row_count rows have been found. In either case, after the initial rows have been found, there is no need to sort any remainder of the result set, and MySQL does not do so.

Main Reference Link

1 Comment

MySQL never does a partial filesort. If it can't order using an index, it will always sort the entire result set before it applies the LIMIT clause.
1

Select Query

SELECT * FROM users WHERE id > 4 LIMIT 0,90; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.