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 中国社区官方博客11 小时前
如何比较两个 Elasticsearch 索引并找出缺失的文档
大数据·运维·数据库·elasticsearch·搜索引擎
DLYSB_11 小时前
生命通道:如何用 HIS 医疗系统直连网络声光终端,打造“零延误”的危急值响应网关?
java·网络·数据库·报警灯
儒雅的大叔11 小时前
MySQL数据库InnoDB数据恢复工具使用总结
数据库·mysql·adb
观远数据11 小时前
ChatBI选型对比:从意图识别到SQL修复,六个维度打分决定是否值得投产
数据库·人工智能·sql
嘉&年华11 小时前
【第008篇】通过dexp和dimp命令导出和导入dmp文件(适用于达梦数据库)
数据库·sql
哥是强混13 小时前
Means:基于 .NET 10 打造的开源自部署 S3 兼容对象存储服务
网络·数据库·.net
ffqws_13 小时前
redis面试:如何保证双写一致
数据库·redis·缓存
水无痕simon13 小时前
6 数据库同义词
数据库
杜子不疼.14 小时前
【Qt初识】信号槽(三):机制意义、断开连接与 Lambda 表达式
开发语言·数据库·qt
BomanGe115 小时前
NSK W1501FA高速精密滚珠丝杠技术手册
数据库·经验分享·规格说明书