sql中select查询大数据量表时很慢

有一张表table1数据量大概130万,查询sql如下:

select count(*) from(

select distinct a.example_id,a.plan_id,a.trustor_id from table1 a

where DEL_STATUS=0 and (example_id,1,2) in(...)

and plan_id in(...)

)

查询花了将近40秒钟,结果集的数据量就有50万,而且多加一个where条件,都会导致查询耗时增加,就算加索引也没有效果,因为我这里必须要加几个where条件,而这些条件又无法过滤掉大部分的数据,所以只会白白增加耗时。

经过一段时间对sql的修改调整,调整为如下的sql:

select count(*) from(

select a.example_id,a.plan_id,a.trustor_id,count(*) cnt from table1 a

where DEL_STATUS=0 and (example_id,1,2) in(...)

group by a.example_id,a.plan_id,a.trustor_id

) where plan_id in()

不使用distinct,而是使用count + group by来去重,然后在外层过滤where条件,发现查询只要5秒多,速度一下子快了太多,可能是数据库中group by有着天然的优势吧

相关推荐
“αβ”2 小时前
MySQL表的操作
linux·网络·数据库·c++·网络协议·mysql·https
p***s912 小时前
Spring数据库原理 之 DataSource
java·数据库·spring
虹科网络安全2 小时前
艾体宝干货 | Redis Java 开发系列#1 从零开始的环境搭建与实践指南
java·数据库·redis
火山引擎开发者社区2 小时前
火山引擎向量数据库 Milvus 版正式商业化:AI 时代的向量检索新标杆
数据库·milvus·火山引擎
神秘的土鸡3 小时前
openEuler 25.09 企业级 MySQL主从复制部署与性能优化实战提升50%
linux·数据库·mysql·性能优化·openeuler
韩立学长3 小时前
基于Springboot课堂教学辅助系统08922bq1(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·后端
goxingman4 小时前
Oracle视图基础
数据库·oracle
黎相思4 小时前
MySQL索引特性
数据库·mysql
rit84324995 小时前
压缩感知信号恢复算法:OMP与CoSaMP对比分析
数据库·人工智能·算法