本篇內(nèi)容主要講解“MySQL優(yōu)化器追蹤分析”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“mysql優(yōu)化器追蹤分析”吧!
創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供梁山網(wǎng)站建設(shè)、梁山做網(wǎng)站、梁山網(wǎng)站設(shè)計(jì)、梁山網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、梁山企業(yè)網(wǎng)站模板建站服務(wù),十余年梁山做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
以下 left join語句,d表與s表關(guān)聯(lián),當(dāng)where條件在d.deptid上時(shí),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;
執(zhí)行語句
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": [ #準(zhǔn)備階段 { "join_preparation": { "select#": 1, "steps": [ { #expanded_query,解析查詢語句,"*" 轉(zhuǎn)換成字段,left join on 處轉(zhuǎn)化成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')" }, { #轉(zhuǎn)化成的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 */ },#準(zhǔn)備階段結(jié)束 { #優(yōu)化階段: "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",----去除多余無關(guān)的條件處理 "resulting_condition": "(`SSS`.`d`.`Deptid` = '00001111')" } ] /* steps */ } /* condition_processing */ },#結(jié)束,因?yàn)檫@里已經(jīng)夠簡化了,所以三次處理后都是同樣的。 { #替代產(chǎn)生的字段 "substitute_generated_columns": { } /* substitute_generated_columns */ }, { #表依賴關(guān)系檢查 "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 */ }, #表依賴關(guān)系檢查結(jié)束 {#找出可使用索引的字段: "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 */ }, {#評(píng)估每個(gè)表單表訪問行數(shù)及相應(yīng)代價(jià)。 "rows_estimation": [ { "table": "`SSS`.`department` `d`", "rows": 1, ---返回1行 "cost": 1, ---代價(jià)為1 "table_type": "const", ---d表使用的方式是const,即根據(jù)主鍵索引獲取 "empty": false }, { "table": "`ppp`.`shop` `s`", "table_scan": { -------s表直接使用全表掃描 "rows": 978662, ------掃描978662行 "cost": 8109 ------代價(jià)為8109 } /* table_scan */ } ] /* rows_estimation */ }, {#評(píng)估執(zhí)行計(jì)劃,這里考慮兩表連接 "considered_execution_plans": [ { "plan_prefix": [------------------執(zhí)行計(jì)劃的前綴,這里是d表,因?yàn)槭莑eft join 我認(rèn)為指的應(yīng)該是驅(qū)動(dòng)表的意思 "`SSS`.`department` `d`" ] /* plan_prefix */, "table": "`ppp`.`shop` `s`", "best_access_path": {-------最優(yōu)訪問路徑 "considered_access_paths": [考慮的訪問路徑 { "rows_to_scan": 978662,---掃描978662行 "access_type": "scan",--------全表掃描的方式 "resulting_rows": 978662, "cost": 203841,----------使用代價(jià) "chosen": true-------選中 } ] /* considered_access_paths */ } /* best_access_path */, "condition_filtering_pct": 100,條件過濾率100%,指的是這里與上一個(gè)表進(jìn)行行過濾的行數(shù) "rows_for_plan": 978662,------執(zhí)行計(jì)劃的掃描行數(shù)978662 "cost_for_plan": 203841,-------執(zhí)行計(jì)劃的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": "(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)
以上優(yōu)化器的主要步驟:
1.join_preparation :準(zhǔn)備階段,包查詢語句轉(zhuǎn)換,轉(zhuǎn)換成嵌套循環(huán)語句等
expanded_query
transformations_to_nested_joins
2.join_optimization :優(yōu)化階段,包括以下主要階段
condition_processing :處理where條件部分,主要包括等式處理、常量處理、多余條件處理
table_dependencies :表依賴檢查
ref_optimizer_key_uses :評(píng)估可用的索引
rows_estimation :評(píng)估訪問單表的方式,及掃描的行數(shù)與代價(jià)
considered_execution_plans :評(píng)估最終可使用的執(zhí)行計(jì)劃
condition_on_constant_tables :檢查帶常量表的條件
attaching_conditions_to_tables :將常量條件作用到表
refine_plan 改進(jìn)計(jì)劃,不理解
3.join_execution :執(zhí)行階段
通過以上可以看錯(cuò),當(dāng)優(yōu)化器一開始對(duì)優(yōu)化器進(jìn)行評(píng)估時(shí)就直接選擇了全表掃描的方式,即是說此時(shí)優(yōu)化器直接忽視了s表已有的索引IND_SHOP_DEPTID。
我們將以下的d.DEPTID = '00001111' 換成s.DEPTID = '00001111',發(fā)現(xiàn)其可以選擇了索引,此時(shí)s表看起來做了驅(qū)動(dòng)表。
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)
追蹤優(yōu)化器過程:
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 */
到此,相信大家對(duì)“mysql優(yōu)化器追蹤分析”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!