SQL在DBA手里-改写篇

背景

最近运营需要做月报汇总交易情况,之前一直是他们手工出的数据,他们想做成月初自动发送邮件,从而减轻他们的工作量。于是他们提供SQL我们在邮件服务器配置做定时发送任务。

表介绍(表及字段已做脱敏处理)

  • trans_profits
    交易毛利表:仅记录每天毛利数据
  • trans_offline_order
    线下订单表:记录线下订单情况
  • trans_online_order
    线上订单表:记录线上订单情况

SQL "变装"过程

原始:SQL
  • 缺点:不易读,查询套子查询

  • 查询解读:将线下及线上订单"交易笔数""交易金额"数据合并再与毛利表按"交易日期"关联查询,显示:"交易笔数","交易金额","毛利金额","月份"
    --注:线上线下订单表为原始数据,毛利表为汇算后的数据,因此毛利表无需count(*)统计交易笔数;

    select d.month as 月,
    round(s.count/10000 , 2) ||'万' as 交易笔数,
    round(s.amt/10000 , 2) ||'万' as 交易金额,
    round(d.profits_amt/10000 , 2) ||'万' as 毛利金额
    from (SELECT to_char(trans_time, 'yyyyMM') as month,
    sum(profits_amt) as profits_amt
    FROM trans_profits -- 交易毛利表
    where trans_time >= to_date('20240101', 'yyyyMMdd')
    and trans_time < to_date('20241231', 'yyyyMMdd')
    group by to_char(trans_time, 'yyyyMM')) d
    left join (select month,
    sum(count) as count,
    sum(amt) as amt
    from (SELECT to_char(trans_time, 'yyyyMM') as month,
    count(1) as count,
    sum(trans_amt) as amt
    FROM trans_offline_order -- 线下订单表
    where trans_cd = '00'
    and trans_time >= to_TIMESTAMP('20240101', 'yyyyMMdd')
    and trans_time < to_TIMESTAMP('20241231', 'yyyyMMdd')
    group by to_char(trans_time, 'yyyyMM')
    union all
    SELECT to_char(trans_time, 'yyyyMM') as month,
    count(1) as count,
    sum(trans_amt) AS amt
    FROM trans_online_order -- 线上订单表
    WHERE trans_type IN ('01', '02')
    and trans_cd = '00'
    and trans_time >= to_TIMESTAMP('20240101', 'yyyyMMdd')
    and trans_time < to_TIMESTAMP('20241231', 'yyyyMMdd')
    group by to_char(trans_time, 'yyyyMM')) t
    group by month) s
    on d.month = s.month
    order by 1;

"变装":SQL
  • 优点:查询简洁易懂

  • 查询解读:将线上、线下及毛利表进行数据合并,其中计算"交易笔数"线上、线下虚拟出列为ct 值为1标记,毛利表因为不需要记得笔数因此ct值标记为0,最后汇总时用sum(ct)列即可得到"交易笔数"。

    SELECT
    substr(t.trans_time,0,6) 月,
    round(sum(ct) /10000 , 2) ||'万' as 交易笔数,
    round(sum(trans_amt)/10000 , 2) ||'万' as 交易金额,
    round(sum(profits_amt)/10000 , 2) ||'万' as 毛利金额
    FROM (
    SELECT to_char(trans_time,'yyyymmdd') trans_time,
    1 ct,
    trans_amt,
    0 profits_amt
    FROM trans_offline_order -- 线下订单表
    where trans_cd = '00'
    and trans_time >= to_TIMESTAMP('20240101', 'yyyyMMdd')
    and trans_time < to_TIMESTAMP('20241231', 'yyyyMMdd')
    union all
    SELECT to_char(trans_time,'yyyymmdd') trans_time,
    1 ct,
    trans_amt,
    0 profits_amt
    FROM trans_online_order -- 线上订单表
    WHERE trans_type IN ('01', '02')
    and trans_cd = '00'
    and trans_time >= to_TIMESTAMP('20240101', 'yyyyMMdd')
    and trans_time < to_TIMESTAMP('20241231', 'yyyyMMdd')
    union all
    SELECT to_char(trans_time,'yyyymmdd') trans_time,
    0 ct,
    0 trans_amt,
    profits_amt
    FROM trans_profits -- 交易毛利表
    where trans_time >= to_date('20240101', 'yyyyMMdd')
    and trans_time < to_date('20241231', 'yyyyMMdd')
    ) t
    GROUP BY substr(t.trans_time,0,6)
    ORDER BY 1 ;

执行计划对比
  • Statistics 资源消耗 相同;

  • | Rows | Bytes | Cost (%CPU)| Time | 这几项明显"变装"后更优于原SQL写法,原SQL写法甚至还用到了TempSpc的耗;

  • 执行时间"变装"后慢了10+ms但影响不大;
    -- 注(疑惑):明明从执行计划来分析"变装"后的SQL更优,为啥会变慢了呢?

总结

SQL在其它部门的作用是以实现需求为主,但在DBA手里需要考虑在不改变需求结果的前提下,要让SQL更具有可读性及良好的性能。

相关推荐
Elastic 中国社区官方博客7 小时前
使用 Groq 与 Elasticsearch 进行智能查询
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
穿过锁扣的风7 小时前
一文搞懂 SQL 五大分类:DQL/DML/DDL/DCL/TCL
数据库·microsoft·oracle
l1t7 小时前
DeepSeek总结的SNKV — 无查询处理器的 SQLite 键值存储
数据库·sqlite·kvstore
洛豳枭薰7 小时前
MySQL 梳理
数据库·mysql
九.九8 小时前
CANN 算子生态的底层安全与驱动依赖:固件校验与算子安全边界的强化
大数据·数据库·安全
蓝帆傲亦8 小时前
代码革命!我用Claude Code 3个月完成1年工作量,这些实战经验全给你
jvm·数据库·oracle
亓才孓8 小时前
[JDBC]事务
java·开发语言·数据库
PD我是你的真爱粉8 小时前
FastAPI使用tortoiseORM
数据库·fastapi
剩下了什么15 小时前
MySQL JSON_SET() 函数
数据库·mysql·json
山峰哥16 小时前
数据库工程与SQL调优——从索引策略到查询优化的深度实践
数据库·sql·性能优化·编辑器