Retrieving only a fixed number of rows in MySQL

后端 未结 6 1354
时光取名叫无心
时光取名叫无心 2020-12-03 07:55

I am testing my database design under load and I need to retrieve only a fixed number of rows (5000)

I can specify a LIMIT to achieve this, however it seems that th

相关标签:
6条回答
  • 2020-12-03 08:09

    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.

    0 讨论(0)
  • 2020-12-03 08:12

    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.

    0 讨论(0)
  • 2020-12-03 08:12

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

    0 讨论(0)
  • 2020-12-03 08:15

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

    0 讨论(0)
  • 2020-12-03 08:15

    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.

    0 讨论(0)
  • 2020-12-03 08:16
     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).

    0 讨论(0)
提交回复
热议问题