我这语句还能优化吗。。

whx20202 2014-10-24 01:01:51

SELECT t1.c c1, t2.c c2, t1.c/t2.c rate, t2.pl, t2.`day` date FROM
(SELECT sum(row_num) c, product_line pl, `day`
FROM nt_stat_num_product_line
WHERE `day`>='20140921' AND `day`<='20141021' AND `name`='SNOOPY_HOST_COUNT'
GROUP BY product_line, `day`) t1
JOIN
(SELECT sum(row_num) c, product_line pl, `day`
FROM nt_stat_num_product_line
WHERE `day`>='20140921' AND `day`<='20141021' AND `name`='ALL_SNOOPY_HOST_COUNT'
GROUP BY product_line, `day`) t2
ON t1.pl=t2.pl AND t1.`day`=t2.`day`
ORDER BY date DESC, rate ASC;


这张表也就几万数据量,两个子查询结果都是几千个,子查询非常快 0.04s
(我甚至可以为两个子查询建立非常针对性的 name, product_line,day复合索引)

问题关键是两个子查询都用了 sum 导致两个数据量为几千的结果集 join 总的查询3s 太慢了
这玩意如何优化,我已经想破脑袋了,本身对数据库非常感兴趣。
...全文
194 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
whx20202 2014-10-24
  • 打赏
  • 举报
回复
引用 4 楼 zy205817 的回复:
单个子查询的运行结果贴出来瞧瞧
SELECT sum(row_num) c, product_line pl, `day`
                FROM nt_stat_num_product_line
                WHERE `day`>='20140921' AND `day`<='20141021' AND `name`='SNOOPY_HOST_COUNT'
                GROUP BY product_line, `day`
结果几千行,随便贴几行: +------+---------------------------------+----------+ | c | pl | day | +------+---------------------------------+----------+ | 633 | xx部门 | 20141001 | | 633 | xx部门 | 20141002 | | 633 | xx部门 | 20141003 | | 633 | xx部门 | 20141004 | | 633 | xx部门 | 20141005 | +------+---------------------------------+----------+ 执行计划(这里没针对group by 加组合索引 但是子查询本身已经够快了) *************************** 1. row *************************** id: 1 select_type: SIMPLE table: nt_stat_num_product_line type: ref possible_keys: idx_nt_stat_name,idx_nt_stat_day key: idx_nt_stat_name key_len: 195 ref: const rows: 2783 Extra: Using where; Using temporary; Using filesort
码无边 2014-10-24
  • 打赏
  • 举报
回复
单个子查询的运行结果贴出来瞧瞧
whx20202 2014-10-24
  • 打赏
  • 举报
回复
问题在于 using temporary 那里面不做group by a ,b,c 直接返回所有结果集给上一层 上一次层 distinct a,b,c即可 (我这样是有前提的: 结果集不group by的时候 本身不大 ,但是你在内层group by 到别的表字段的话,会有Using temporary; Using filesort 这种情就可以像我这样,拿出来group by 这样就是在临时内存里做了? 我猜想如果连接的时候group by 他是跨表做的? )
whx20202 2014-10-24
  • 打赏
  • 举报
回复
明白了 已经优化
whx20202 2014-10-24
  • 打赏
  • 举报
回复
*************************** 1. row *************************** id: 1 select_type: PRIMARY table: <derived2> type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 3796 Extra: Using temporary; Using filesort *************************** 2. row *************************** id: 1 select_type: PRIMARY table: <derived3> type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 4625 Extra: Using where *************************** 3. row *************************** id: 3 select_type: DERIVED table: nt_stat_num_product_line type: ref possible_keys: idx_nt_stat_name,idx_nt_stat_day key: idx_nt_stat_name key_len: 195 ref: rows: 5746 Extra: Using where; Using temporary; Using filesort *************************** 4. row *************************** id: 2 select_type: DERIVED table: nt_stat_num_product_line type: ref possible_keys: idx_nt_stat_name,idx_nt_stat_day key: idx_nt_stat_name key_len: 195 ref: rows: 4943 Extra: Using where; Using temporary; Using filesort
whx20202 2014-10-24
  • 打赏
  • 举报
回复
+----+-------------+--------------------------+------+----------------------------------+------------------+---------+------+------+----------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+--------------------------+------+----------------------------------+------------------+---------+------+------+----------------------------------------------+ | 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 3796 | Using temporary; Using filesort | | 1 | PRIMARY | <derived3> | ALL | NULL | NULL | NULL | NULL | 4625 | Using where | | 3 | DERIVED | nt_stat_num_product_line | ref | idx_nt_stat_name,idx_nt_stat_day | idx_nt_stat_name | 195 | | 5746 | Using where; Using temporary; Using filesort | | 2 | DERIVED | nt_stat_num_product_line | ref | idx_nt_stat_name,idx_nt_stat_day | idx_nt_stat_name | 195 | | 4943 | Using where; Using temporary; Using filesort | +----+-------------+--------------------------+------+----------------------------------+------------------+---------+------+------+----------------------------------------------+
whx20202 2014-10-24
  • 打赏
  • 举报
回复
还有数据比较慢 不是因为最外层用了一次除法 t1.c/t2.c rate 我把这一项删了还是慢

56,687

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