更新时间:2023-08-27 11:41:10
I think it's documented behavior of LIMIT optimization, see http://dev.mysql.com/doc/refman/5.5/en/limit-optimization.html
优化LIMIT查询
Optimizing LIMIT Queries
MySQL有时会优化具有LIMIT row_count子句而没有HAVING子句的查询:
MySQL sometimes optimizes a query that has a LIMIT row_count clause and no HAVING clause:
[...] 如果您将LIMIT row_count与ORDER BY一起使用,MySQL会在找到排序结果的前row_count行后立即结束排序,而不是对整个结果进行排序.如果通过使用索引进行排序,这将非常快.如果必须执行文件排序,则在找到第一个row_count之前,将选择与查询匹配的所有行(不带LIMIT子句),并对其中的大多数或全部进行排序.找到初始行后,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 are selected, and most or all of them are sorted, before the first row_count are found. After the initial rows have been found, MySQL does not sort any remainder of the result set.
[...]
MySQL一旦向客户端发送了所需的行数,它将立即中止查询,除非您使用的是SQL_CALC_FOUND_ROWS.
As soon as MySQL has sent the required number of rows to the client, it aborts the query unless you are using SQL_CALC_FOUND_ROWS.
因为您要获取某个日期旁边的ID,所以我认为对结果进行排序非常重要,因为否则您可以获取任意值.否则,您必须在条件中使用MIN(id)以获得所需的id值.
Because you're trying to get the ID next to a certain date, I would think ordering the result very vital, because else you can get an arbitrary value. Else you've got to use MIN(id) with your conditions to get the desired id value.