如何在SQL中使用"LIMIT"参数? [英] How does 'LIMIT' parameter work in sql?

查看:347
本文介绍了如何在SQL中使用"LIMIT"参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有4000行,并且定义了X个限制.

I have 4000 rows for example, and I define X limit.

查询找到X行后停止了吗?还是查询找到所有行,然后从找到的行中获取X行?

The query stops after it finds X rows? or the query finds all the rows and then takes X rows from the found rows?

谢谢.

推荐答案

来自

如果您将LIMIT row_count与ORDER BY一起使用,则MySQL结束排序为 一旦找到排序结果的前row_count行, 而不是对整个结果进行排序.如果订购是通过使用 索引,这是非常快的.如果必须执行文件排序,则所有 必须选择不带LIMIT子句的匹配查询,并且大多数或 必须先对它们全部进行分类,然后才能确定 找到第一个row_count行.无论哪种情况,在 已找到初始行,无需对任何剩余行进行排序 结果集,而MySQL则没有.

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.

因此,在应用LIMIT之前,知道整个结果集似乎是可能.但是MySQL会尝试所有无法做到的事情.而且,您可以通过提供与查询匹配的有用索引来提供帮助.

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.

此外,如果未对集合进行排序,则在将足够的行流式传输到结果集后,它将终止SELECT操作.

Furthermore, if the set is not sorted it terminates the SELECT operation as soon as it's streamed enough rows to the result set.

这篇关于如何在SQL中使用"LIMIT"参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