Retrieving only a fixed number of rows in MySQL

杀马特。学长 韩版系。学妹 提交于 2019-11-28 09:04:46

MySQL is smart in that if you specify a LIMIT 5000 in your query, and it is possible to produce that result without generating the whole result set first, then it will not build the whole result.

For instance, the following query:

SELECT * FROM table ORDER BY column LIMIT 5000

This query will need to scan the whole table unless there is an index on column, in which case it does the smart thing and uses the index to find the rows with the smallest column.

Teja
 SELECT * FROM `your_table` LIMIT 0, 5000 

This will display the first 5000 results from the database.

 SELECT * FROM `your_table` LIMIT 1001, 5000 

This will show records from 1001 to 6000 (counting from 0).

@Jarosław Gomułka is right
If you use LIMIT 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. 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. if the set is not sorted it terminates the SELECT operation as soon as it's got enough rows to the result set.

Complexity of such query is O(LIMIT) (unless you specify order by).

It means that if 10000000 rows will match your query, and you specify limit equal to 5000, then the complexity will be O(5000).

The exact plan the query optimizer uses depends on your query (what fields are being selected, the LIMIT amount and whether there is an ORDER BY) and your table (keys, indexes, and number of rows in the table). Selecting an unindexed column and/or ordering by a non-key column is going to produce a different execution plan than selecting a column and ordering by the primary key column. The later will not even touch the table, and only process the number of rows specified in your LIMIT.

As I explained in this article, each database defines its own ay of limiting the result set size depends on the database you are using.

While the SQL:2008 specification defines a standard syntax for limiting a SQL query, MySQL 8 does not support it.

Therefore, on MySQL, you need to use the LIMIT clause to restrict the result set to the Top-N records:

SELECT
    title
FROM
    post
ORDER BY
    id DESC
LIMIT 50

Notice that we are using an ORDER BY clause since, otherwise, there is no guarantee which are the first records to be included in the returning result set.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!