当我试图检查不同测试之间的结果是否有差异时,我遇到了一个问题。内部select语句返回大约5000行,但连接不会在一分钟内完成。我预计输出大约是10行。加入这么慢有什么原因吗?

select * from(
           select *
           from R inner join C
           on R.i = C.j
           where C.j in (2343,3423,4222,1124,2344)
) AS A,(
           select *
           from R inner join C
           on R.i = C.j
           where C.j in (2343,3423,4222,1124,2344)
) AS B
where A.x = B.x and
A.y = B.y and
A.result <> B.result

最佳答案

我想你可以用聚合来做你想做的事:

select x, y, group_concat(distinct result) as results
from R inner join
     C
     on R.i = C.j
where C.j in (2343, 3423, 4222, 1124, 2344)
group by x, y
having count(distinct result) > 1;

对于这个查询,C(j)R(i)上的索引将非常有用。我也会将xy添加到适当的索引中,但我不知道它们是从哪个表进行组合的。

关于mysql - 缓慢的SQL连接5000行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37793456/

10-14 14:01
查看更多