侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

MySQL如何选择合适的索引

数据库  /  管理员 发布于 6年前   204

先来看一个栗子

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

如果用name索引查找数据需要遍历name字段联合索引树,然后根据遍历出来的主键值去主键索引树里再去查出最终数据,成本比全表扫描还高。

可以用覆盖索引优化,这样只需要遍历name字段的联合索引树就可以拿到所有的结果。

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

可以看到通过select出的字段是覆盖索引,MySQL底层使用了索引优化。在看另一个case:

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

对于上面的这两种 name>'a' 和 name>'zzz'的执行结果, mysql最终是否选择走索引或者一张表涉及多个索引, mysql最终如何选择索引,可以通过trace工具来一查究竟,开启trace工具会影响mysql性能,所以只能临时分析sql使用,用完之后需要立即关闭。

SET SESSION optimizer_trace="enabled=on",end_markers_in_json=on; --开启traceSELECT * 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优化阶段  "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, --扫描行数     "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, --\\索引扫描行数      "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执行阶段  "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如何选择合适的索引,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对站的支持!如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!


  • 上一条:
    当Mysql行锁遇到复合主键与多列索引详解
    下一条:
    mysql类似oracle rownum写法实例详解
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客