这段 SQL 大佬能给点优化建议吗?统计一个月每天的数据还有更加简洁的写法吗?目前运行速度太慢了

2019-09-25 10:35:52 +08:00
 galaChe

SELECT su.real_name AS realName, sd.name AS deptName, su.user_name AS userName, ahh.* FROM ( SELECT ROUND( SUM( IF ( DAY ( ah.audit_time ) = '1', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '1', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '2', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '2', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '3', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '3', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '4', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '4', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '5', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '5', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '6', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '6', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '7', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '7', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '8', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '8', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '9', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '9', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '10', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '10', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '11', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '11', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '12', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '12', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '13', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '13', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '14', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '14', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '15', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '15', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '16', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '16', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '17', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '17', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '18', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '18', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '19', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '19', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '20', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '20', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '21', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '21', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '22', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '22', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '23', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '23', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '24', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '24', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '25', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '25', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '26', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '26', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '27', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '27', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '28', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '28', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '29', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '29', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '30', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '30', ROUND( SUM( IF ( DAY ( ah.audit_time ) = '31', ah.audit_num * ifnull( kpi.kpi_value, 1 ), NULL ) ), 2 ) AS '31', SUM( ( ROUND( ( ah.audit_num * ifnull( kpi.kpi_value, 1 ) ), 2 ) ) ) AS auditCount, ah.account_number AS empNumber, ah.emp_name AS employeeName, SUM( ROUND( ( ah.pass_num * ifnull( kpi.kpi_value, 1 ) ), 2 ) ) AS passCount, SUM( ROUND( ( ah.delete_Num * ifnull( kpi.kpi_value, 1 ) ), 2 ) ) AS delCount FROM audit_days AS ah LEFT JOIN skill_group_kpi AS kpi ON ( ah.group_name = kpi.group_name ) AND kpi.is_deleted = 0 WHERE 1 = 1 GROUP BY ah.account_number ORDER BY NULL ) ahh LEFT JOIN sys_user su ON su.qq LIKE concat( '%', ahh.empNumber, '%' ) AND ahh.empNumber != 0 LEFT JOIN sys_department sd ON sd.id = su.department_id

9296 次点击
所在节点    MySQL
74 条回复
polymerdg
2019-09-25 10:38:13 +08:00
这一坨 看着好难受
dovme
2019-09-25 10:40:52 +08:00
格式化一下,然后截图发图片,你这个看都不想看
sethverlo
2019-09-25 10:41:27 +08:00
没排版,没 explain, 甚至都没个业务逻辑说明…这咋看…
liprais
2019-09-25 10:44:57 +08:00
坐等楼下说把数据读出来在应用里手写逻辑处理
arrow8899
2019-09-25 10:51:31 +08:00
把数据读出来在应用里手写逻辑处理
leexy
2019-09-25 10:51:58 +08:00
把数据读出来在应用里手写逻辑处理
woodensail
2019-09-25 10:52:12 +08:00
把数据读出来在应用里手写逻辑处理
zzz686970
2019-09-25 10:52:17 +08:00
有一点是为什么没有每一天 date 的字段,在这个基础上进行 left join 和 group by 不是更简单快捷么
a2532521
2019-09-25 11:04:28 +08:00
喷了..穷举
junan008
2019-09-25 11:08:56 +08:00
新增统计表,每天统计啊。。
CallMeReznov
2019-09-25 11:11:00 +08:00
要统计每天的数据,肯定是每天统计然后单独出表,你这个.......
ZXCDFGTYU
2019-09-25 11:11:13 +08:00
这条 SQL 惊为天人,骨骼惊奇,秀的我头皮发麻
ZXCDFGTYU
2019-09-25 11:11:26 +08:00
程序员写个代码不容易,放过孩子吧
claysec
2019-09-25 11:13:33 +08:00
我的天。。当初是怎么撸出来的
sunziren
2019-09-25 11:17:12 +08:00
@a2532521 :doge
fuchaofather
2019-09-25 11:22:22 +08:00
吐了, 这谁能看. 不变更现有逻辑, 改离线模式呢?
wps353
2019-09-25 11:23:29 +08:00
AP 业务?
nnnToTnnn
2019-09-25 11:26:22 +08:00
```
SELECT su.real_name AS realName, sd.name AS deptName, su.user_name AS userName, ahh.*
FROM (
SELECT
ROUND(SUM(IF(DAY(ah.audit_time) = '1', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '1'
, ROUND(SUM(IF(DAY(ah.audit_time) = '2', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '2'
, ROUND(SUM(IF(DAY(ah.audit_time) = '3', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '3'
, ROUND(SUM(IF(DAY(ah.audit_time) = '4', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '4'
, ROUND(SUM(IF(DAY(ah.audit_time) = '5', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '5'
, ROUND(SUM(IF(DAY(ah.audit_time) = '6', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '6'
, ROUND(SUM(IF(DAY(ah.audit_time) = '7', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '7'
, ROUND(SUM(IF(DAY(ah.audit_time) = '8', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '8'
, ROUND(SUM(IF(DAY(ah.audit_time) = '9', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '9'
, ROUND(SUM(IF(DAY(ah.audit_time) = '10', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '10'
, ROUND(SUM(IF(DAY(ah.audit_time) = '11', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '11'
, ROUND(SUM(IF(DAY(ah.audit_time) = '12', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '12'
, ROUND(SUM(IF(DAY(ah.audit_time) = '13', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '13'
, ROUND(SUM(IF(DAY(ah.audit_time) = '14', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '14'
, ROUND(SUM(IF(DAY(ah.audit_time) = '15', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '15'
, ROUND(SUM(IF(DAY(ah.audit_time) = '16', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '16'
, ROUND(SUM(IF(DAY(ah.audit_time) = '17', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '17'
, ROUND(SUM(IF(DAY(ah.audit_time) = '18', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '18'
, ROUND(SUM(IF(DAY(ah.audit_time) = '19', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '19'
, ROUND(SUM(IF(DAY(ah.audit_time) = '20', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '20'
, ROUND(SUM(IF(DAY(ah.audit_time) = '21', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '21'
, ROUND(SUM(IF(DAY(ah.audit_time) = '22', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '22'
, ROUND(SUM(IF(DAY(ah.audit_time) = '23', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '23'
, ROUND(SUM(IF(DAY(ah.audit_time) = '24', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '24'
, ROUND(SUM(IF(DAY(ah.audit_time) = '25', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '25'
, ROUND(SUM(IF(DAY(ah.audit_time) = '26', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '26'
, ROUND(SUM(IF(DAY(ah.audit_time) = '27', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '27'
, ROUND(SUM(IF(DAY(ah.audit_time) = '28', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '28'
, ROUND(SUM(IF(DAY(ah.audit_time) = '29', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '29'
, ROUND(SUM(IF(DAY(ah.audit_time) = '30', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '30'
, ROUND(SUM(IF(DAY(ah.audit_time) = '31', ah.audit_num * ifnull(kpi.kpi_value, 1), NULL)), 2) AS '31'
, SUM(ROUND(ah.audit_num * ifnull(kpi.kpi_value, 1), 2)) AS auditCount
, ah.account_number AS empNumber, ah.emp_name AS employeeName
, SUM(ROUND(ah.pass_num * ifnull(kpi.kpi_value, 1), 2)) AS passCount
, SUM(ROUND(ah.delete_Num * ifnull(kpi.kpi_value, 1), 2)) AS delCount
FROM audit_days ah
LEFT JOIN skill_group_kpi kpi
ON ah.group_name = kpi.group_name
AND kpi.is_deleted = 0
WHERE 1 = 1
GROUP BY ah.account_number
ORDER BY NULL
) ahh
LEFT JOIN sys_user su
ON su.qq LIKE concat('%', ahh.empNumber, '%')
AND ahh.empNumber != 0
LEFT JOIN sys_department sd ON sd.id = su.department_id

```

我看了你的 SQL,特么有个问题想问下你


```
ON su.qq LIKE concat('%', ahh.empNumber, '%')
```

关联表的时候不应该强关联么? 怎么还有一个 on like? 哪个大神能解释一下?


其次

自己跑下解析计划,就不就知道哪里慢了? 我这里有没有数据,光看 SQL 估计有点难,目前就看出来了一个 on like 很叼,叼爆了
HansCathy
2019-09-25 11:27:15 +08:00
在内存里面处理啊
0NF09LJPS51k57uH
2019-09-25 11:27:40 +08:00
能否每天做一次 snapshot ?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/603956

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX