在執行表掃描(Full Table Scan)前,優化器會呼叫儲存引擎的 info() 函數,收集統計資訊以決定執行計畫。
info() 返回後,優化器直接讀取這些屬性。SHOW TABLE STATUS 使用。sql/handler.h)| 屬性 | 含義 |
|---|---|
data_file_length | 資料檔長度 |
max_data_file_length | 資料檔最大長度 |
index_file_length | 索引檔長度 |
max_index_file_length | 索引檔最大長度 |
delete_length | 可回收空間(刪除記錄殘留) |
auto_increment_value | 下一個自增值 |
records | **表預估行數(最關鍵)** |
deleted | 已刪除行數 |
mean_rec_length | 平均記錄長度(位元組) |
create_time / check_time / update_time | 建立/檢查/更新時間戳 |
records = 0 或 1:優化器傾向直接掃描或特殊處理。records >= 2:正常走索引/掃描成本評估。int ha_example::info(uint flag)
{
// 計算或估算 records
if (external_source) {
share->records = estimate_rows_from_external(); // 必須 >= 2
} else {
share->records = calculate_rows_from_index();
}
share->data_file_length = get_data_file_size();
share->index_file_length = get_index_file_size();
share->auto_increment_value = next_auto_inc();
return 0;
}