加入收藏 | 设为首页 | 会员中心 | 我要投稿 PHP编程网 - 黄冈站长网 (http://www.0713zz.com/)- 数据应用、建站、人体识别、智能机器人、语音技术!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

一次Group By+Order By性能优化分析

发布时间:2019-03-22 17:04:58 所属栏目:MySql教程 来源:周梦康
导读:副标题#e# 最近通过一个日志表做排行的时候发现特别卡,最后问题得到了解决,梳理一些索引和MySQL执行过程的经验,但是最后还是有5个谜题没解开,希望大家帮忙解答下 主要包含如下知识点 用数据说话证明慢日志的扫描行数到底是如何统计出来的 从 group by

使用idx_aid_day_pv索引的效果:

  1. mysql> explain select aid,sum(pv) as num from article_rank force index(idx_aid_day_pv) where day>=20181220 and day<=20181224 group by aid order by null limit 10; 
  2.  
  3. +----+-------------+--------------+------------+-------+-------------------------------+----------------+---------+------+------+----------+--------------------------+ 
  4.  
  5. | id | select_type | table        | partitions | type  | possible_keys                 | key            | key_len | ref  | rows | filtered | Extra                    | 
  6.  
  7. +----+-------------+--------------+------------+-------+-------------------------------+----------------+---------+------+------+----------+--------------------------+ 
  8.  
  9. |  1 | SIMPLE      | article_rank | NULL       | index | idx_day_aid_pv,idx_aid_day_pv | idx_aid_day_pv | 12      | NULL |   10 |    11.11 | Using where; Using index | 
  10.  
  11. +----+-------------+--------------+------------+-------+-------------------------------+----------------+---------+------+------+----------+--------------------------+  

查看 optimizer trace 信息

  1. # 开启optimizer_trace  
  2. set optimizer_trace='enabled=on';  
  3. # 执行 sql   
  4. select aid,sum(pv) as num from article_rank force index(idx_aid_day_pv) where day>=20181220 and day<=20181224 group by aid order by num desc limit 10;  
  5. # 查看 trace 信息  
  6. select trace from `information_schema`.`optimizer_trace`G;  

摘取里面最后的执行结果如下

  1. {  
  2.   "join_execution": {  
  3.     "select#": 1,  
  4.     "steps": [  
  5.       {  
  6.         "creating_tmp_table": {  
  7.           "tmp_table_info": {  
  8.             "table": "intermediate_tmp_table",  
  9.             "row_length": 20,  
  10.             "key_length": 0,  
  11.             "unique_constraint": false,  
  12.             "location": "memory (heap)",  
  13.             "row_limit_estimate": 838860  
  14.           }  
  15.         }  
  16.       },  
  17.       {  
  18.         "filesort_information": [  
  19.           {  
  20.             "direction": "desc",  
  21.             "table": "intermediate_tmp_table",  
  22.             "field": "num"  
  23.           }  
  24.         ],  
  25.         "filesort_priority_queue_optimization": {  
  26.           "limit": 10,  
  27.           "rows_estimate": 552213,  
  28.           "row_size": 24,  
  29.           "memory_available": 262144,  
  30.           "chosen": true  
  31.         },  
  32.         "filesort_execution": [  
  33.         ],  
  34.         "filesort_summary": {  
  35.           "rows": 11,  
  36.           "examined_rows": 552203,  
  37.           "number_of_tmp_files": 0,  
  38.           "sort_buffer_size": 352,  
  39.           "sort_mode": "<sort_key, rowid>"  
  40.         }  
  41.       }  
  42.     ]  
  43.   }  
  44. }  

执行流程如下

    1. 创建一张临时表,临时表上有两个字段,aid和num字段(sum(pv) as num);

(编辑:PHP编程网 - 黄冈站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读