跟蹤MYSQL 的查詢優化過程方法

2021-09-08 18:33:31 字數 3393 閱讀 4765

handle_select()

mysql_select()

join::prepare()

setup_fields()

join::optimize()

/*optimizer is from here ...

*/optimize_cond()

opt_sum_query()

make_join_statistics()

get_quick_record_count()

choose_plan()

/*find the best way to access tables

*//*

as specified by the user.

*/optimize_straight_join()

best_access_path()

/*find a (sub-)optimal plan among all or subset

*//*

of all possible query plans where the user

*//*

controls the exhaustiveness of the search.

*/greedy_search()

best_extension_by_limited_search()

best_access_path()

/*perform an exhaustive search for an optimal plan

*/find_best()

make_join_select()

/*... to here

*/join::exec()

create table `t` (

`a`

int(11

) not null,

`b`

int(11

) default null,

`c`

int(11

) not null,

primary key (`a`),

key `b` (`b`),

key `ix` (`b`,`c`)

) engine=innodb default charset=utf8

json格式:

mysql> explain format=json select b from

t\g*************************** 1. row **********************explain: ,

"table

": ,

"used_columns

": [

"b"]}}}

1 row in

set, 1 warning (0.00 sec)

更加詳細的過程生成:

1.set optimizer_trace_max_mem_size=300000;

2.set end_markers_in_json=true;

3.set optimizer_trace="enabled=on";

4.sql語句

5.select trace from information_schema.optimizer_trace\g;

6.set optimizer_trace="enabled=off";

主要分為三個部分 

join_preparation:sql的準備階段,sql被格式化

對應函式 join::prepare

join_optimization:sql優化階段

對應函式join::optimize

join_execution:sql執行階段

對應函式:join::exec

join_optimization是核心。

下面詳細介紹join_optimization的過程:

1)condition_processing階段,進行where條件處理,分別是相等處理,常量處理,刪除冗餘條件

2)ref_optimizer_key_uses階段,查詢可進行ref type訪問的索引(索引的等值訪問)。

3)records_estimation階段,進行訪問開銷預估。這個階段是最複雜的。先處理訪問型別(explain select_type欄位的值),候選項分別為全表掃瞄和所有的索引,開銷最小的那個勝出。如果你的語句有group by,那麼在group_index_range子階段確定是否有適用於range 訪問的索引。

4)considered_execution_plans節顯示了選定的執行計畫

5)attaching_conditions_to_tables分析where條件是否可以執行pushdown,應該是再掃瞄該錶時過濾掉。

6)clause_processing階段分別處理group by, order by從句。

eg:
set optimizer_trace_max_mem_size=300000;

set end_markers_in_json=true;

set optimizer_trace="enabled=on";

mysql> select b from t;

mysql>

select

trace

from

information_schema.optimizer_trace\g

*************************** 1. row ***************************trace: ]}

},]},}

]},]},

"condition_filtering_pct

": 100

,

"rows_for_plan

": 1

,

"cost_for_plan

": 1.2

,

"chosen

": true

} ]},]

}},]}

]}},}

]}1 row in

set (0.00 sec)

set optimizer_trace="enabled=off";

select trace into dumpfile "json.txt" from information_schema.optimizer_trace;

查詢優化(MySQL優化查詢)

關聯查詢太多join 設計缺陷或不得已的需求 資料庫伺服器調優及各個引數設定不適當 緩衝 執行緒數等 慢查詢日誌 找出執行速度慢的sql語句 慢查詢的開啟並捕獲 explain 慢sql分析 show profile查詢sql在mysql伺服器裡面的執行細節和生命週期情況 sql資料庫伺服器的引數調...

mysql優化limit查詢語句的5個方法

mysql的分頁比較簡單,只需要limit offset,length就可以獲取資料了,但是當offset和length比較大的時候,mysql明顯效能下降 1.子查詢優化法 先找出第一條資料,然後大於等於這條資料的id就是要獲取的資料 缺點 資料必須是連續的,可以說不能有where條件,where...

mysql優化limit查詢語句的5個方法

mysql的分頁比較簡單,只需要limit offset,length就可以獲取資料了,但是當offset和length比較大的時候,mysql明顯效能下降 1.子查詢優化法 先找出第一條資料,然後大於等於這條資料的id就是要獲取的資料 缺點 資料必須是連續的,可以說不能有where條件,where...