阳神,指尖,澳门旅游攻略-哥哥新闻,大哥哥的眼光还原事情的真相

admin 2周前 ( 10-28 11:57 ) 0条评论
摘要: MySQL如何选择合适的索引...

小强现已写过几篇mysql的底层数据结构剖析和sql优化的文章,现已给你都挑选出出来了.

工作中遇到的99%SQL优化,这儿都能给你解决方案深化了解Mysql索引底层数据结构与算法

Mysql功能优化神器-Explain

接下来以一个栗子开端今日的内容:

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; --敞开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优化阶段
"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_an徐丽萩莎alysis */
}
] /* rows_estimation */
},
{
"considered_execution_plans": [
{
"plan_prefix": [
] /* plan_prefix */,
"table": "`employees`",
"best_access_path": {
"considered_acces秋千门s_宝骏830paths": [
{
"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_tracobsummary": {
"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"
}
] /* fil易阳指电脑版esort_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": ""
} /* 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": [
{
"tab邪修花尊le": "`employees`",
"row_may_be_null": false,
"map_bit": 0,
"depends_on_map_bits": [
] /* depends_on_m阳神,指尖,澳门旅行攻略-哥哥新闻,大哥哥的眼光复原工作的本相ap_bits */
}
] /* table_dependencies */
},
{
"ref_optimizer_key_uses": [
] /* ref_optimizOOfulier_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 */
} /* rang一角书屋e_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
}
] /* considere盗墓天道体系d_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_na变形计20140623me_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_queu茶浴炉e_optimization */,
"filesort_execution": [
] /* filesort_execution */,
"filesort_summary": {
"rows": 0,
"examined_rows": 0,
"number_of_tmp_files": 0,
"sort_buffer_size": 200704,
"sort_mode": ""
} /* filesort_summary */
}
] /* steps */
} /* join_execution */
}
] /* steps */
}

检查trace字段可知索引扫描的本钱低于全表扫描的本钱,所以MySQL终究挑选索引扫描。

SET SESSION optimizer_trace="enabled=off"; -- 封闭trace

》》》比较欣赏,更喜爱被转发

文章版权及转载声明:

作者:admin本文地址:http://www.geogianews.com/articles/4080.html发布于 2周前 ( 10-28 11:57 )
文章转载或复制请以超链接形式并注明出处哥哥新闻,大哥哥的眼光还原事情的真相