[英]how to resolve preformance issue in IN Clause SQL Query for huge set of data?
我正在尝试从大量数据中进行查询。 查询继续运行,没有任何结果或错误。 我为一小组测试数据运行的相同查询工作正常。
询问:
> SELECT * FROM table1 t1 WHERE t1.col1 IN (SELECT distinct(t2.col2)
> FROM table2 t2 Left Join table3 t3 on t2.col1 = t3.col1 WHERE
> t3.col1=value);
我再次尝试使用
SELECT * FROM table1 t1 WHERE t1.col1 = (raw_value);
- 单值过滤器工作正常。
SELECT * FROM table1 t1 WHERE t1.col1 IN ( raw_value, raw_value);
- 一个以上的值过滤器会产生性能问题。
请给我建议以提高性能。
-谢谢你。
据我所知,不需要子查询中的left join
,因为过滤器位于用于on
条件的同一列上:如果是这样,只需将其删除。 我建议使用exists
来表达查询:
select t1.*
from table1 t1
where exists (select 1 from table2 t2 on t2.col2 = t1.col1 and t2.col1 = value)
为了提高性能,您需要table2(col1, value)
上的索引。 table1(col1)
上的索引也可能有帮助。
大型 IN() 子句的性能总是很差 - 没有办法解决这个问题。 解决方案是使用 JOIN 而不是 IN()。
更快的是:
SELECT *
FROM table1 t1
JOIN table2 t2 ON t1.col1 = t2.col2
LEFT JOIN table3 t3 ON t2.col1 = t3.col1
WHERE t2.col1=value;
此外,这里的LEFT JOIN
实际上不会做任何事情,所以最佳形式是:
SELECT *
FROM table1 t1
JOIN table2 t2 ON t1.col1 = t2.col2
WHERE t2.col1=value;
确保你有一个索引
table2 (col1)
table2 (col2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.