I have 4000 rows for example, and I define X limit.
The query stops after it finds X rows? or the query finds all the rows and then takes X rows from the found rows?
Thank you.
From MySQL Reference Manual:
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.
So it looks like it's possible that the entire result set is known before the LIMIT is applied. But MySQL will try everything it can not to do so. And you can help it by providing useful indexes that match your queries.
EDIT: Furthermore, if the set is not sorted it terminates the SELECT operation as soon as it's streamed enough rows to the result set.
SELECT * FROM your_table LIMIT 0, 10
This will display the first 10 results from the database.
SELECT * FROM your_table LIMIT 5, 5
This will show records 6, 7, 8, 9, and 10
It's like telling MySql; I want you to start counting from 5+1 or the 6th record, but Select only upto 5 records
SELECT * FROM your_table LIMIT 0, 10 // MySql will return 10 rows starting from 0 and in SELECT * FROM your_table LIMIT 5, 5 //MySql will Return rows Starting from the 6th & 7th, 8th, 9th, and 10th ... How doesn't this ANSWER Help the User? .... (some how) I feel Bullied by your commentsIntroduction to MySQL LIMIT clause
The following illustrates the LIMIT clause syntax with two arguments:
SELECT select_list FROM table_name LIMIT [offset,] row_count; offset specifies the offset of the first row to return. The offset of the first row is 0, not 1.row_count specifies the maximum number of rows to return.The following picture illustrates the LIMIT clause:
Therefore, these two clauses are equivalent:
> LIMIT row_count; > LIMIT 0 , row_count; The following picture illustrates the evaluation order of the LIMIT clause in the SELECT statement:
I'm assuming you're thinking about MySQL, in which according to the documentation, the answer is it depends. If you're using a LIMIT (without a HAVING), then:
LIMIT, MySQL uses indexes in some cases when normally it would prefer to do a full table scan.SQL_CALC_FOUND_ROWS.There are a few other cases which you should read about in the documentation.
It stops after it found the number of rows specified in the LIMIT clause. This can be verified with a large amount of data. It retrieves the result in a time that is not possible if it is getting all the rows of the table and filtering after that.
If you are using MS SQL Server, then you can write it as given below.
Select TOP [x] * From MyTable Hope it helps.
Vamyip
LIMITis not a SQL standard keyword, so which RDBMS are you using?