中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

mysql優化器追蹤分析

發布時間:2021-11-08 11:38:25 來源:億速云 閱讀:117 作者:iii 欄目:MySQL數據庫

本篇內容主要講解“mysql優化器追蹤分析”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“mysql優化器追蹤分析”吧!

以下 left join語句,d表與s表關聯,當where條件在d.deptid上時,s表無法走索引。因此通過開啟trace方式做一些追蹤。
root@(none) 09:20:20>explain SELECT * FROM SSS.DEPARTMENT d LEFT JOIN ppp.shop s ON d.DEPTID = s.DEPTID WHERE d.DEPTID = '00001111';
+----+-------------+-------+------------+-------+----------------------------+---------+---------+-------+--------+----------+-------------+
| id | select_type | table | partitions | type  | possible_keys              | key     | key_len | ref   | rows   | filtered | Extra       |
+----+-------------+-------+------------+-------+----------------------------+---------+---------+-------+--------+----------+-------------+
|  1 | SIMPLE      | d     | NULL       | const | PRIMARY,INDEX_DEPARTMENT_5 | PRIMARY | 130     | const |      1 |   100.00 | NULL        |
|  1 | SIMPLE      | s     | NULL       | ALL   | NULL                       | NULL    | NULL    | NULL  | 978629 |   100.00 | Using where |
+----+-------------+-------+------------+-------+----------------------------+---------+---------+-------+--------+----------+-------------+

開啟optimizer_trace:

set optimizer_trace='enabled=on';

 set optimizer_trace_max_mem_size=1000000;

 set end_markers_in_json=on;

 執行語句

 select * from information_schema.optimizer_trace\G;

