为何关联两张10行表的MySQL查询未返回10条结果?
MySQL关联查询未返回预期行数问题排查
我关联了两张各包含10行数据的表,关联字段为唯一列。我清楚CTE表可能存在重复值,但查询结果本应返回10条记录。
对应的SQL查询语句如下:
with ints as ( select 1 as i union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9 union all select 10 ),cte as ( select floor(rand()*10)+1 as x from ints ) select i.i, c1.x from ints i inner join cte c1 on c1.x=i.i order by i.i;
以下是随机生成的查询结果(每次执行结果会变化):
| i | x |
|---|---|
| 2 | 8 |
| 2 | 5 |
| 3 | 3 |
| 5 | 4 |
| 8 | 8 |
| 10 | 2 |
编辑:由于好奇MySQL在未使用LIMIT的情况下未返回10条记录的原因,我提交了编号为111852的bug报告。
编辑(2023-07-28):MySQL官方对该bug的回复:
您可以通过禁用派生表合并来避免此问题。该问题可能是优化器将派生表和视图引用合并到外部查询块时产生的。
解决该问题的命令如下:
set optimizer_switch = 'derived_merge=off';
内容的提问来源于stack exchange,提问作者Luuk
相关产品推荐
相关产品推荐

