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

相关推荐
薛定谔的算法10 小时前
phoneGPT:构建专业领域的检索增强型智能问答系统
前端·数据库·后端
Databend12 小时前
Databend 亮相 RustChinaConf 2025,分享基于 Rust 构建商业化数仓平台的探索
数据库
得物技术13 小时前
破解gh-ost变更导致MySQL表膨胀之谜|得物技术
数据库·后端·mysql
Raymond运维17 小时前
MariaDB源码编译安装(二)
运维·数据库·mariadb
沢田纲吉17 小时前
🗄️ MySQL 表操作全面指南
数据库·后端·mysql
RestCloud1 天前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
RestCloud1 天前
为什么说零代码 ETL 是未来趋势?
数据库·api
ClouGence1 天前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
DemonAvenger2 天前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化
AAA修煤气灶刘哥2 天前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql