用织梦做模板网站,高校保卫处网站建设工作总结,学编程可以建设网站吗,购物商城网站建设方案最近遇到查询一张大数据量表时#xff0c;需要对一个字段做in查询#xff0c;in中的元素数量可能达到几千个#xff0c;即使对这个字段加上索引#xff0c;速度也慢到无法接受
示例表结构如下#xff1a; 表中有几十万的数据#xff0c;且example_id和data_id字段加了联…最近遇到查询一张大数据量表时需要对一个字段做in查询in中的元素数量可能达到几千个即使对这个字段加上索引速度也慢到无法接受
示例表结构如下 表中有几十万的数据且example_id和data_id字段加了联合索引只做一个简单的select查询
select * from TEST_TABLE01 where example_id:exampleId and data_id in(:dataIds) 其中in存在1000个元素查询速度很慢因为in的个数太多会全表扫描导致索引失效。
优化方案
不使用in语法将sql语句简化成下面这种索引就生效了
select * from TEST_TABLE01 where example_id:exampleId and data_id:dataId
但是这样一次只能查询一条data_id匹配的数据这就意味着程序要和数据库交互1000次但是我测试的速度要快于上面的in方式。
进一步优化减少数据库交互方式使用union all拼接sql
select * from TEST_TABLE01 where example_id:exampleId and data_id:dataId0
union all
select * from TEST_TABLE01 where example_id:exampleId and data_id:dataId1
union all
select * from TEST_TABLE01 where example_id:exampleId and data_id:dataId2
union all
select * from TEST_TABLE01 where example_id:exampleId and data_id:dataId3
...
...
union all
select * from TEST_TABLE01 where example_id:exampleId and data_id:dataId999 程序中对dataId的参数进行组装这样只和数据库交互一次索引也不会失效这种方式解决了in查询慢的问题。
对于delete也可以使用类似的方式优化
delete from TEST_TABLE01 a
WHERE exists (select * from (select * TEST_TABLE01 where example_id:exampleId and data_id:dataId0union allselect * TEST_TABLE01 where example_id:exampleId and data_id:dataId1) b where a.idb.id
)