sql 语句 in的优化。

2025-03-24 11:08:27
推荐回答(4个)
回答1:

假设原来的句子是
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上加索引,提高查询速度。

回答2:

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

回答3:

尝试在 in 的这个字段上建索引

回答4:

select * from t1
join t2 on t1.f1=t2.f2
where t2.f2=xxx