原始SQL:

这里想做的事情就是查询一周的一个计算值

可以理解为报表的那种

主表 t_wechat_clue 生产库上200万数据量

然后需要联表一些限制条件

SELECT
IFNULL(SUM((CASE WHEN CLUE.gen_date = DATE_ADD(CURRENT_DATE,INTERVAL -6 DAY) AND HOUR(CLUE.gen_time) = 22 THEN CLUE.follow_total ELSE 0 END)),0) AS y1,
IFNULL(SUM((CASE WHEN CLUE.gen_date = DATE_ADD(CURRENT_DATE,INTERVAL -5 DAY) AND HOUR(CLUE.gen_time) = 22 THEN CLUE.follow_total ELSE 0 END)),0) AS y2,
IFNULL(SUM((CASE WHEN CLUE.gen_date = DATE_ADD(CURRENT_DATE,INTERVAL -4 DAY) AND HOUR(CLUE.gen_time) = 22 THEN CLUE.follow_total ELSE 0 END)),0) AS y3,
IFNULL(SUM((CASE WHEN CLUE.gen_date = DATE_ADD(CURRENT_DATE,INTERVAL -3 DAY) AND HOUR(CLUE.gen_time) = 22 THEN CLUE.follow_total ELSE 0 END)),0) AS y4,
IFNULL(SUM((CASE WHEN CLUE.gen_date = DATE_ADD(CURRENT_DATE,INTERVAL -2 DAY) AND HOUR(CLUE.gen_time) = 22 THEN CLUE.follow_total ELSE 0 END)),0) AS y5,
IFNULL(SUM((CASE WHEN CLUE.gen_date = DATE_ADD(CURRENT_DATE,INTERVAL -1 DAY) AND HOUR(CLUE.gen_time) = 22 THEN CLUE.follow_total ELSE 0 END)),0) AS y6,
IFNULL(SUM((CASE WHEN CLUE.gen_date = DATE_ADD(CURRENT_DATE,INTERVAL 0 DAY) AND HOUR(CLUE.gen_time) > HOUR(DATE_ADD(NOW(), INTERVAL -2 HOUR)) THEN CLUE.follow_total ELSE 0 END)),0) AS y7, IFNULL(SUM(DISTINCT CASE WHEN CLUE.org_code IS NOT NULL THEN (CASE WHEN CLUE.gen_date = DATE_ADD(CURRENT_DATE,INTERVAL -6 DAY) AND HOUR(CLUE.gen_time) = 22 THEN CLUE.dist_total ELSE 0 END) ELSE 0 END), 0) AS a1,
IFNULL(SUM(DISTINCT CASE WHEN CLUE.org_code IS NOT NULL THEN (CASE WHEN CLUE.gen_date = DATE_ADD(CURRENT_DATE,INTERVAL -5 DAY) AND HOUR(CLUE.gen_time) = 22 THEN CLUE.dist_total ELSE 0 END) ELSE 0 END), 0) AS a2,
IFNULL(SUM(DISTINCT CASE WHEN CLUE.org_code IS NOT NULL THEN (CASE WHEN CLUE.gen_date = DATE_ADD(CURRENT_DATE,INTERVAL -4 DAY) AND HOUR(CLUE.gen_time) = 22 THEN CLUE.dist_total ELSE 0 END) ELSE 0 END), 0) AS a3,
IFNULL(SUM(DISTINCT CASE WHEN CLUE.org_code IS NOT NULL THEN (CASE WHEN CLUE.gen_date = DATE_ADD(CURRENT_DATE,INTERVAL -3 DAY) AND HOUR(CLUE.gen_time) = 22 THEN CLUE.dist_total ELSE 0 END) ELSE 0 END), 0) AS a4,
IFNULL(SUM(DISTINCT CASE WHEN CLUE.org_code IS NOT NULL THEN (CASE WHEN CLUE.gen_date = DATE_ADD(CURRENT_DATE,INTERVAL -2 DAY) AND HOUR(CLUE.gen_time) = 22 THEN CLUE.dist_total ELSE 0 END) ELSE 0 END), 0) AS a5,
IFNULL(SUM(DISTINCT CASE WHEN CLUE.org_code IS NOT NULL THEN (CASE WHEN CLUE.gen_date = DATE_ADD(CURRENT_DATE,INTERVAL -1 DAY) AND HOUR(CLUE.gen_time) = 22 THEN CLUE.dist_total ELSE 0 END) ELSE 0 END), 0) AS a6,
IFNULL(SUM(DISTINCT CASE WHEN CLUE.org_code IS NOT NULL THEN (CASE WHEN CLUE.gen_date = DATE_ADD(CURRENT_DATE,INTERVAL 0 DAY) AND HOUR(CLUE.gen_time) > HOUR(DATE_ADD(NOW(), INTERVAL -2 HOUR)) THEN CLUE.dist_total ELSE 0 END) ELSE 0 END), 0) AS a7
FROM
t_wechat_clues CLUE
JOIN t_dms_member DMS_MEMBER ON CLUE.emp_id = DMS_MEMBER.employee_id AND LENGTH(CLUE.org_code)=8 AND SUBSTR(CLUE.org_code, 1, 1)='P'
JOIN t_wechat_member WC_MEMBER ON DMS_MEMBER.wechat_emp_id = WC_MEMBER.userid AND WC_MEMBER.STATUS = 1 AND WC_MEMBER.TYPE = 2
JOIN (SELECT TT.id ,TT.NAME FROM (
WITH RECURSIVE cte AS(
SELECT a.id, a.parent_id,a.name FROM tt_wechat_org a WHERE a.id='49'
UNION ALL
SELECT k.id, k.parent_id,k.name FROM tt_wechat_org k INNER JOIN cte c ON c.id = k.parent_id
) SELECT id,NAME,parent_id FROM cte) TT ) TREE_ORG ON TREE_ORG.id = WC_MEMBER.department

