mysql替代in_mysql百万数据查询⽤什么代替in,该如何处
理?
展开全部
mysql百万数据查询⽤exists 代替 in 是⼀个好的选择:
select num from a where num in(select num from b)
⽤下⾯的32313133353236313431303231363533e78988e69d8331333365633864语句替换:
select num from a where exists(select 1 from b where num=a.num)
SQL查询语句优化⽅法:
1、应尽量避免在 where ⼦句中使⽤!=或<>操作符,否则将引擎放弃使⽤索引⽽进⾏全表扫描。
2、对查询进⾏优化,应尽量避免全表扫描,⾸先应考虑在 where 及 order by 涉及的列上建⽴索引。
3、应尽量避免在 where ⼦句中对字段进⾏ null 值判断,否则将导致引擎放弃使⽤索引⽽进⾏全表扫描,如:
select id from t where num is null
可以在num上设置默认值0,确保表中num列没有null值,然后这样查询:
select id from t where num=0
4、尽量避免在 where ⼦句中使⽤ or 来连接条件,否则将导致引擎放弃使⽤索引⽽进⾏全表扫描,如:
select id from t where num=10 or num=20
可以这样查询:
select id from t where num=10
union allsql语句替换表中内容
select id from t where num=20
5、下⾯的查询也将导致全表扫描:(不能前置百分号)
select id from t where name like ‘%c%’
若要提⾼效率,可以考虑全⽂检索。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。