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有着天然的优势吧

相关推荐
一颗宁檬不酸3 小时前
文件管理知识点
数据库
3 小时前
达梦数据库-事务
数据库·达梦数据库·dm
网硕互联的小客服4 小时前
MYSQL数据库和MSSQL数据库有什么区别?分别适用于什么脚本程序?
数据库·mysql·sqlserver
weixin_462446235 小时前
【原创实践】python 获取节假日列表 并保存为excel
数据库·python·excel
RPA 机器人就找八爪鱼5 小时前
RPA 赋能银行数字化转型:四大核心应用场景深度解析
数据库·人工智能·rpa
掂掂三生有幸5 小时前
使用openGauss搭建一个监狱管理系统
数据库
VX:Fegn08955 小时前
计算机毕业设计|基于Java人力资源管理系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot·后端·课程设计
siriuuus5 小时前
Redis 安装、多实例部署、主从复制及 Cluster 实践
数据库·redis·centos
Polaris_GQ6 小时前
Navicat连接Oracle数据库报错:12514问题
数据库
老华带你飞6 小时前
健身房|基于springboot + vue健身房管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端