反正实际上数据返回到页面渲染Echarts花了10秒的样子

经理说8行,必须要改动

前提情况是,这里联表的字段和日期时间都做了索引处理

慢的原因是查询的字段导致

另外还有就是联表的字段数据类型需要一致

不要让MySQL去推导转换,这会浪费一部分性能

优化之后:

随后同事给我一套UnionALL解法,虽然SQL很多,但是效率提升明显!

SELECT
IFNULL(SUM(CLUE.follow_total),0) AS y1 ,
SUM(DISTINCT (CASE WHEN CLUE.org_code IS NOT NULL THEN CLUE.dist_total ELSE 0 END)) a1
FROM
t_wechat_clues CLUE
JOIN t_dms_member DMS_MEMBER ON CLUE.emp_id = DMS_MEMBER.employee_id AND LENGTH(CLUE.org_code)=8 AND SUBSTR(CLUE.org_code, 1, 1)='P'
JOIN t_wechat_member WC_MEMBER ON DMS_MEMBER.wechat_emp_id = WC_MEMBER.userid AND WC_MEMBER.STATUS = 1 AND WC_MEMBER.TYPE = 2
JOIN (SELECT TT.id ,TT.NAME FROM (
WITH RECURSIVE cte AS(
SELECT a.id, a.parent_id,a.name FROM tt_wechat_org a WHERE a.id='49'
UNION ALL
SELECT k.id, k.parent_id,k.name FROM tt_wechat_org k INNER JOIN cte c ON c.id = k.parent_id
) SELECT id,NAME,parent_id FROM cte) TT ) TREE_ORG ON TREE_ORG.id = WC_MEMBER.department
WHERE CLUE.gen_date = DATE_ADD(CURRENT_DATE,INTERVAL -6 DAY) AND HOUR(CLUE.gen_time) = 22
UNION ALL SELECT
IFNULL(SUM(CLUE.follow_total),0) AS y1 ,
SUM(DISTINCT (CASE WHEN CLUE.org_code IS NOT NULL THEN CLUE.dist_total ELSE 0 END)) a1
FROM
t_wechat_clues CLUE
JOIN t_dms_member DMS_MEMBER ON CLUE.emp_id = DMS_MEMBER.employee_id AND LENGTH(CLUE.org_code)=8 AND SUBSTR(CLUE.org_code, 1, 1)='P'
JOIN t_wechat_member WC_MEMBER ON DMS_MEMBER.wechat_emp_id = WC_MEMBER.userid AND WC_MEMBER.STATUS = 1 AND WC_MEMBER.TYPE = 2
JOIN (SELECT TT.id ,TT.NAME FROM (
WITH RECURSIVE cte AS(
SELECT a.id, a.parent_id,a.name FROM tt_wechat_org a WHERE a.id='49'
UNION ALL
SELECT k.id, k.parent_id,k.name FROM tt_wechat_org k INNER JOIN cte c ON c.id = k.parent_id
) SELECT id,NAME,parent_id FROM cte) TT ) TREE_ORG ON TREE_ORG.id = WC_MEMBER.department
WHERE CLUE.gen_date = DATE_ADD(CURRENT_DATE,INTERVAL -5 DAY) AND HOUR(CLUE.gen_time) = 22 UNION ALL SELECT
IFNULL(SUM(CLUE.follow_total),0) AS y1 ,
SUM(DISTINCT (CASE WHEN CLUE.org_code IS NOT NULL THEN CLUE.dist_total ELSE 0 END)) a1
FROM
t_wechat_clues CLUE
JOIN t_dms_member DMS_MEMBER ON CLUE.emp_id = DMS_MEMBER.employee_id AND LENGTH(CLUE.org_code)=8 AND SUBSTR(CLUE.org_code, 1, 1)='P'
JOIN t_wechat_member WC_MEMBER ON DMS_MEMBER.wechat_emp_id = WC_MEMBER.userid AND WC_MEMBER.STATUS = 1 AND WC_MEMBER.TYPE = 2
JOIN (SELECT TT.id ,TT.NAME FROM (
WITH RECURSIVE cte AS(
SELECT a.id, a.parent_id,a.name FROM tt_wechat_org a WHERE a.id='49'
UNION ALL
SELECT k.id, k.parent_id,k.name FROM tt_wechat_org k INNER JOIN cte c ON c.id = k.parent_id
) SELECT id,NAME,parent_id FROM cte) TT ) TREE_ORG ON TREE_ORG.id = WC_MEMBER.department
WHERE CLUE.gen_date = DATE_ADD(CURRENT_DATE,INTERVAL -4 DAY) AND HOUR(CLUE.gen_time) = 22 UNION ALL SELECT
IFNULL(SUM(CLUE.follow_total),0) AS y1 ,
SUM(DISTINCT (CASE WHEN CLUE.org_code IS NOT NULL THEN CLUE.dist_total ELSE 0 END)) a1
FROM
t_wechat_clues CLUE
JOIN t_dms_member DMS_MEMBER ON CLUE.emp_id = DMS_MEMBER.employee_id AND LENGTH(CLUE.org_code)=8 AND SUBSTR(CLUE.org_code, 1, 1)='P'
JOIN t_wechat_member WC_MEMBER ON DMS_MEMBER.wechat_emp_id = WC_MEMBER.userid AND WC_MEMBER.STATUS = 1 AND WC_MEMBER.TYPE = 2
JOIN (SELECT TT.id ,TT.NAME FROM (
WITH RECURSIVE cte AS(
SELECT a.id, a.parent_id,a.name FROM tt_wechat_org a WHERE a.id='49'
UNION ALL
SELECT k.id, k.parent_id,k.name FROM tt_wechat_org k INNER JOIN cte c ON c.id = k.parent_id
) SELECT id,NAME,parent_id FROM cte) TT ) TREE_ORG ON TREE_ORG.id = WC_MEMBER.department
WHERE CLUE.gen_date = DATE_ADD(CURRENT_DATE,INTERVAL -3 DAY) AND HOUR(CLUE.gen_time) = 22 UNION ALL SELECT
IFNULL(SUM(CLUE.follow_total),0) AS y1 ,
SUM(DISTINCT (CASE WHEN CLUE.org_code IS NOT NULL THEN CLUE.dist_total ELSE 0 END)) a1
FROM
t_wechat_clues CLUE
JOIN t_dms_member DMS_MEMBER ON CLUE.emp_id = DMS_MEMBER.employee_id AND LENGTH(CLUE.org_code)=8 AND SUBSTR(CLUE.org_code, 1, 1)='P'
JOIN t_wechat_member WC_MEMBER ON DMS_MEMBER.wechat_emp_id = WC_MEMBER.userid AND WC_MEMBER.STATUS = 1 AND WC_MEMBER.TYPE = 2
JOIN (SELECT TT.id ,TT.NAME FROM (
WITH RECURSIVE cte AS(
SELECT a.id, a.parent_id,a.name FROM tt_wechat_org a WHERE a.id='49'
UNION ALL
SELECT k.id, k.parent_id,k.name FROM tt_wechat_org k INNER JOIN cte c ON c.id = k.parent_id
) SELECT id,NAME,parent_id FROM cte) TT ) TREE_ORG ON TREE_ORG.id = WC_MEMBER.department
WHERE CLUE.gen_date = DATE_ADD(CURRENT_DATE,INTERVAL -2 DAY) AND HOUR(CLUE.gen_time) = 22 UNION ALL SELECT
IFNULL(SUM(CLUE.follow_total),0) AS y1 ,
SUM(DISTINCT (CASE WHEN CLUE.org_code IS NOT NULL THEN CLUE.dist_total ELSE 0 END)) a1
FROM
t_wechat_clues CLUE
JOIN t_dms_member DMS_MEMBER ON CLUE.emp_id = DMS_MEMBER.employee_id AND LENGTH(CLUE.org_code)=8 AND SUBSTR(CLUE.org_code, 1, 1)='P'
JOIN t_wechat_member WC_MEMBER ON DMS_MEMBER.wechat_emp_id = WC_MEMBER.userid AND WC_MEMBER.STATUS = 1 AND WC_MEMBER.TYPE = 2
JOIN (SELECT TT.id ,TT.NAME FROM (
WITH RECURSIVE cte AS(
SELECT a.id, a.parent_id,a.name FROM tt_wechat_org a WHERE a.id='49'
UNION ALL
SELECT k.id, k.parent_id,k.name FROM tt_wechat_org k INNER JOIN cte c ON c.id = k.parent_id
) SELECT id,NAME,parent_id FROM cte) TT ) TREE_ORG ON TREE_ORG.id = WC_MEMBER.department
WHERE CLUE.gen_date = DATE_ADD(CURRENT_DATE,INTERVAL -1 DAY) AND HOUR(CLUE.gen_time) = 22 UNION ALL SELECT
IFNULL(SUM(CLUE.follow_total),0) AS y1 ,
SUM(DISTINCT (CASE WHEN CLUE.org_code IS NOT NULL THEN CLUE.dist_total ELSE 0 END)) a1
FROM
t_wechat_clues CLUE
JOIN t_dms_member DMS_MEMBER ON CLUE.emp_id = DMS_MEMBER.employee_id AND LENGTH(CLUE.org_code)=8 AND SUBSTR(CLUE.org_code, 1, 1)='P'
JOIN t_wechat_member WC_MEMBER ON DMS_MEMBER.wechat_emp_id = WC_MEMBER.userid AND WC_MEMBER.STATUS = 1 AND WC_MEMBER.TYPE = 2
JOIN (SELECT TT.id ,TT.NAME FROM (
WITH RECURSIVE cte AS(
SELECT a.id, a.parent_id,a.name FROM tt_wechat_org a WHERE a.id='49'
UNION ALL
SELECT k.id, k.parent_id,k.name FROM tt_wechat_org k INNER JOIN cte c ON c.id = k.parent_id
) SELECT id,NAME,parent_id FROM cte) TT ) TREE_ORG ON TREE_ORG.id = WC_MEMBER.department
WHERE CLUE.gen_date = DATE_ADD(CURRENT_DATE,INTERVAL -0 DAY) AND HOUR(CLUE.gen_time) > HOUR(DATE_ADD(NOW(), INTERVAL -2 HOUR))