root@(none) 09:39:58> select * from information_schema.optimizer_trace\G;
*************************** 1. row ***************************
                            QUERY: SELECT * FROM SSS.DEPARTMENT d LEFT JOIN ppp.shop s ON d.DEPTID = s.DEPTID WHERE d.DEPTID = '00001111'
                            TRACE: {
  "steps": [
  
  #準備階段
    {
      "join_preparation": {
        "select#": 1,
        "steps": [
          {  
          #expanded_query,解析查詢語句,"*" 轉換成字段,left join on 處轉化成on((`SSS`.`d`.`Deptid` = convert(`ppp`.`s`.`Deptid` using utf8mb4))))
            "expanded_query": "/* select#1 */ select `SSS`.`d`.`Organid` AS `Organid`,。。。`s`.`Status` AS `Status`,`ppp`.`s`.`Stylecategoryid` AS `Stylecategoryid`,`ppp`.`s`.`Turnontime` AS `Turnontime` from (`SSS`.`department` `d` left join `ppp`.`shop` `s` on((`SSS`.`d`.`Deptid` = convert(`ppp`.`s`.`Deptid` using utf8mb4)))) where (`SSS`.`d`.`Deptid` = '00001111')"
          },
          {
          #轉化成的nested join語句:
            "transformations_to_nested_joins": {
              "transformations": [
                "parenthesis_removal"
              ] /* transformations */,
              "expanded_query": "/* select#1 */ select `SSS`.`d`.`Organid`。。。 `SSS`.`d`.`Guidecode` AS `Guidecode`,`SSS`.`d`.`Createdate` AS `Createdate`,`SSS`.`d`.`Plateformuser` AS `Plateformuser`,`SSS`.`d`.`Plateformdept` AS `Plateformdept`,`SSS`.`d`.`Agentuser` AS `Agentuser`,`SSS`.`d`.`Agentdept` AS `Agentdept`,`SSS`.`d`.`Shopstatus` AS `Shopstatus`,`SSS`.`d`.`Deptshortname` AS `Deptshortname`,`SSS`.`d`.`Storetype` AS `Storetype`,`SSS`.`d`.`Depttype` AS `Depttype`,`ppp`.`s`.`Shopid` AS `Shopid`,`ppp`.`s`.`Objectid` AS `Objectid`,`ppp`.`s`.`Shopname` AS `Shopname`,`ppp`Tel`,`ppp`.`s`.`Introduce` AS `Introduce`,`ppp`.`s`.`Industry` AS `Industry`,`ppp`.`s`.`Address` AS `Address`,`ppp`.`s`.`Shop360image` AS `Shop360image`,`ppp`.`s`.`Domain` AS `Domain`,`ppp`.`s`.`Organid` AS `Organid`,`ppp`.`s`.`Deptid` AS `Deptid`,`ppp`.`s`.`Brandids` AS `Brandids`,`ppp`.`s`.`Extdata` AS `Extdata`,`ppp`.`s`.`Ranking` AS `Ranking`,`ppp`.`s`.`Isdelete` AS `Isdelete`,`ppp`.`s`.`District` AS `District`,`ppp`.`s`.`City` AS `City`,`ppp`.`s`.`Province` AS `Province`,`ppp`.`s`.`Phone` AS `Phone`,`ppp`.`s`.`Watermarkimage` AS `Watermarkimage`,`ppp`.`s`.`Drawingimage` AS `Drawingimage`,`ppp`.`s`.`Contactuser` AS `Contactuser`,`ppp`.`s`.`Panoloadingimage` AS `Panoloadingimage`,`ppp`.`s`.`Lngandlat` AS `Lngandlat`,`ppp`.`s`.`Createtime` AS `Createtime`,`ppp`.`s`.`Shoptype` AS `Shoptype`,`ppp`.`s`.`Status` AS `Status`,`ppp`.`s`.`Stylecategoryid` AS `Stylecategoryid`,`ppp`.`s`.`Turnontime` AS `Turnontime` from `SSS`.`department` `d` left join `ppp`.`shop` `s` on((`SSS`.`d`.`Deptid` = convert(`ppp`.`s`.`Deptid` using utf8mb4))) where (`SSS`.`d`.`Deptid` = '00001111')"
            } /* transformations_to_nested_joins */
          }
        ] /* steps */
      } /* join_preparation */
    },#準備階段結束
    
    
    {
    #優化階段:
      "join_optimization": {
        "select#": 1,
        "steps": [
          { 
          #處理where條件部分,化簡條件:
            "condition_processing": {
              "condition": "WHERE",
              "original_condition": "(`SSS`.`d`.`Deptid` = '00001111')",---原始條件
              "steps": [
                {
                  "transformation": "equality_propagation", ----等式處理
                  "resulting_condition": "(`SSS`.`d`.`Deptid` = '00001111')"
                },
                {
                  "transformation": "constant_propagation",-----常量處理
                  "resulting_condition": "(`SSS`.`d`.`Deptid` = '00001111')"
                },
                {
                  "transformation": "trivial_condition_removal",----去除多余無關的條件處理
                  "resulting_condition": "(`SSS`.`d`.`Deptid` = '00001111')"
                }
              ] /* steps */
            } /* condition_processing */
          },#結束,因為這里已經夠簡化了,所以三次處理后都是同樣的。
          
          {
          #替代產生的字段
            "substitute_generated_columns": {
            } /* substitute_generated_columns */
          },
          
          {
          #表依賴關系檢查
            "table_dependencies": [
              {
                "table": "`SSS`.`department` `d`", ------表d
                "row_may_be_null": false,
                "map_bit": 0,
                "depends_on_map_bits": [
                ] /* depends_on_map_bits */
              },
              {
                "table": "`ppp`.`shop` `s`", --------表s
                "row_may_be_null": true,
                "map_bit": 1,
                "depends_on_map_bits": [
                  0
                ] /* depends_on_map_bits */
              }
            ] /* table_dependencies */
          }, #表依賴關系檢查結束
          
          
          {#找出可使用索引的字段:
            "ref_optimizer_key_uses": [
              {
                "table": "`SSS`.`department` `d`",
                "field": "Deptid", ----------可用的是Deptid
                "equals": "'00001111'",
                "null_rejecting": false ---
              },
              {
                "table": "`SSS`.`department` `d`",
                "field": "Deptid",
                "equals": "'00001111'",
                "null_rejecting": false
              }
            ] /* ref_optimizer_key_uses */
          },
          
          {#評估每個表單表訪問行數及相應代價。
            "rows_estimation": [
              {
                "table": "`SSS`.`department` `d`",
                "rows": 1,   ---返回1行
                "cost": 1,   ---代價為1
                "table_type": "const",  ---d表使用的方式是const,即根據主鍵索引獲取
                "empty": false
              },
              {
                "table": "`ppp`.`shop` `s`",
                "table_scan": { -------s表直接使用全表掃描
                  "rows": 978662, ------掃描978662行
                  "cost": 8109    ------代價為8109
                } /* table_scan */
              }
            ] /* rows_estimation */
          },
          
          
          {#評估執行計劃,這里考慮兩表連接
            "considered_execution_plans": [
              {
                "plan_prefix": [------------------執行計劃的前綴,這里是d表,因為是left join 我認為指的應該是驅動表的意思
                  "`SSS`.`department` `d`"
                ] /* plan_prefix */,
                "table": "`ppp`.`shop` `s`",
                "best_access_path": {-------最優訪問路徑
                  "considered_access_paths": [考慮的訪問路徑
                    {
                      "rows_to_scan": 978662,---掃描978662行
                      "access_type": "scan",--------全表掃描的方式
                      "resulting_rows": 978662,
                      "cost": 203841,----------使用代價
                      "chosen": true-------選中
                    }
                  ] /* considered_access_paths */
                } /* best_access_path */,
                "condition_filtering_pct": 100,條件過濾率100%,指的是這里與上一個表進行行過濾的行數
                "rows_for_plan": 978662,------執行計劃的掃描行數978662
                "cost_for_plan": 203841,-------執行計劃的cost203841
                "chosen": true---------選中
              }
            ] /* considered_execution_plans */
          },
          
          
          {#檢查帶常量表的條件
            "condition_on_constant_tables": "('00001111' = '00001111')",
            "condition_value": true
          },
          
          
          {   #將常量條件作用到表,這里主要是將d表的中的deptid條件作用到s表的deptid
            "attaching_conditions_to_tables": {
              "original_condition": "('00001111' = '00001111')",
              "attached_conditions_computation": [
              ] /* attached_conditions_computation */,
              "attached_conditions_summary": [
                {
                  "table": "`ppp`.`shop` `s`",
                  "attached": "<if>(is_not_null_compl(s), ('00001111' = convert(`ppp`.`s`.`Deptid` using utf8mb4)), true)"
                }
              ] /* attached_conditions_summary */
            } /* attaching_conditions_to_tables */
          },
          
          
          {
            "refine_plan": [
              {
                "table": "`ppp`.`shop` `s`"
              }
            ] /* refine_plan */
          }
        ] /* steps */
      } /* join_optimization */
    },
    
    {
      "join_execution": {
        "select#": 1,
        "steps": [
        ] /* steps */
      } /* join_execution */
    }
    
  ] /* steps */
}
MISSING_BYTES_BEYOND_MAX_MEM_SIZE: 0
          INSUFFICIENT_PRIVILEGES: 0
1 row in set (0.00 sec)

以上優化器的主要步驟:

1.join_preparation :準備階段,包查詢語句轉換,轉換成嵌套循環語句等

   expanded_query

   transformations_to_nested_joins

2.join_optimization :優化階段,包括以下主要階段

  condition_processing :處理where條件部分,主要包括等式處理、常量處理、多余條件處理

  table_dependencies :表依賴檢查

  ref_optimizer_key_uses :評估可用的索引

  rows_estimation        :評估訪問單表的方式,及掃描的行數與代價

  considered_execution_plans :評估最終可使用的執行計劃

  condition_on_constant_tables :檢查帶常量表的條件

  attaching_conditions_to_tables :將常量條件作用到表

  refine_plan 改進計劃,不理解

3.join_execution  :執行階段

通過以上可以看錯,當優化器一開始對優化器進行評估時就直接選擇了全表掃描的方式,即是說此時優化器直接忽視了s表已有的索引IND_SHOP_DEPTID。

我們將以下的d.DEPTID = '00001111' 換成s.DEPTID = '00001111',發現其可以選擇了索引,此時s表看起來做了驅動表。

SELECT * FROM SSS.DEPARTMENT d LEFT JOIN ppp.shop s ON d.DEPTID = s.DEPTID WHERE s.DEPTID = '00001111';
root@SSS 04:28:39>explain  SELECT * FROM SSS.DEPARTMENT d LEFT JOIN ppp.shop s ON d.DEPTID = s.DEPTID WHERE s.DEPTID = '00001111';
+----+-------------+-------+------------+--------+----------------------------+-----------------+---------+-------+------+----------+-------------+
| id | select_type | table | partitions | type   | possible_keys              | key             | key_len | ref   | rows | filtered | Extra       |
+----+-------------+-------+------------+--------+----------------------------+-----------------+---------+-------+------+----------+-------------+
|  1 | SIMPLE      | s     | NULL       | ref    | IND_SHOP_DEPTID            | IND_SHOP_DEPTID | 99      | const |    1 |   100.00 | NULL        |
|  1 | SIMPLE      | d     | NULL       | eq_ref | PRIMARY,INDEX_DEPARTMENT_5 | PRIMARY         | 130     | func  |    1 |   100.00 | Using where |
+----+-------------+-------+------------+--------+----------------------------+-----------------+---------+-------+------+----------+-------------+
2 rows in set, 1 warning (0.00 sec)

