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 小时前
HBase: 看上去很美
大数据·数据库·hbase
io无心3 小时前
Shardingsphere5分库分表
数据库·mysql
wWYy.4 小时前
Mysql:主键索引 唯一索引 普通索引 前缀索引
数据库·mysql
宝杰X74 小时前
Android Room3 多平台数据库
android·数据库
sky_8106138 小时前
Oracle ERP 各模块业务管理功能及底层表说明
数据库·oracle
YMatrix 官方技术社区9 小时前
CittaBase vs. Neo4j :原生图性能实测与混合检索实践
数据库·功能测试·ymatrix
闲猫10 小时前
LangChain / Integrations / Integrations by component / Tool
java·数据库·langchain
xqqxqxxq10 小时前
SQL 连接查询技术笔记
数据库·笔记·sql
Databend10 小时前
从万亿级大模型到全线应用:Databend Cloud 助力头部 AI 企业构建全链路 Trace 数据管道
大数据·数据库·sql
MC皮蛋侠客11 小时前
SQLAlchemy 系列(十一):从 1.x 到 2.x——渐进迁移与数据访问层治理
数据库·python