生产库执行结果是909ms 1秒差点就查完了

【DataBase】SQL优化案例:其一的更多相关文章

  1. SQL优化案例—— RowNumber分页

    将业务语句翻译成SQL语句不仅是一门技术,还是一门艺术. 下面拿我们程序开发工程师最常用的ROW_NUMBER()分页作为一个典型案例来说明. 先来看看我们最常见的分页的样子: WITH CTE AS ...

  2. mysql的sql优化案例

    前言 mysql的sql优化器比较弱,选择执行计划貌似很随机. 案例 一.表结构说明mysql> show create table table_order\G***************** ...

  3. SQL 优化案例 1

    create or replace procedure SP_GET_NEWEST_CAPTCHA( v_ACCOUNT_ID in VARCHAR2, --接收短信的手机号 v_Tail_num i ...

  4. SQL 优化案例

    create or replace procedure SP_GET_NEWEST_CAPTCHA( v_ACCOUNT_ID in VARCHAR2, --接收短信的手机号 v_Tail_num i ...

  5. SQL夯实基础(四):子查询及sql优化案例

    首先我们先明确一下sql语句的执行顺序,如下有前至后执行: (1)from  (2) on   (3) join  (4) where  (5)group by  (6) avg,sum...  (7 ...

  6. 百倍性能的PL/SQL优化案例(r11笔记第13天)

    我相信你是被百倍性能的字样吸引了,不过我所想侧重的是优化的思路,这个比优化技巧更重要,而结果嘛,其实我不希望说成是百倍提升,“”自黑“”一下. 有一个真实想法和大家讨论一下,就是一个SQL语句如果原本 ...

  7. 数栈SQL优化案例:隐式转换

    MySQL是当下最流行的关系型数据库之一,互联网高速发展的今天,MySQL数据库在电商.金融等诸多行业的生产系统中被广泛使用. 在实际的开发运维过程中,想必大家也常常会碰到慢SQL的困扰.一条性能不好 ...

  8. sqlserver sql优化案例及思路

    始sql: SELECT TOP 100 PERCENT ZZ.CREW_NAME AS 机组, ZZ.CREW_ID, AA.年度时间, CC.当月时间, DD.连续七天时间 AS 最近七天 FRO ...

  9. SQL 优化案例之变更表结构

    从慢日志报表中看到一条很长的SQL select id from myinfo and (( SUBSTRING_INDEX(location_axis, ) ) ) ), '$%') ) or ( ...

  10. 一则SQL优化案例

    原始sql: select CASE ) counts ,) else deadline end as deadline from t_product_credit) c group by sort ...

