我是一名销售人员开发人员,我必须编写一个SQL查询并做到了,但性能却很低。你能帮我吗?
我的查询是这样的:
select col1, col2,col3,col4
from table1
where col1 is not null
and col2='ABC'
and (col3 IN (SELECT field1 FROM table 2)
OR col4 in('A','B','C'))
有办法我可以优化它以获得更好的性能吗?
更新
我使用左外部联接来实现它,这是正确的方法吗?
答案 0 :(得分:2)
尝试以下查询:
SELECT col1, col2,col3,col4 FROM TABLE1 T1
WHERE (EXISTS (SELECT 1 FROM TABLE2 T2 WHERE T1.COL3 = T2.FIELD1)
OR COL4 IN ('A','B','C'))
SELECT col1, col2,col3,col4 FROM TABLE1 T1
WHERE EXISTS (SELECT 1 FROM TABLE2 T2 WHERE T1.COL3 = T2.FIELD1)
UNION
SELECT col1, col2,col3,col4 FROM TABLE1 T1 WHERE COL4 IN ('A','B','C')