減少不必要的group by欄位

來源:互聯網
上載者:User

後台某維度統計語句
SELECT products_id, sku, px_id, sj_id, cat_path, COUNT(*) AS pv, COUNT(DISTINCT ip) AS ip_numbers, SUM(is_bounce) AS bounce_numbers, SUM(remain_time) AS remain_time
    FROM dm_pv_records_search
    WHERE 1 AND add_date >= '2014-02-26 10:00:00' AND products_id > 0
GROUP BY products_id,sku, px_id, sj_id, cat_path
order by ip_numbers desc limit  0,20;

每次有同事在後台查詢這類資料時,都反應資料非常慢,偶爾不能出來資料。

explain
+----+-------------+----------------------+-------+----------------------+----------+---------+------+---------+----------------------------------------------+
| id | select_type | table                | type  | possible_keys        | key      | key_len | ref  | rows    | Extra                                        |
+----+-------------+----------------------+-------+----------------------+----------+---------+------+---------+----------------------------------------------+
|  1 | SIMPLE      | dm_pv_records_search | range | products_id,add_date | add_date | 8      | NULL | 12831019 | Using where; Using temporary; Using filesort |
+----+-------------+----------------------+-------+----------------------+----------+---------+------+---------+----------------------------------------------+

這條語句索引使用是正確的,不過結果集及其大,而且需要對結果集group by操作。

觀察該sql語句發現,sku,px_id,sj_id,cat_path四個欄位為冗餘欄位,都可以從其他表連表獲得。

而改sql又存在order by .. limit m,n,可知道後期結果集只有20個,更加適合後期串連相關表擷取其他必要的欄位sku,px_id,sj_id,cat_path。
如此一轉換,可以將group by中的sku,px_id,sj_id,cat_path去掉(其中sku和cat_path為字串),節省後期大結果集中排序的記憶體。
而group by和distinct最佳化的一種方式是,盡量減少不必要的欄位,可以參考簡朝陽寫的《MySQL效能調優和架構設計》的第8.6小結,或者參考以下串連: 。

我們可以嘗試將sql改成
SELECT products_id, COUNT(*) AS pv, COUNT(DISTINCT ip) AS ip_numbers, SUM(is_bounce) AS bounce_numbers, SUM(remain_time) AS remain_time
    FROM dm_pv_records_search
    WHERE 1 AND add_date >= '2014-02-26 10:00:00' AND products_id > 0
GROUP BY products_id
order by ip_numbers desc limit  0,20;

對比兩次profiling,前者執行時間是58s,後者執行時間是5s.
前者profiling如下
+----------------------+-----------+----------+------------+--------------+---------------+
| Status              | Duration  | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+----------------------+-----------+----------+------------+--------------+---------------+
| starting            |  0.000133 | 0.000000 |  0.000000 |            0 |            0 |
| checking permissions |  0.000014 | 0.000000 |  0.000000 |            0 |            0 |
| Opening tables      |  0.000031 | 0.000000 |  0.000000 |            0 |            0 |
| System lock          |  0.000018 | 0.000000 |  0.000000 |            0 |            0 |
| init                |  0.000061 | 0.000000 |  0.000000 |            0 |            0 |
| optimizing          |  0.000019 | 0.000000 |  0.000000 |            0 |            0 |
| statistics          |  0.000073 | 0.000000 |  0.000000 |            0 |            0 |
| preparing            |  0.000051 | 0.000000 |  0.000000 |            0 |            0 |
| Creating tmp table  |  0.000051 | 0.000000 |  0.000000 |            0 |            0 |
| Sorting for group    | 47.735389 | 4.614299 |  10.773362 |        3632 |      2811456 |
| executing            |  0.000010 | 0.000000 |  0.000000 |            0 |            0 |
| Copying to tmp table | 11.566292 | 0.910861 |  0.586911 |          256 |          4408 |
| Sorting result      |  0.030459 | 0.025996 |  0.004000 |            0 |            0 |
| Sending data        |  0.000057 | 0.000000 |  0.000000 |            0 |            0 |
| end                  |  0.000005 | 0.000000 |  0.000000 |            0 |            0 |
| removing tmp table  |  0.008139 | 0.000000 |  0.008998 |            0 |            0 |
| end                  |  0.000007 | 0.000000 |  0.000000 |            0 |            0 |
| query end            |  0.000004 | 0.000000 |  0.000000 |            0 |            0 |
| closing tables      |  0.000011 | 0.000000 |  0.000000 |            0 |            0 |
| freeing items        |  0.000071 | 0.000000 |  0.000000 |            0 |            0 |
| removing tmp table  |  0.000006 | 0.000000 |  0.000000 |            0 |            0 |
| freeing items        |  0.000331 | 0.000000 |  0.000000 |            0 |            0 |
| logging slow query  |  0.000006 | 0.000000 |  0.000000 |            0 |            0 |
| logging slow query  |  0.000047 | 0.000000 |  0.000000 |            0 |            8 |
| cleaning up          |  0.000005 | 0.000000 |  0.000000 |            0 |            0 |
+----------------------+-----------+----------+------------+--------------+---------------+

去掉多餘欄位後的profiling
+--------------------------------+----------+----------+------------+--------------+---------------+
| Status                        | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+--------------------------------+----------+----------+------------+--------------+---------------+
| starting                      | 0.000027 | 0.000000 |  0.000000 |            0 |            0 |
| Waiting for query cache lock  | 0.000008 | 0.000000 |  0.000000 |            0 |            0 |
| checking query cache for query | 0.000082 | 0.000000 |  0.000000 |            0 |            0 |
| checking permissions          | 0.000011 | 0.000000 |  0.000000 |            0 |            0 |
| Opening tables                | 0.000020 | 0.000000 |  0.000000 |            0 |            0 |
| System lock                    | 0.000014 | 0.000000 |  0.000000 |            0 |            0 |
| Waiting for query cache lock  | 0.000038 | 0.000000 |  0.000000 |            0 |            0 |
| init                          | 0.000040 | 0.000000 |  0.000000 |            0 |            0 |
| optimizing                    | 0.000019 | 0.000000 |  0.000000 |            0 |            0 |
| statistics                    | 0.000061 | 0.000000 |  0.000000 |            0 |            0 |
| preparing                      | 0.000051 | 0.000000 |  0.000000 |            0 |            0 |
| Creating tmp table            | 0.000042 | 0.000000 |  0.000000 |            0 |            0 |
| Sorting for group              | 5.275880 | 2.539614 |  6.813964 |          120 |        287368 |
| executing                      | 0.000011 | 0.000000 |  0.000000 |            0 |            0 |
| Copying to tmp table          | 0.492344 | 0.478927 |  0.021997 |          24 |          264 |
| Sorting result                | 0.018007 | 0.016997 |  0.001000 |            0 |            0 |
| Sending data                  | 0.000039 | 0.000000 |  0.000000 |            0 |            0 |
| end                            | 0.000005 | 0.000000 |  0.000000 |            0 |            0 |
| removing tmp table            | 0.001028 | 0.000000 |  0.000999 |            0 |            0 |
| end                            | 0.000007 | 0.000000 |  0.000000 |            0 |            0 |
| query end                      | 0.000004 | 0.000000 |  0.000000 |            0 |            0 |
| closing tables                | 0.000011 | 0.000000 |  0.000000 |            0 |            0 |
| freeing items                  | 0.000073 | 0.000000 |  0.000000 |            0 |            0 |
| removing tmp table            | 0.000006 | 0.000000 |  0.000000 |            0 |            0 |
| freeing items                  | 0.000008 | 0.000000 |  0.000000 |            0 |            0 |
| Waiting for query cache lock  | 0.000003 | 0.000000 |  0.000000 |            0 |            0 |
| freeing items                  | 0.000290 | 0.000000 |  0.000000 |            0 |            0 |
| Waiting for query cache lock  | 0.000007 | 0.000000 |  0.000000 |            0 |            0 |
| freeing items                  | 0.000003 | 0.000000 |  0.000000 |            0 |            0 |
| storing result in query cache  | 0.000005 | 0.000000 |  0.000000 |            0 |            0 |
| logging slow query            | 0.000003 | 0.000000 |  0.000000 |            0 |            0 |
| logging slow query            | 0.000068 | 0.000000 |  0.000000 |            0 |            8 |
| cleaning up                    | 0.000007 | 0.000000 |  0.000000 |            0 |            0 |
+--------------------------------+----------+----------+------------+--------------+---------------+

我們可以看到沒有去掉多餘欄位前 Sorting for group和Copying to tmp table佔用了大部分io和cpu,而去掉後Copying to tmp table佔用io和cpu時間相對來說較少,而且 Sorting for group效能提升5倍之多!

注意事項
1. 最佳化group by和distinct我們在sql層面能做的事情是盡量減少不必要的欄位

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.