sql 语句 in的优化。

select(视图)fom ...where ...in...
其中in里有几千个ID。执行速度非常慢。如何进行优化?
最新回答
素觞流年

2026-10-22 10:59:28

假设原来的句子是
select * from t1 where t1.f1 in (select t2.f2 from t2 where t2.f2=xxx)
和你的很类似
你用子查询 很慢
我们现在修改为:
select t1.* from t1 ,t2 where t1.f1 = t2.f2 and t2.f2=xxxx
这里使用了关联查询代替了子查询大大提高效率。
其次你可以考虑在表t1.f1上加索引,提高查询速度。
步信停云

2026-05-21 14:53:23

这样试下:
把id放到一张表中,
where id in(select id from 表)
Dreams°終遇妳

2026-07-27 06:05:49

尝试在 in 的这个字段上建索引
梦散蝶逝

2026-02-17 00:56:18

select * from t1
join t2 on t1.f1=t2.f2
where t2.f2=xxx
收起 1条折叠回答