系统里这个同时查冷热表的sql,动动手指,从1
https://www.518cn.com 发布时间:2025-03-18 22:28 作者:网络
摘要:系统将交易数据按交易时间分为热表(最近3个月)和冷表(3个月前)。为保证用户体验,当企业门户端查询跨越冷热表时,尤其针对大客户,查询性能优化至关重要。以下是程序的SQ
系统将交易数据按交易时间分为热表(最近3个月)和冷表(3个月前)。为保证用户体验,当企业门户端查询跨越冷热表时,尤其针对大客户,查询性能优化至关重要。以下是程序的SQL查询语句及其优化版本。
系统里的交易数据按交易时间做了冷热表分离(热表仅存储最近3个月的交易数据,3个月前的交易数据自动结转至冷表),我们内部运营系统的交易查询功能进行了冷热数据分开查询。
然后企业客户端呢,为了不影响用户体验,企业门户端的交易查询功能,当选择的查询时间段同时涉及到冷热表时,需要union(合并)两表进行数据查询。这时,尤其是针对那些交易量比较大的客户来说,在查询性能上我们就要做一些努力。
上sql
select
count(*) as orderNum,
IFNULL(sum(amount), 0) as totalAmt,
SUM(CASE WHEN order_status = 'SUCCESS' THEN amount ELSE 0 END) as totalSuccessAmt,
SUM(CASE WHEN order_status = 'FAIL' THEN amount ELSE 0 END) as totalFailAmt
from
(
select
*
from
order_detail
WHERE
enterprise_id = 1655100723787649
and create_time >= '2024-02-04 00:00:00'
and create_time <= '2024-09-20'
UNION
select
*
from
order_detail_mig
WHERE
enterprise_id = 1655100723787649
and create_time >= '2024-02-04 00:00:00'
and create_time <= '2024-09-20'
) od
### 执行计划及耗时
本文sql查询方式:本地通过堡垒机访问生产库,执行耗时15s。(生产log实际耗时≈12s)
id |
select_type |
table |
partitions |
type |
possible_keys |
key |
key_len |
ref |
rows |
filtered |
Extra |
1 | PRIMARY | <derived2> | (null) | ALL | (null) | (null) | (null) | (null) | 403059 | 100 | (null) |
2 | DERIVED | order_detail | (null) | ref | uk_enterprise_order_no,idx_create_time,idx_create_time_payee_account_enterprise_id | uk_enterprise_order_no | 8 | const | 376527 | 50 | Using where |
3 | UNION | order_detail_mig | (null) | ref | uk_enterprise_order_no,idx_create_time | uk_enterprise_order_no | 8 | const | 429592 | 50 | Using where |
4 | UNION RESULT | <union2,3> | (null) | ALL | (null) | (null) | (null) | (null) | (null) | (null) | Using temporary |
sql优化后v1
先把`UNION`换成`UNION ALL`
### 执行计划及耗时
sql优化后v2
select
count(*) as orderNum,
sum(amount) as totalAmt,
order_status
from
order_detail
WHERE
enterprise_id = 1655100723787649
and create_time >= '2024-02-04 00:00:00'
and create_time <= '2024-09-20'
group by order_status
UNION ALL
select
count(*) as orderNum,
sum(amount) as totalAmt,
order_status
from
order_detail_mig
WHERE
enterprise_id = 1655100723787649
and create_time >= '2024-02-04 00:00:00'
and create_time <= '2024-09-20'
group by order_status
### 执行计划及耗时
相关文章
- 优化GreatSQL日志文件空间占用 GreatSQL对于日志文件磁盘空间占用,做了一些优化,对于binlog、...03-18
- "数据约束条件" date: 2022-11-24T21:24:31 08:00 draft: false MySQL字段约束条件 无符号, 零填充...03-18
【GreatSQL优化器-16】INDEX_SKIP_SCAN
【GreatSQL优化器-16】INDEX_SKIP_SCAN 一、INDEX_SKIP_SCAN介绍 GreatSQL 优化器的索引跳跃扫描(Index Ski...03-18- MySQL 是一个非常流行的开源关系数据库管理系统,在各种应用场景中都得到了广泛的应用。随...03-18
- 🤖 DB-GPT 是一个开源的 AI 原生数据应用程序开发框架,具有 AWEL(代理工作流表达式语...03-18
GreatSQL 8.0.32-27 GA (2025-3-10)
GreatSQL 8.0.32-27 GA (2025-3-10) 版本信息 发布时间:2025年3月10日 版本号:8.0.32-27, Revision aa66a38591...03-18- 6. MySQL 索引的数据结构(详细说明) @目录6. MySQL 索引的数据结构(详细说明)1. 为什么使用索引2...03-18
- @Override @Transactional(rollbackFor = Exception.class) public void batchInsertDeviceData(IotMsgNotifyData iotMsgNotifyDa...03-18
- 个人Qt项目总结——数据库查询断言问题 问题: 当我使用MySQL数据库的查询操作时, 如果查询...03-18
- MySQL 是一种广泛使用的关系数据库管理系统,MySQL 8 是其最新的主要版本,结合了出色的性能和...03-18
最新评论