MYSQL where in (*,*,*,*,*,...................) 大量查询优化


今天遇到的问题查询需要使用 where in ,虽然MySQL对于IN做了相应的优化,即将IN中的常量全部存储在一个数组里面,而且这个数组是排好序的。但是如果数值较多,产生的消耗也是比较大的。

1:select id from t where num in(1,2,3) 对于连续的数值,能用between就不要用in了;

2:使用join连接来替换。

下面两段是我查资料时在评论区看到的,实际操作确实优化作用很大,特此记录:

优化前:
select docId from tab1 where word in (select word from tab1 where docId=123) group by docId limit 1000;
优化后: select docId from (select word from tab1 where docId
=123) as t2 join tab1 t on t.word=t2.word where t2.word is not null GROUP BY docId limit 1000

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM