亚洲综合原千岁中文字幕_国产精品99久久久久久久vr_无码人妻aⅴ一区二区三区浪潮_成人h动漫精品一区二区三

主頁(yè) > 知識(shí)庫(kù) > MySQL如何選擇合適的索引

MySQL如何選擇合適的索引

熱門(mén)標(biāo)簽:真人語(yǔ)音電話機(jī)器人 怎么在地圖標(biāo)注位置生成圖片 悟空科技電話機(jī)器人 電銷(xiāo)卡外呼系統(tǒng)供應(yīng)商 騰訊地圖標(biāo)注提升 400電話可以免費(fèi)申請(qǐng)嗎 銅陵防封電銷(xiāo)卡 美國(guó)反騷擾電話機(jī)器人 福建外呼系統(tǒng)定制化

先來(lái)看一個(gè)栗子

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

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

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

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

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

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

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

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

看trace字段:

{
 "steps": [
 {
  "join_preparation": { --第一階段:SQl準(zhǔn)備階段
  "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": [ --表依賴(lài)詳情
    {
    "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": [ --預(yù)估標(biāo)的訪問(wèn)成本
    {
    "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": { ‐‐分析各個(gè)索引使用成本
     "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最終會(huì)選擇全表掃描。

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"; -- 關(guān)閉trace

總結(jié)

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

您可能感興趣的文章:
  • 為什么MySQL數(shù)據(jù)庫(kù)索引選擇使用B+樹(shù)?
  • 探究MySQL優(yōu)化器對(duì)索引和JOIN順序的選擇
  • mysql的in會(huì)不會(huì)讓索引失效?
  • MySQL組合索引與最左匹配原則詳解
  • Mysql如何適當(dāng)?shù)奶砑铀饕榻B
  • 一個(gè)案例徹底弄懂如何正確使用mysql inndb聯(lián)合索引
  • MySQL中有哪些情況下數(shù)據(jù)庫(kù)索引會(huì)失效詳析
  • 深入淺析Mysql聯(lián)合索引最左匹配原則
  • Mysql使用索引的正確方法及索引原理詳解
  • MySQL的索引詳解
  • MySQL索引使用說(shuō)明(單列索引和多列索引)

標(biāo)簽:湖南 烏海 湖北 臨汾 聊城 云浮 白銀 武威

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySQL如何選擇合適的索引》,本文關(guān)鍵詞  MySQL,如何,選擇,合適,的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MySQL如何選擇合適的索引》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于MySQL如何選擇合適的索引的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    亚洲女人国产香蕉久久精品| 久久国产精品自线拍免费| 日本久久久久久久 97久久精品一区二区三区 狠狠色噜噜狠狠狠狠97 日日干综合 五月天婷婷在线观看高清 九色福利视频 | 欧美日本二区| 日韩在线观看视频黄| 欧美爱色| 青青久久国产成人免费网站| 日本伦理网站| 日日爽天天| 一级片免费在线观看视频| 欧美另类videosbestsex视频| 好男人天堂网 久久精品国产这里是免费 国产精品成人一区二区 男人天堂网2021 男人的天堂在线观看 丁香六月综合激情 | 一级女人毛片人一女人| 一级女性全黄生活片免费| 亚洲精品久久久中文字| 国产91精品露脸国语对白| 成人a级高清视频在线观看| 国产精品123| 999精品影视在线观看| 亚洲 欧美 成人日韩| 久久国产影院| 一本高清在线| 青青久久网| 99久久精品国产高清一区二区| 九九久久99| 欧美激情影院| 四虎久久影院| 色综合久久天天综线观看| 青草国产在线观看| 91麻豆国产| 久久精品人人做人人爽97| 免费的黄视频| 欧美激情一区二区三区视频| 黄视频网站在线看| 国产欧美精品| 久久国产一久久高清| 国产综合91天堂亚洲国产| 精品视频在线观看免费| 韩国毛片免费大片| 欧美激情伊人| 国产91素人搭讪系列天堂| 国产视频网站在线观看| 好男人天堂网 久久精品国产这里是免费 国产精品成人一区二区 男人天堂网2021 男人的天堂在线观看 丁香六月综合激情 | 精品视频免费看| 天天做人人爱夜夜爽2020| 日本在线不卡视频| 91麻豆tv| 欧美激情影院| 免费的黄视频| 一本高清在线| 国产一区二区精品久久| 成人免费观看的视频黄页| 亚洲第一视频在线播放| 99色播| 91麻豆tv| 亚洲wwwwww| 中文字幕一区二区三区精彩视频| 亚欧乱色一区二区三区| 精品国产一区二区三区久久久狼| 日韩在线观看视频网站| 欧美夜夜骑 青草视频在线观看完整版 久久精品99无色码中文字幕 欧美日韩一区二区在线观看视频 欧美中文字幕在线视频 www.99精品 香蕉视频久久 | 色综合久久天天综合观看| 黄色福利片| 国产伦理精品| 精品视频在线观看免费| 国产一区精品| 色综合久久天天综合观看| 欧美18性精品| 99久久精品国产免费| 韩国三级香港三级日本三级la | 午夜在线亚洲男人午在线| 日韩一级黄色大片| 国产视频网站在线观看| 韩国三级视频网站| 国产一区精品| 韩国毛片| 欧美国产日韩久久久| 精品毛片视频| 日韩字幕在线| 国产高清在线精品一区二区| 一本高清在线| 国产不卡在线观看| 国产91精品系列在线观看| 黄视频网站在线看| 国产视频一区在线| 精品毛片视频| 国产一区二区精品久久| 天天色色网| 九九久久99| 青青久久国产成人免费网站| 国产一级生活片| 精品视频一区二区| 成人免费高清视频| 尤物视频网站在线观看| 久久国产影视免费精品| 高清一级毛片一本到免费观看| 黄色免费三级| 久久精品免视看国产明星| 精品视频一区二区| 国产视频一区二区在线播放| 青草国产在线观看| 国产成人欧美一区二区三区的| 日本在线不卡视频| 日本久久久久久久 97久久精品一区二区三区 狠狠色噜噜狠狠狠狠97 日日干综合 五月天婷婷在线观看高清 九色福利视频 | 久久99爰这里有精品国产| 国产国语在线播放视频| 色综合久久天天综合观看| 欧美电影免费看大全| 精品视频在线观看一区二区| 免费毛片基地| 黄视频网站免费| 色综合久久天天综线观看| 91麻豆精品国产自产在线观看一区| 四虎影视久久久| 九九精品久久久久久久久| 高清一级淫片a级中文字幕| 国产原创中文字幕| 韩国三级视频网站| 久久99中文字幕久久| 日本久久久久久久 97久久精品一区二区三区 狠狠色噜噜狠狠狠狠97 日日干综合 五月天婷婷在线观看高清 九色福利视频 | 国产一区二区高清视频| 精品国产三级a| 久草免费资源| 国产美女在线一区二区三区| 国产网站免费观看| 欧美1区2区3区| 精品久久久久久影院免费| 国产伦久视频免费观看 视频| 香蕉视频久久| 美女免费精品高清毛片在线视| 深夜做爰性大片中文| 日本伦理黄色大片在线观看网站| 国产一区二区精品在线观看| 国产一区二区精品久久91| 韩国三级一区| 精品视频在线观看一区二区三区| 国产一级强片在线观看| 二级片在线观看| 日本伦理网站| 黄视频网站在线观看| 精品国产一级毛片| 天天色成人网| 国产一区精品| 精品久久久久久中文字幕2017| 成人在免费观看视频国产| 欧美爱色| 国产视频一区二区在线播放| 日本特黄特黄aaaaa大片| 国产麻豆精品高清在线播放| 国产成人精品综合久久久| 日韩中文字幕在线播放| 精品视频一区二区三区| 色综合久久天天综合绕观看| 国产综合成人观看在线| 国产精品自拍亚洲| 国产麻豆精品| 精品国产一区二区三区国产馆| 九九免费精品视频| 精品视频在线观看一区二区| 国产高清视频免费观看| 久久国产精品只做精品| 麻豆污视频| 亚洲 男人 天堂| 尤物视频网站在线观看| 999久久久免费精品国产牛牛| 国产美女在线一区二区三区| a级黄色毛片免费播放视频| 国产91精品系列在线观看| 青青久久国产成人免费网站| 一本高清在线| 成人高清视频免费观看| 亚洲www美色| 国产麻豆精品高清在线播放| 欧美国产日韩久久久| 日韩欧美一二三区| 亚洲精品久久久中文字| 久久99中文字幕| 99热精品一区| 午夜在线观看视频免费 成人| 午夜在线亚洲男人午在线| 亚洲 激情| 国产一区二区精品久久| 国产不卡在线观看视频| 国产视频在线免费观看| 麻豆系列 在线视频| 亚飞与亚基在线观看| 精品国产亚一区二区三区| 久久成人综合网| 一 级 黄 中国色 片| 999久久久免费精品国产牛牛| 国产福利免费观看| 久久国产一久久高清| 高清一级片| 超级乱淫伦动漫| 久久久成人网| 黄视频网站在线看| 欧美日本国产| 韩国毛片免费大片| 中文字幕97| 欧美电影免费|