MySQL 如何处理查询中的 ORDER BY 和 LIMIT?
我有一个如下所示的查询:
SELECTarticleFROMtable1ORDERBYpublish_dateLIMIT20
ORDERBY 是如何工作的?它会对所有记录进行排序,然后获取前 20 条记录,还是获取 20 条记录并按 publish_date
字段对它们进行排序?
如果是最后一篇,则不能保证您真正获得最新的 20 篇文章。
I have a query that looks like this:
SELECT article FROM table1 ORDER BY publish_date LIMIT 20
How does ORDER BY work? Will it order all records, then get the first 20, or will it get 20 records and order them by the publish_date
field?
If it's the last one, you're not guaranteed to really get the most recent 20 articles.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
它将首先排序,然后获取前 20 个。数据库还将处理
ORDER BY
之前的WHERE
子句中的任何内容。It will order first, then get the first 20. A database will also process anything in the
WHERE
clause beforeORDER BY
.LIMIT 子句可用于限制 SELECT 语句返回的行数。 LIMIT 接受一个或两个数字参数,它们必须都是非负整数常量(使用准备好的语句时除外)。
使用两个参数,第一个参数指定要返回的第一行的偏移量,第二个参数指定要返回的最大行数。初始行的偏移量为 0(不是 1):
SELECT * FROM tbl LIMIT 5,10; # 检索行 6-15
要检索从某个偏移量到结果集末尾的所有行,可以使用较大的数字作为第二个参数。此语句检索从第 96 行到最后一行的所有行:
SELECT * FROM tbl LIMIT 95,18446744073709551615;
使用一个参数,该值指定从结果集开头返回的行数:
从 tbl LIMIT 5 中选择*; # 检索前 5 行
换句话说,LIMIT row_count 相当于 LIMIT 0, row_count。
所有详细信息: http://dev.mysql.com/doc/refman /5.0/en/select.html
The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements).
With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):
SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15
To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. This statement retrieves all rows from the 96th row to the last:
SELECT * FROM tbl LIMIT 95,18446744073709551615;
With one argument, the value specifies the number of rows to return from the beginning of the result set:
SELECT * FROM tbl LIMIT 5; # Retrieve first 5 rows
In other words, LIMIT row_count is equivalent to LIMIT 0, row_count.
All details on: http://dev.mysql.com/doc/refman/5.0/en/select.html
正如 @James 所说,它将对所有记录进行排序,然后获取前 20 行。
这样,您保证会看到 20 篇最先发表的文章,较新的文章将不会显示。
根据您的情况,我建议您将
desc
添加到order bypublish_date
中,如果您想要最新的文章,那么最新的文章将是第一个。如果您需要将结果保持升序排列,并且仍然只想要 10 篇最新文章,您可以要求 mysql 对结果进行两次排序。
下面的这个查询将对结果进行降序排序,并将结果限制为 10(即括号内的查询)。仍然会按照降序排序,我们对此不满意,所以我们要求mysql再排序一次。现在我们在最后一行得到了最新的结果。
如果您需要所有列,可以这样完成:
当我手动编写查询来检查数据库中的各种内容时,我会使用这种技术。我还没有在生产环境中使用过它,但是现在当我对其进行基准测试时,额外的排序不会影响性能。
Just as @James says, it will order all records, then get the first 20 rows.
As it is so, you are guaranteed to get the 20 first published articles, the newer ones will not be shown.
In your situation, I recommend that you add
desc
toorder by publish_date
, if you want the newest articles, then the newest article will be first.If you need to keep the result in ascending order, and still only want the 10 newest articles you can ask mysql to sort your result two times.
This query below will sort the result descending and limit the result to 10 (that is the query inside the parenthesis). It will still be sorted in descending order, and we are not satisfied with that, so we ask mysql to sort it one more time. Now we have the newest result on the last row.
If you need all columns, it is done this way:
I use this technique when I manually write queries to examine the database for various things. I have not used it in a production environment, but now when I bench marked it, the extra sorting does not impact the performance.
您可以在订单末尾添加 [asc] 或 [desc] 来获取最早或最新的记录
例如,这将首先为您提供最新的记录
在
之后附加
LIMIT
子句排序依据You could add [asc] or [desc] at the end of the order by to get the earliest or latest records
For example, this will give you the latest records first
Append the
LIMIT
clause afterORDER BY
如果有一个合适的索引,在本例中是在
publish_date
字段上,那么 MySQL 不需要扫描整个索引来获取所请求的 20 条记录 - 这 20 条记录将在索引的开头找到。但如果没有合适的索引,那么就需要对表进行全扫描。有一篇 MySQL 性能博客文章 从 2009 年开始就此事。
If there is a suitable index, in this case on the
publish_date
field, then MySQL need not scan the whole index to get the 20 records requested - the 20 records will be found at the start of the index. But if there is no suitable index, then a full scan of the table will be needed.There is a MySQL Performance Blog article from 2009 on this.
您可以使用此代码
从 table1 中选择文章 ORDER BYpublish_date LIMIT 0,10
其中 0 是记录的起始限制 & 10条记录数
You can use this code
SELECT article FROM table1 ORDER BY publish_date LIMIT 0,10
where 0 is a start limit of record & 10 number of record
LIMIT 通常作为最后一个操作应用,因此结果将首先排序,然后限制为 20 个。事实上,一旦找到前 20 个排序结果,排序就会停止。
LIMIT is usually applied as the last operation, so the result will first be sorted and then limited to 20. In fact, sorting will stop as soon as first 20 sorted results are found.
可以简化为:
SELECTarticleFROMtable1ORDERBYpublish_dateDESCFETCHFIRST20ROWSONLY;
您还可以在
ORDERBY
中添加许多参数,这些参数只是用逗号分隔,例如:ORDER BYpublish_date、tab2、tab3 DESC
等...Could be simplified to this:
SELECT article FROM table1 ORDER BY publish_date DESC FETCH FIRST 20 ROWS ONLY;
You could also add many argument in the
ORDER BY
that is just comma separated like:ORDER BY publish_date, tab2, tab3 DESC
etc...