我在这样的MySQL数据库上有一个左连接.
select *
from tableA
left join tableB on (tableA.id=tableB.id1 and tableB.col2='Red')...which在tableA和tableB上执行>500 K行的OK
但是,将其更改为此(并假设索引正常).
select *
from tableA
left join tableB on
((tableA.id=tableB.id1 and tableB.col2='Red') OR
(tableA.id=tableB.id2 and tableB.col2='Blue') )就性能而言,...kills it。
为什么演出会如此火爆?我能换个办法吗?
发布于 2014-04-08 05:27:23
编辑
不太确定你需要什么
我不认为它更有效率,但是试一试。
select
*
from
tableA as a
left join tableB as b1
on a.id=b1.id1
and b1.col2='Red'
left join tableB as b2
on a.id=b2.id2
and b2.col2='Blue'
where
(b1.id1 is not null or b2.id2 is not null)
or (b1.id1 is null and b2.id2 is null)你必须用SELECT和CASE WHEN来管理结果.
您可以比较相应列的性能和索引(这取决于完整表和查询中的内容,但这里应该是id, id1 and col2)。
发布于 2014-04-08 03:51:36
哦没注意到..。这个怎么样..。不要执行死刑,这样你就能更好地判断。
select *
from tableB, tableA
where (tableA.id=tableB.id1 and tableB.col2='Red') OR
(tableA.id=tableB.id2 and tableB.col2='Blue')https://stackoverflow.com/questions/22926885
复制相似问题