gpt4 book ai didi

mysql - 请问为什么 "ERROR 1235 (42000): This version of MySQL doesn' t还不支持 'LIMIT & IN/ALL/ANY/SOME subquery'”?

转载 作者:行者123 更新时间:2023-11-29 06:54:59 26 4
gpt4 key购买 nike

我知道这个限制和重写 SQL 的替代方案。但我想知道为什么?任何人都可以提供有关此限制的推论吗?

最佳答案

类似的查询

select * from table where id in (select id from othertable)

基本上会被解释为

select * from table where exists 
(select id from othertable where table.id = othertable.id)

这就是您对该查询的期望。您特别希望 IN 查询使用 othertable.id 上的索引。在 manual ,描述为

Some optimizations that MySQL itself makes are: [...]

  • MySQL rewrites IN, ALL, ANY, and SOME subqueries in an attempt to take advantage of the possibility that the select-list columns in the subquery are indexed.

如果添加限制,这些正是错误消息中提到的四个运算符,这并非纯粹巧合:

select * from table where id in 
(select id from othertable order by id limit 10)

ERROR 1235 (42000): This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'.

以类似的方式重写该查询不再是可能的,因为它不同于

select * from table where exists 
(select id from othertable where table.id = othertable.id
order by id limit 10)

要执行带有限制的IN,MySQL 可以检索othertable 的前 10 行,将该结果集存储为派生子表,并且检查 id 是否在那里。你当然可以这样做:

select * from table where id in 
(select id from
(select id from othertable order by id limit 10) subtable)

与第一个示例类似,这将被解释为

select * from table where exists 
(select * from
(select id from othertable order by id limit 10) subtable
where table.id = subtable.id)

所以它混合了优化器的工作方式(它将重写查询)和 limit 的工作方式(它在找到行后停止执行,而不是跳过它们) 、预期是什么(索引的使用)以及最终开发人员是否决定允许特定语法。

您可能会争辩说,如果 MySQL 遇到 INlimit 子查询,那么它总是可以回退到将查询作为派生表执行 - 但您也可以通过显式使用派生子表。你也可能会说,你可以想办法实现这一点,或者以不同的方式实现它——你是对的,确实有。这就是错误消息中出现“yet”的原因。因此,请随意实现它们或至少描述它们,例如尽可能彻底地提出功能请求,并考虑 MySQL 的所有其他部分如何工作。但请确保它们实际上比仅使用子表更快。

关于mysql - 请问为什么 "ERROR 1235 (42000): This version of MySQL doesn' t还不支持 'LIMIT & IN/ALL/ANY/SOME subquery'”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45995825/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com