| 
                         我们查询两个数据,一个是满足条件的行数,一个是group by统计之后的行数。 
- mysql> select count(*) from article_rank where day>=20181220 and day<=20181224;  
 - +----------+  
 - | count(*) |  
 - +----------+  
 - |   785102 |  
 - +----------+  
 - mysql> select count(distinct aid) from article_rank where day>=20181220 and day<=20181224;  
 - +---------------------+  
 - | count(distinct aid) |  
 - +---------------------+  
 - |              552203 |  
 - +---------------------+  
 
  
发现满足条件的总行数(785102)+group by 之后的总行数(552203)+limit 的值 = 慢日志里统计的 Rows_examined。 
要解答这个问题,就必须搞清楚上面这个 sql 到底分别都是如何运行的。 
执行流程分析 
索引示例 
为了便于理解,我按照索引的规则先模拟idx_day_aid_pv索引的一小部分数据 
因为索引idx_day_aid_pv最左列是day,所以当我们需要查找20181220~20181224之间的文章的pv总和的时候,我们需要遍历20181220~20181224这段数据的索引。 
- 查看 optimizer trace 信息  
 - # 开启 optimizer_trace  
 - set optimizer_trace='enabled=on';  
 - # 执行 sql   
 - select aid,sum(pv) as num from article_rank where day>=20181220 and day<=20181224 group by aid order by num desc limit 10;  
 - # 查看 trace 信息  
 - select trace from `information_schema`.`optimizer_trace`G;  
 
  
摘取里面最后的执行结果如下 
- {  
 -   "join_execution": {  
 -     "select#": 1,  
 -     "steps": [  
 -       {  
 -         "creating_tmp_table": {  
 -           "tmp_table_info": {  
 -             "table": "intermediate_tmp_table",  
 -             "row_length": 20,  
 -             "key_length": 4,  
 -             "unique_constraint": false,  
 -             "location": "memory (heap)",  
 -             "row_limit_estimate": 838860  
 -           }  
 -         }  
 -       },  
 -       {  
 -         "converting_tmp_table_to_ondisk": {  
 -           "cause": "memory_table_size_exceeded",  
 -           "tmp_table_info": {  
 -             "table": "intermediate_tmp_table",  
 -             "row_length": 20,  
 -             "key_length": 4,  
 -             "unique_constraint": false,  
 -             "location": "disk (InnoDB)",  
 -             "record_format": "fixed"  
 -           }  
 -         }  
 -       },  
 -       {  
 -         "filesort_information": [  
 -           {  
 -             "direction": "desc",  
 -             "table": "intermediate_tmp_table",  
 -             "field": "num"  
 -           }  
 -         ],  
 -         "filesort_priority_queue_optimization": {  
 -           "limit": 10,  
 -           "rows_estimate": 1057,  
 -           "row_size": 36,  
 -           "memory_available": 262144,  
 -           "chosen": true  
 -         },  
 -         "filesort_execution": [  
 -         ],  
 -         "filesort_summary": {  
 -           "rows": 11,  
 -           "examined_rows": 552203,  
 -           "number_of_tmp_files": 0,  
 -           "sort_buffer_size": 488,  
 -           "sort_mode": "<sort_key, additional_fields>"  
 -         }  
 -       }  
 -     ]  
 -   }  
 - }  
 
  
分析临时表字段 
mysql gdb 调试更多细节 https://mengkang.net/1336.html                         (编辑:52站长网) 
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! 
                     |