追蹤優化器過程:

1.在ref_optimizer_key_uses 過程找到s表可以通過"'00001111'"走索引,并且通過"Deptid"等值訪問

2.在rows_estimation過程中s表選擇IND_SHOP_DEPTID的cost最低。

3.在considered_execution_plans過程選擇IND_SHOP_DEPTID的訪問路徑,并訪問方式是ref。

  {
            "ref_optimizer_key_uses": [
              {
                "table": "`SSS`.`department` `d`",
                "field": "Deptid",
                "equals": "convert(`ppp`.`s`.`Deptid` using utf8mb4)",
                "null_rejecting": false
              },
              {
                "table": "`SSS`.`department` `d`",
                "field": "Deptid",
                "equals": "convert(`ppp`.`s`.`Deptid` using utf8mb4)",
                "null_rejecting": false
              },
              {
                "table": "`ppp`.`shop` `s`",
                "field": "Deptid",
                "equals": "'00001111'",
                "null_rejecting": false
              }
            ] /* ref_optimizer_key_uses */
          },
          {
            "rows_estimation": [
              {
                "table": "`SSS`.`department` `d`",
                "table_scan": {
                  "rows": 911858,
                  "cost": 7212
                } /* table_scan */
              },
              {
                "table": "`ppp`.`shop` `s`",
                "range_analysis": {
                  "table_scan": {
                    "rows": 959814,
                    "cost": 200074
                  } /* table_scan */,
                  "potential_range_indexes": [
                    {
                      "index": "PRIMARY",
                      "usable": false,
                      "cause": "not_applicable"
                    },
                    {
                      "index": "IND_SHOP_DEPTID",
                      "usable": true,
                      "key_parts": [
                        "Deptid",
                        "Shopid"
                      ] /* key_parts */
                    },
                    {
                      "index": "IND_SHOP_DOMAIN",
                      "usable": false,
                      "cause": "not_applicable"
                    }
                  ] /* potential_range_indexes */,
                  "setup_range_conditions": [
                  ] /* setup_range_conditions */,
                  "group_index_range": {
                    "chosen": false,
                    "cause": "not_single_table"
                  } /* group_index_range */,
                  "analyzing_range_alternatives": {
                    "range_scan_alternatives": [
                      {
                        "index": "IND_SHOP_DEPTID",
                        "ranges": [
                          "00001111 <= Deptid <= 00001111"
                        ] /* ranges */,
                        "index_dives_for_eq_ranges": true,
                        "rowid_ordered": true,
                        "using_mrr": false,
                        "index_only": false,
                        "rows": 1,
                        "cost": 2.21,
                        "chosen": true
                      }
                    ] /* range_scan_alternatives */,
                    "analyzing_roworder_intersect": {
                      "usable": false,
                      "cause": "too_few_roworder_scans"
                    } /* analyzing_roworder_intersect */
                  } /* analyzing_range_alternatives */,
                  "chosen_range_access_summary": {
                    "range_access_plan": {
                      "type": "range_scan",
                      "index": "IND_SHOP_DEPTID",
                      "rows": 1,
                      "ranges": [
                        "00001111 <= Deptid <= 00001111"
                      ] /* ranges */
                    } /* range_access_plan */,
                    "rows_for_plan": 1,
                    "cost_for_plan": 2.21,
                    "chosen": true
                  } /* chosen_range_access_summary */
                } /* range_analysis */
              }
            ] /* rows_estimation */
          },
          {
            "considered_execution_plans": [
              {
                "plan_prefix": [
                ] /* plan_prefix */,
                "table": "`ppp`.`shop` `s`",
                "best_access_path": {
                  "considered_access_paths": [
                    {
                      "access_type": "ref",
                      "index": "IND_SHOP_DEPTID",
                      "rows": 1,
                      "cost": 1.2,
                      "chosen": true
                    },
                    {
                      "access_type": "range",
                      "range_details": {
                        "used_index": "IND_SHOP_DEPTID"
                      } /* range_details */,
                      "chosen": false,
                      "cause": "heuristic_index_cheaper"
                    }
                  ] /* considered_access_paths */
                } /* best_access_path */,
                "condition_filtering_pct": 100,
                "rows_for_plan": 1,
                "cost_for_plan": 1.2,
                "rest_of_plan": [
                  {
                    "plan_prefix": [
                      "`ppp`.`shop` `s`"
                    ] /* plan_prefix */,
                    "table": "`SSS`.`department` `d`",
                    "best_access_path": {
                      "considered_access_paths": [
                        {
                          "access_type": "eq_ref",
                          "index": "PRIMARY",
                          "rows": 1,
                          "cost": 1.2,
                          "chosen": true,
                          "cause": "clustered_pk_chosen_by_heuristics"
                        },
                        {
                          "access_type": "scan",
                          "cost": 189584,
                          "rows": 911858,
                          "chosen": false,
                          "cause": "cost"
                        }
                      ] /* considered_access_paths */
                    } /* best_access_path */,
                    "added_to_eq_ref_extension": true,
                    "condition_filtering_pct": 100,
                    "rows_for_plan": 1,
                    "cost_for_plan": 2.4,
                    "chosen": true
                  }
                ] /* rest_of_plan */
              }
            ] /* considered_execution_plans */

到此,相信大家對“mysql優化器追蹤分析”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

巩义市| 澄城县| 邯郸市| 麻城市| 龙门县| 金阳县| 若尔盖县| 色达县| 常熟市| 滦平县| 夏河县| 绵竹市| 南丰县| 台东县| 浦北县| 巴里| 保山市| 额济纳旗| 丹巴县| 汽车| 信丰县| 蕲春县| 平武县| 普兰县| 宝应县| 曲阜市| 揭阳市| 屏东市| 玉屏| 衡水市| 乐亭县| 南宫市| 葫芦岛市| 集安市| 通榆县| 庐江县| 闵行区| 东阿县| 肃北| 美姑县| 富裕县|