随机推荐

  1. three.js高性能渲染室外场景

    大家好,本文在相关文章的基础上,使用three.js渲染了高性能的室外场景,在移动端也有较好的性能,并给出了代码,分析了关键点,感谢大家~ 关键词:three.js.Web3D.WebGL.室外场景. ...

  2. 透过 node-exporter 彻底弄懂机器监控:01. node-exporter 框架讲解

    前言 Prometheus 生态里有很多采集器负责各类监控数据的采集,其中使用最广泛的,显然是 node-exporter,负责 Linux.BSD 等系统的常规监控指标的采集,比如 CPU.内存.硬 ...

  3. 使用spark-sql处理Doris大表关联

    背景 最近项目上有一个需求,需要将两张表(A表和B表)的数据进行关联并回写入其中一张表(A表),两张表都是分区表,但是关联条件不包括分区字段. 分析过程 方案一 最朴素的想法,直接关联执行,全表关联, ...

  4. springboot之日志配置-logback

    springboot之日志配置-logback 1.为什么使用logback logback是springboot默认集成的,是基于Sl4J的日志框架. logback的内核重写了,使得在某些关键路径 ...

  5. Angular 集成 StreamSaver

    应用场景: 实现目标: 在网页端实现大文件(文件大小 >= 2 G) 断点续传 实际方案: 发送多次请求, 每次请求一部分文件数据, 然后通过续写将文件数据全部写入. 难点: 无法实现文件续写, ...

  6. es6.6.1 索引的增加,查询,修改,删除

    1.新增 test2/user2/1/_create PUT操作{"name":"qiqi","age":17} 2.查询 test2/us ...

  7. CNN --入门MNIST识别

    Smiling & Weeping ---- 下次你撑伞低头看水洼, 就会想起我说雨是神的烟花. 简介:主要是看刘二大人的视频讲解:https://www.bilibili.com/video ...

  8. java的ConCurrentHashMap

    一般的应用的编程,用到ConCurrentHashMap的机会很少,就象大家调侃的一样:只有面试的时候才用得着. 但还是有. 网上关于这个的资料,多如牛毛,大部分是原理分析和简单例子. 原理的核心就一 ...

  9. 李沐多模态串讲视频总结 ALBEF VLMo BLIP CoCa BEITv3 模型简要介绍

    开场 多模态串讲的上篇是比较传统的多模态任务 多模态最后的模态交互很重要 传统的缺点是都用了预训练的目标检测器,训练和部署都很困难. ViLT 把预训练的目标检测器换成了一层的 Patch Embed ...

  10. python重拾第七天-面向对象进阶

    本节内容: 面向对象高级语法部分 经典类vs新式类 静态方法.类方法.属性方法 类的特殊方法 反射 异常处理 Socket开发基础 作业:开发一个支持多用户在线的FTP程序 面向对象高级语法部分 经典 ...