欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MySQL如何選擇合適的索引

 更新時間:2019年09月06日 13:00:46   作者:小強的進階之路  
這篇文章主要介紹了MySQL如何選擇合適的索引,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下

先來看一個栗子

EXPLAIN select * from employees where name > 'a';

如果用name索引查找數(shù)據(jù)需要遍歷name字段聯(lián)合索引樹,然后根據(jù)遍歷出來的主鍵值去主鍵索引樹里再去查出最終數(shù)據(jù),成本比全表掃描還高。

可以用覆蓋索引優(yōu)化,這樣只需要遍歷name字段的聯(lián)合索引樹就可以拿到所有的結果。

EXPLAIN select name,age,position from employees where name > 'a';

可以看到通過select出的字段是覆蓋索引,MySQL底層使用了索引優(yōu)化。在看另一個case:

EXPLAIN select * from employees where name > 'zzz';

對于上面的這兩種 name>'a' 和 name>'zzz'的執(zhí)行結果, mysql最終是否選擇走索引或者一張表涉及多個索引, mysql最終如何選擇索引,可以通過trace工具來一查究竟,開啟trace工具會影響mysql性能,所以只能臨時分析sql使用,用完之后需要立即關閉。

SET SESSION optimizer_trace="enabled=on",end_markers_in_json=on; --開啟trace
SELECT * FROM employees WHERE name > 'a' ORDER BY position;
SELECT * FROM information_schema.OPTIMIZER_TRACE;

看trace字段:

{
 "steps": [
 {
  "join_preparation": { --第一階段:SQl準備階段
  "select#": 1,
  "steps": [
   {
   "expanded_query": "/* select#1 */ select `employees`.`id` AS `id`,`employees`.`name` AS `name`,`employees`.`age` AS `age`,`employees`.`position` AS `position`,`employees`.`hire_time` AS `hire_time` from `employees` where (`employees`.`name` > 'a') order by `employees`.`position`"
   }
  ] /* steps */
  } /* join_preparation */
 },
 {
  "join_optimization": { --第二階段:SQL優(yōu)化階段
  "select#": 1,
  "steps": [
   {
   "condition_processing": { --條件處理
    "condition": "WHERE",
    "original_condition": "(`employees`.`name` > 'a')",
    "steps": [
    {
     "transformation": "equality_propagation",
     "resulting_condition": "(`employees`.`name` > 'a')"
    },
    {
     "transformation": "constant_propagation",
     "resulting_condition": "(`employees`.`name` > 'a')"
    },
    {
     "transformation": "trivial_condition_removal",
     "resulting_condition": "(`employees`.`name` > 'a')"
    }
    ] /* steps */
   } /* condition_processing */
   },
   {
   "table_dependencies": [ --表依賴詳情
    {
    "table": "`employees`",
    "row_may_be_null": false,
    "map_bit": 0,
    "depends_on_map_bits": [
    ] /* depends_on_map_bits */
    }
   ] /* table_dependencies */
   },
   {
   "ref_optimizer_key_uses": [
   ] /* ref_optimizer_key_uses */
   },
   {
   "rows_estimation": [ --預估標的訪問成本
    {
    "table": "`employees`",
    "range_analysis": {
     "table_scan": { --全表掃描情況
     "rows": 3, --掃描行數(shù)
     "cost": 3.7 --查詢成本
     } /* table_scan */,
     "potential_range_indices": [ --查詢可能使用的索引
     {
      "index": "PRIMARY", --主鍵索引
      "usable": false,
      "cause": "not_applicable"
     },
     {
      "index": "idx_name_age_position", --輔助索引
      "usable": true,
      "key_parts": [
      "name",
      "age",
      "position",
      "id"
      ] /* key_parts */
     },
     {
      "index": "idx_age",
      "usable": false,
      "cause": "not_applicable"
     }
     ] /* potential_range_indices */,
     "setup_range_conditions": [
     ] /* setup_range_conditions */,
     "group_index_range": {
     "chosen": false,
     "cause": "not_group_by_or_distinct"
     } /* group_index_range */,
     "analyzing_range_alternatives": { ‐‐分析各個索引使用成本
     "range_scan_alternatives": [
      {
      "index": "idx_name_age_position",
      "ranges": [
       "a < name"
      ] /* ranges */,
      "index_dives_for_eq_ranges": true,
      "rowid_ordered": false,
      "using_mrr": false,
      "index_only": false, ‐‐是否使用覆蓋索引
      "rows": 3, --‐‐索引掃描行數(shù)
      "cost": 4.61, --索引使用成本
      "chosen": false, ‐‐是否選擇該索引
      "cause": "cost"
      }
     ] /* range_scan_alternatives */,
     "analyzing_roworder_intersect": {
      "usable": false,
      "cause": "too_few_roworder_scans"
     } /* analyzing_roworder_intersect */
     } /* analyzing_range_alternatives */
    } /* range_analysis */
    }
   ] /* rows_estimation */
   },
   {
   "considered_execution_plans": [
    {
    "plan_prefix": [
    ] /* plan_prefix */,
    "table": "`employees`",
    "best_access_path": {
     "considered_access_paths": [
     {
      "access_type": "scan",
      "rows": 3,
      "cost": 1.6,
      "chosen": true,
      "use_tmp_table": true
     }
     ] /* considered_access_paths */
    } /* best_access_path */,
    "cost_for_plan": 1.6,
    "rows_for_plan": 3,
    "sort_cost": 3,
    "new_cost_for_plan": 4.6,
    "chosen": true
    }
   ] /* considered_execution_plans */
   },
   {
   "attaching_conditions_to_tables": {
    "original_condition": "(`employees`.`name` > 'a')",
    "attached_conditions_computation": [
    ] /* attached_conditions_computation */,
    "attached_conditions_summary": [
    {
     "table": "`employees`",
     "attached": "(`employees`.`name` > 'a')"
    }
    ] /* attached_conditions_summary */
   } /* attaching_conditions_to_tables */
   },
   {
   "clause_processing": {
    "clause": "ORDER BY",
    "original_clause": "`employees`.`position`",
    "items": [
    {
     "item": "`employees`.`position`"
    }
    ] /* items */,
    "resulting_clause_is_simple": true,
    "resulting_clause": "`employees`.`position`"
   } /* clause_processing */
   },
   {
   "refine_plan": [
    {
    "table": "`employees`",
    "access_type": "table_scan"
    }
   ] /* refine_plan */
   },
   {
   "reconsidering_access_paths_for_index_ordering": {
    "clause": "ORDER BY",
    "index_order_summary": {
    "table": "`employees`",
    "index_provides_order": false,
    "order_direction": "undefined",
    "index": "unknown",
    "plan_changed": false
    } /* index_order_summary */
   } /* reconsidering_access_paths_for_index_ordering */
   }
  ] /* steps */
  } /* join_optimization */
 },
 {
  "join_execution": { --第三階段:SQL執(zhí)行階段
  "select#": 1,
  "steps": [
   {
   "filesort_information": [
    {
    "direction": "asc",
    "table": "`employees`",
    "field": "position"
    }
   ] /* filesort_information */,
   "filesort_priority_queue_optimization": {
    "usable": false,
    "cause": "not applicable (no LIMIT)"
   } /* filesort_priority_queue_optimization */,
   "filesort_execution": [
   ] /* filesort_execution */,
   "filesort_summary": {
    "rows": 3,
    "examined_rows": 3,
    "number_of_tmp_files": 0,
    "sort_buffer_size": 200704,
    "sort_mode": "<sort_key, additional_fields>"
   } /* filesort_summary */
   }
  ] /* steps */
  } /* join_execution */
 }
 ] /* steps */
}

全表掃描的成本低于索引掃描, 索引MySQL最終會選擇全表掃描。

SELECT * FROM employees WHERE name > 'zzz' ORDER BY position;
SELECT * FROM information_schema.OPTIMIZER_TRACE;

{
 "steps": [
 {
  "join_preparation": {
  "select#": 1,
  "steps": [
   {
   "expanded_query": "/* select#1 */ select `employees`.`id` AS `id`,`employees`.`name` AS `name`,`employees`.`age` AS `age`,`employees`.`position` AS `position`,`employees`.`hire_time` AS `hire_time` from `employees` where (`employees`.`name` > 'zzz') order by `employees`.`position`"
   }
  ] /* steps */
  } /* join_preparation */
 },
 {
  "join_optimization": {
  "select#": 1,
  "steps": [
   {
   "condition_processing": {
    "condition": "WHERE",
    "original_condition": "(`employees`.`name` > 'zzz')",
    "steps": [
    {
     "transformation": "equality_propagation",
     "resulting_condition": "(`employees`.`name` > 'zzz')"
    },
    {
     "transformation": "constant_propagation",
     "resulting_condition": "(`employees`.`name` > 'zzz')"
    },
    {
     "transformation": "trivial_condition_removal",
     "resulting_condition": "(`employees`.`name` > 'zzz')"
    }
    ] /* steps */
   } /* condition_processing */
   },
   {
   "table_dependencies": [
    {
    "table": "`employees`",
    "row_may_be_null": false,
    "map_bit": 0,
    "depends_on_map_bits": [
    ] /* depends_on_map_bits */
    }
   ] /* table_dependencies */
   },
   {
   "ref_optimizer_key_uses": [
   ] /* ref_optimizer_key_uses */
   },
   {
   "rows_estimation": [
    {
    "table": "`employees`",
    "range_analysis": {
     "table_scan": {
     "rows": 3,
     "cost": 3.7
     } /* table_scan */,
     "potential_range_indices": [
     {
      "index": "PRIMARY",
      "usable": false,
      "cause": "not_applicable"
     },
     {
      "index": "idx_name_age_position",
      "usable": true,
      "key_parts": [
      "name",
      "age",
      "position",
      "id"
      ] /* key_parts */
     },
     {
      "index": "idx_age",
      "usable": false,
      "cause": "not_applicable"
     }
     ] /* potential_range_indices */,
     "setup_range_conditions": [
     ] /* setup_range_conditions */,
     "group_index_range": {
     "chosen": false,
     "cause": "not_group_by_or_distinct"
     } /* group_index_range */,
     "analyzing_range_alternatives": {
     "range_scan_alternatives": [
      {
      "index": "idx_name_age_position",
      "ranges": [
       "zzz < name"
      ] /* ranges */,
      "index_dives_for_eq_ranges": true,
      "rowid_ordered": false,
      "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": "idx_name_age_position",
      "rows": 1,
      "ranges": [
      "zzz < name"
      ] /* 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": "`employees`",
    "best_access_path": {
     "considered_access_paths": [
     {
      "access_type": "range",
      "rows": 1,
      "cost": 2.41,
      "chosen": true,
      "use_tmp_table": true
     }
     ] /* considered_access_paths */
    } /* best_access_path */,
    "cost_for_plan": 2.41,
    "rows_for_plan": 1,
    "sort_cost": 1,
    "new_cost_for_plan": 3.41,
    "chosen": true
    }
   ] /* considered_execution_plans */
   },
   {
   "attaching_conditions_to_tables": {
    "original_condition": "(`employees`.`name` > 'zzz')",
    "attached_conditions_computation": [
    ] /* attached_conditions_computation */,
    "attached_conditions_summary": [
    {
     "table": "`employees`",
     "attached": "(`employees`.`name` > 'zzz')"
    }
    ] /* attached_conditions_summary */
   } /* attaching_conditions_to_tables */
   },
   {
   "clause_processing": {
    "clause": "ORDER BY",
    "original_clause": "`employees`.`position`",
    "items": [
    {
     "item": "`employees`.`position`"
    }
    ] /* items */,
    "resulting_clause_is_simple": true,
    "resulting_clause": "`employees`.`position`"
   } /* clause_processing */
   },
   {
   "refine_plan": [
    {
    "table": "`employees`",
    "pushed_index_condition": "(`employees`.`name` > 'zzz')",
    "table_condition_attached": null,
    "access_type": "range"
    }
   ] /* refine_plan */
   },
   {
   "reconsidering_access_paths_for_index_ordering": {
    "clause": "ORDER BY",
    "index_order_summary": {
    "table": "`employees`",
    "index_provides_order": false,
    "order_direction": "undefined",
    "index": "idx_name_age_position",
    "plan_changed": false
    } /* index_order_summary */
   } /* reconsidering_access_paths_for_index_ordering */
   }
  ] /* steps */
  } /* join_optimization */
 },
 {
  "join_execution": {
  "select#": 1,
  "steps": [
   {
   "filesort_information": [
    {
    "direction": "asc",
    "table": "`employees`",
    "field": "position"
    }
   ] /* filesort_information */,
   "filesort_priority_queue_optimization": {
    "usable": false,
    "cause": "not applicable (no LIMIT)"
   } /* filesort_priority_queue_optimization */,
   "filesort_execution": [
   ] /* filesort_execution */,
   "filesort_summary": {
    "rows": 0,
    "examined_rows": 0,
    "number_of_tmp_files": 0,
    "sort_buffer_size": 200704,
    "sort_mode": "<sort_key, additional_fields>"
   } /* filesort_summary */
   }
  ] /* steps */
  } /* join_execution */
 }
 ] /* steps */
}

查看trace字段可知索引掃描的成本低于全表掃描的成本,所以MySQL最終選擇索引掃描。

SET SESSION optimizer_trace="enabled=off"; -- 關閉trace

總結

以上所述是小編給大家介紹的MySQL如何選擇合適的索引,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!

相關文章

  • mysql 5.7.25 安裝配置方法圖文教程

    mysql 5.7.25 安裝配置方法圖文教程

    這篇文章主要為大家詳細介紹了mysql 5.7.25 安裝配置方法圖文教程,文中安裝步驟介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • Mysql報Table?'mysql.user'?doesn't?exist問題的解決方法

    Mysql報Table?'mysql.user'?doesn't?exist問題的解

    這篇文章主要給大家介紹了關于Mysql報Table?'mysql.user'?doesn't?exist問題的解決方法,初學者可能會遇到這個問題,文中通過圖文將解決方法介紹的非常詳細,需要的朋友可以參考下
    2022-05-05
  • Mysql中in和exists的區(qū)別?&?not?in、not?exists、left?join的相互轉換問題

    Mysql中in和exists的區(qū)別?&?not?in、not?exists、left?join的相互轉換問題

    這篇文章主要介紹了Mysql中in和exists的區(qū)別?&?not?in、not?exists、left?join的相互轉換,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-09-09
  • MySQL之MyISAM存儲引擎的非聚簇索引詳解

    MySQL之MyISAM存儲引擎的非聚簇索引詳解

    這篇文章主要為大家詳細介紹了MySQL之MyISAM存儲引擎的非聚簇索引,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • MySQL清空所有表的數(shù)據(jù)方法示例

    MySQL清空所有表的數(shù)據(jù)方法示例

    本文主要介紹了MySQL清空所有表的數(shù)據(jù)方法示例,要清空MySQL數(shù)據(jù)庫中所有表的數(shù)據(jù),但保留表結構,下面就介紹了幾種常用的方法,具有一定的參考價值,感興趣的可以了解一下
    2024-07-07
  • 優(yōu)化mysql的limit offset的例子

    優(yōu)化mysql的limit offset的例子

    在mysql中,通常使用limit做分頁,而且經(jīng)常會跟order by 連用。在order by 上加索引有時候是很有幫助的,不然系統(tǒng)會做很多的filesort
    2013-02-02
  • 一篇文章帶你了解SQL之CASE WHEN用法詳解

    一篇文章帶你了解SQL之CASE WHEN用法詳解

    本文介紹下,在mysql數(shù)據(jù)庫中,有關case when語句的用法,介紹了case when語句的基礎知識,并提供了相關實例,供大家學習參考,有需要的朋友不要錯過
    2021-08-08
  • linux下利用Docker安裝mysql的步驟

    linux下利用Docker安裝mysql的步驟

    這篇文章主要給大家介紹了關于在linux下利用Docker安裝mysql的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • CentOS7下mysql 8.0.16 安裝配置方法圖文教程

    CentOS7下mysql 8.0.16 安裝配置方法圖文教程

    這篇文章主要為大家詳細介紹了CentOS7下mysql 8.0.16 安裝配置方法圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • mysql創(chuàng)建表分區(qū)的實現(xiàn)示例

    mysql創(chuàng)建表分區(qū)的實現(xiàn)示例

    表分區(qū)是指根據(jù)一定規(guī)則,將數(shù)據(jù)庫中的一張表分解成多個更小的,容易管理的部分,本文主要介紹了mysql創(chuàng)建表分區(qū)的實現(xiàn)示例,感興趣的可以了解一下
    2024-01-01

最新評論