GDB 除錯 Mysql 實戰(三)優先佇列排序演算法中的行記錄長度統計是怎麼來的(下)
背景
接著 https://mengkang.net/1328.html 的案例,我們繼續磕。
上一篇 GDB 除錯 Mysql 實戰(三)優先佇列排序演算法探究(上) 分析了實驗3中的 row_size
為什麼是24。其他實驗的 row_size
都是36,掃描行數也不符合預期。這篇就來探究下。
以實驗1為例來分析
select `aid`,sum(`pv`) as num from article_rank force index(idx_day_aid_pv)where `day`>20190115 group by aid order by num desc LIMIT 10;
optimizer_trace.join_execution.steps
的結果如下
{ "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": 649091, "number_of_tmp_files": 0, "sort_buffer_size": 488, "sort_mode": "<sort_key, additional_fields>" } } ] } }
row_size 為什麼是 36
(gdb) b Sort_param::init_for_filesort Breakpoint 1 at 0xf1a89f: file /root/newdb/mysql-server/sql/filesort.cc, line 107.
(gdb) b Filesort::get_addon_fields Breakpoint 2 at 0xf21231: file /root/newdb/mysql-server/sql/filesort.cc, line 2459. (gdb) b /root/newdb/mysql-server/sql/filesort.cc:2496 Breakpoint 3 at 0xf212f9: file /root/newdb/mysql-server/sql/filesort.cc, line 2496. (gdb) b /root/newdb/mysql-server/sql/filesort.cc:2523 Breakpoint 4 at 0xf2145f: file /root/newdb/mysql-server/sql/filesort.cc, line 2523.
排序欄位還是實驗3一樣是16位元組,後面20位元組則是兩個欄位相加20位元組+ (null_fields + 7) / 8
一個可為空的欄位,所以最後是36了。
rows_estimate 為什麼是 1057
(gdb) b /root/newdb/mysql-server/sql/filesort.cc:320 Breakpoint 5 at 0xf1b1d9: file /root/newdb/mysql-server/sql/filesort.cc, line 320. ... Breakpoint 5, filesort (thd=0x7f0214000d80, filesort=0x7f021401f668, sort_positions=false, examined_rows=0x7f022804d050, found_rows=0x7f022804d048, returned_rows=0x7f022804d040) at /root/newdb/mysql-server/sql/filesort.cc:320 320num_rows= table->file->estimate_rows_upper_bound(); (gdb) s ha_innobase::estimate_rows_upper_bound (this=0x7f0214022b50) at /root/newdb/mysql-server/storage/innobase/handler/ha_innodb.cc:13655 ha_innobase::estimate_rows_upper_bound (this=0x7f0214022b50) at /root/newdb/mysql-server/storage/innobase/handler/ha_innodb.cc:13655 warning: Source file is more recent than executable. 13655DBUG_ENTER("estimate_rows_upper_bound"); (gdb) n 13661update_thd(ha_thd()); (gdb) n 13663TrxInInnoDBtrx_in_innodb(m_prebuilt->trx); (gdb) n 13665m_prebuilt->trx->op_info = "calculating upper bound for table rows"; (gdb) n 13667index = dict_table_get_first_index(m_prebuilt->table); (gdb) n 13669ulintstat_n_leaf_pages = index->stat_n_leaf_pages; (gdb) p stat_n_leaf_pages $19 = 139646902217632 (gdb) n 13671ut_a(stat_n_leaf_pages > 0); (gdb) p UNIV_PAGE_SIZE No symbol "UNIV_PAGE_SIZE" in current context. (gdb) n 13674((ulonglong) stat_n_leaf_pages) * UNIV_PAGE_SIZE; (gdb) n 13681estimate = 2 * local_data_file_length (gdb) p local_data_file_length $20 = 16384 (gdb) p stat_n_leaf_pages $21 = 1 (gdb) n 13682/ dict_index_calc_min_rec_len(index); (gdb) n 13684m_prebuilt->trx->op_info = ""; (gdb) p estimate $22 = 1057 (gdb) p dict_index_calc_min_rec_len(index) $23 = 31
也就是說 local_data_file_length
是16位元組,為當前系統一個記憶體頁大小。
dict_index_calc_min_rec_len
註釋中寫道
Calculates the minimum record length in an index.
dict_index_calc_min_rec_len(index)
的值為31,太複雜了先不看了,懵逼了。