我需要提高此查询的性能:
Select
t1.column1, t1.column2
From
table1 t1, table2 t2
Where
t1.id = t2.id and
t1.some_column = t2.some_column
--------
minus
--------
Select
t1.column1, t1.column2
From
table1 t1, table2 t2, table3 t3
Where
t1.id = t2.id and
t1.some_column = t2.some_column and
t1.id = t3.id
我正在尝试使用“not exists”而不是“minus”重写此查询。有人可以给我一个建议吗?
答案 0 :(得分:4)
怎么样?
Select
t1.column1, t1.column2
From
table1 t1, table2 t2
Where
t1.id = t2.id
and t1.some_column = t2.some_column
and not exists (select 1 from table3 t3 where t1.id = t3.id)
答案 1 :(得分:4)
它取决于第一个查询和table3之间的比例大小/# - 行:
SELECT t1.column1, t1.column2
FROM table1 t1, table2 t2
WHERE t1.id = t2.id
AND t1.some_column = t2.some_column
AND t1.id NOT IN (SELECT t3.id FROM table3 t3);
任何一个解决方案都可以从每个表id
列的索引中受益,如果是这样的话,还可以在用于过滤第一个查询的列上使用其他索引(或包含id
的复合索引)。 / p>