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

相关推荐
liulilittle37 分钟前
Linux shell 搜索指定后缀名文件,并复制到指定目录。
linux·服务器·数据库
必胜刻38 分钟前
Redis哨兵模式(Linux)
linux·数据库·redis
dualven_in_csdn1 小时前
【数据库损坏】关于一次现场数据库损坏
数据库·mysql
锦衣夜行?1 小时前
oracle 未知长度从左到右截取某个字符串
数据库·oracle
han_hanker2 小时前
@JsonIgnore,@JsonProperty, @JsonInclude,@JsonFormat
数据库·oracle
hanyi_qwe2 小时前
MySQL事务基础
数据库·mysql
l1t2 小时前
三种用SQL解决Advent of Code 2022第8题 树顶木屋 的比较和分析
数据库·sql·oracle·duckdb·advent of code
如果未来,2 小时前
Oracle的Redo log和Undo log的区别
数据库·oracle
koping_wu2 小时前
【方案设计】Mysql相关场景
数据库·mysql
杨云龙UP2 小时前
SQL Server小技巧:用 SSMS 重置登录密码,不影响正在运行的系统
运维·服务器·数据库·sql·sqlserver