亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

MySQL索引優(yōu)化實(shí)例分析

 更新時(shí)間:2022年07月29日 15:19:15   作者:Hz488  
這篇文章主要介紹了MySQL索引優(yōu)化實(shí)例分析,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下

1.數(shù)據(jù)準(zhǔn)備

#1.建立員工表,并創(chuàng)建name,age,position索引,id為自增主鍵
CREATE TABLE `employees` (
 ?`id` int(11) NOT NULL AUTO_INCREMENT,
 ?`name` varchar(24) NOT NULL DEFAULT '' COMMENT '姓名',
 ?`age` int(11) NOT NULL DEFAULT '0' COMMENT '年齡',
 ?`position` varchar(20) NOT NULL DEFAULT '' COMMENT '職位',
 ?`hire_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '入職時(shí)間',
 ?PRIMARY KEY (`id`),
 ?KEY `idx_name_age_position` (`name`,`age`,`position`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=100010 DEFAULT CHARSET=utf8 COMMENT='員工記錄表'

# 2.前面插入三條數(shù)據(jù),并建立employees_min_copy表插入這三條數(shù)據(jù)
INSERT INTO employees (name,age,`position`,hire_time) VALUES 
('LiLei',22,'manager','2021-08-17 21:00:55')
,('HanMeimei',23,'dev','2021-08-17 21:00:55')
,('Lucy',23,'dev','2021-08-17 21:00:55')
;
#3.再通過(guò)執(zhí)行計(jì)劃向表中插入十萬(wàn)條數(shù)據(jù)
#3.1建立存儲(chǔ)過(guò)程,往employees表中插入數(shù)據(jù)(MySQL8.0版本)
DELIMITER $$
USE `zhebase`$$
DROP PROCEDURE IF EXISTS `batch_insert_employees`$$
CREATE PROCEDURE `batch_insert_employees`(IN `start_number` BIGINT,IN `counts` BIGINT)
BEGIN 
 ? ?DECLARE start_number BIGINT DEFAULT start_number;
 ? ?DECLARE stop_number BIGINT DEFAULT start_number;
 ? ?SET stop_number=start_number + counts;
 ? ?WHILE start_number < stop_number DO
 ? ? ? ?INSERT INTO employees(name,age,position,hire_time) VALUES(CONCAT('zhang',start_number),start_number,'dev',now());
 ? ? ? ?SET start_number=start_number+1;
 ? ?END WHILE ;
 ? ?COMMIT;
END$$
DELIMITER ;

#3.2執(zhí)行存儲(chǔ)過(guò)程插入十萬(wàn)條數(shù)據(jù)
CALL batch_insert_employees(1,100000);

2.實(shí)例一

1.聯(lián)合索引第一個(gè)字段用范圍不會(huì)走索引  

EXPLAIN SELECT * FROM employees WHERE name > 'LiLei' AND age = 22 AND position ='manager';

 原因:MySQL 內(nèi)部可能覺(jué)得第一個(gè)字段就用范圍,結(jié)果集應(yīng)該很大,還需要回表,回表效率不高,不如直接采用全表掃描 但是我們可以強(qiáng)制走索引

EXPLAIN SELECT * FROM employees force index(idx_name_age_position) WHERE name > 'LiLei' AND age = 22 AND position ='manager';

-- 關(guān)閉查詢(xún)緩存
set global query_cache_size=0;
set global query_cache_type=0;
-- 執(zhí)行時(shí)間0.321s
SELECT * FROM employees WHERE name > 'LiLei';
-- 執(zhí)行時(shí)間0.458s
SELECT * FROM employees force index(idx_name_age_position) WHERE name > 'LiLei';

 使用了強(qiáng)制走索引讓聯(lián)合索引第一個(gè)字段范圍查找也走索引,掃描的行rows看上去也少了點(diǎn),但是最終查找效率不一定比全表掃描高,因?yàn)榛乇硇什桓?/strong>

對(duì)于這種情況,如果可以使用覆蓋索引,就使用覆蓋索引進(jìn)行優(yōu)化 

EXPLAIN SELECT name,age,position FROM employees WHERE name > 'LiLei' AND age = 22 AND position ='manager';

2.in 和 or 在表數(shù)據(jù)量比較大的情況會(huì)走索引,在表記錄不多的情況下會(huì)選擇全表掃描

EXPLAIN SELECT * FROM employees
WHERE name in ('LiLei','HanMeimei','Lucy')
AND age = 22
AND position ='manager';
#表數(shù)據(jù)量大走索引,數(shù)據(jù)量小全表掃描
EXPLAIN SELECT * FROM employees
WHERE (name = 'LiLei' or name = 'HanMeimei')
AND age = 22 
AND position ='manager';

 將十萬(wàn)行數(shù)據(jù)的employees表復(fù)制一份插入幾行數(shù)據(jù),再進(jìn)行查詢(xún) 

發(fā)現(xiàn)進(jìn)行了全表掃描 

3.like xx% 無(wú)論數(shù)據(jù)量多少一般情況都會(huì)走索引

EXPLAIN SELECT * FROM employees WHERE name like 'LiLei%' AND age = 22 AND position ='manager';

 MySQL 底層使用索引下推(Index Condition Pushdown,ICP) 來(lái)對(duì) like xx%進(jìn)行優(yōu)化。

索引下推: 對(duì)于輔助的聯(lián)合索引(idx_name_age_position),通常按照最左前綴原則,SELECT * FROM employees WHERE name like 'LiLei%' AND age = 22 AND position ='manager' 因?yàn)樵?nbsp;name 是范圍查詢(xún),過(guò)濾完后,age 和 position 是無(wú)序的,后續(xù)索引無(wú)法使用,只會(huì)走name字段索引。

  • MySQL5.6 以前: 先在索引樹(shù)中匹配 name 是 'LiLei' 開(kāi)頭的索引,然后根據(jù)索引下的主鍵進(jìn)行回表操作,在主鍵索引上在匹配 age 和 position
  • MySQL 5.6以后: 引入索引下推,先在索引樹(shù)種匹配 name 是 'LiLei' 開(kāi)頭的索引,同時(shí)將該所與樹(shù)通有的所有條件字段進(jìn)行判斷,過(guò)濾掉不符合條件的記錄再回表匹配其他條件及查詢(xún)整行數(shù)據(jù)。
  • 優(yōu)點(diǎn): 過(guò)濾掉不符合條件的記錄之后再回表,可以有效的減少回表次數(shù),提高查詢(xún)效率。

MySQL 范圍查找為什么沒(méi)有使用索引下推優(yōu)化?  可能因?yàn)榉秶檎医Y(jié)果集一般較大,like xx%在大多數(shù)情況下,過(guò)濾后結(jié)果集較小。而結(jié)果集大的時(shí)候,每次檢索出來(lái)都要匹配后面的字段,不一定比立即回表要快。但是也不是絕對(duì)的,有些時(shí)候 Like xx%也不會(huì)走索引下推。

3.MySQL如何選擇合適的索引?

先來(lái)看兩條 SQL 語(yǔ)句:

# MySQL直接使用全表掃描
EXPLAIN select * from employees where name > 'a';
# MySQL走索引
EXPLAIN select * from employees where name > 'zzz';

 我們發(fā)現(xiàn)第一條 SQL 進(jìn)行了全表掃描,第二條 SQL 走了索引。對(duì)應(yīng)第一條SQL,MySQL 通過(guò)計(jì)算執(zhí)行成本發(fā)現(xiàn)走索引成本比全部掃描更高(走索引需要遍歷 name 字段,再進(jìn)行回表操作查出最終數(shù)據(jù),比直接查聚簇索引樹(shù)更慢)。對(duì)于這種情況可以使用覆蓋索引進(jìn)行優(yōu)化。至于 MySQL 如何選擇最終索引,可以用 Trace 工具進(jìn)行查看。但開(kāi)啟trace工具會(huì)影響 MySQL 性能,用完之后需立即關(guān)閉。

#開(kāi)啟trace
set session optimizer_trace="enabled=on",end_markers_in_json=on; 
#關(guān)閉trace
set session optimizer_trace="enabled=off";
#使用trace
select * from employees where name > 'a' order by position;
select * from information_schema.OPTIMIZER_TRACE;

下面是執(zhí)行后的Trace中的內(nèi)容:

{
 ?"steps": [
 ? ?{
 ? ? ?#第一階段:SQL準(zhǔn)備階段,格式化sql
 ? ? ?"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` > 'a') order by `employees`.`position` limit 0,200"
 ? ? ? ? ?}
 ? ? ? ?] /* steps */
 ? ? ?} /* join_preparation */
 ? ?},
 ? ?{
 ? ? ?#第二階段:SQL優(yōu)化階段
 ? ? ?"join_optimization": {
 ? ? ? ?"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 */
 ? ? ? ? ?},
 ? ? ? ? ?{
 ? ? ? ? ? ?"substitute_generated_columns": {
 ? ? ? ? ? ?} /* substitute_generated_columns */
 ? ? ? ? ?},
 ? ? ? ? ?{
 ? ? ? ? ? ?#表依賴(lài)詳情
 ? ? ? ? ? ?"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 */
 ? ? ? ? ?},
 ? ? ? ? ?{
 ? ? ? ? ? ?#預(yù)估表的訪(fǎng)問(wèn)成本
 ? ? ? ? ? ?"rows_estimation": [
 ? ? ? ? ? ? ?{
 ? ? ? ? ? ? ? ?"table": "`employees`",
 ? ? ? ? ? ? ? ?"range_analysis": {
 ? ? ? ? ? ? ? ? ?"table_scan": { --全表掃描情況
 ? ? ? ? ? ? ? ? ? ?"rows": 93205, --掃描行數(shù)
 ? ? ? ? ? ? ? ? ? ?"cost": 9394.9 --查詢(xún)成本
 ? ? ? ? ? ? ? ? ?} /* table_scan */,
 ? ? ? ? ? ? ? ? ?#查詢(xún)可能使用的索引
 ? ? ? ? ? ? ? ? ?"potential_range_indexes": [
 ? ? ? ? ? ? ? ? ? ?{
 ? ? ? ? ? ? ? ? ? ? ?"index": "PRIMARY", ?--主鍵索引
 ? ? ? ? ? ? ? ? ? ? ?"usable": false, -- 是否使用
 ? ? ? ? ? ? ? ? ? ? ?"cause": "not_applicable"
 ? ? ? ? ? ? ? ? ? ?},
 ? ? ? ? ? ? ? ? ? ?{
 ? ? ? ? ? ? ? ? ? ? ?#輔助索引
 ? ? ? ? ? ? ? ? ? ? ?"index": "idx_name_age_position",
 ? ? ? ? ? ? ? ? ? ? ?"usable": true,
 ? ? ? ? ? ? ? ? ? ? ?"key_parts": [
 ? ? ? ? ? ? ? ? ? ? ? ?"name",
 ? ? ? ? ? ? ? ? ? ? ? ?"age",
 ? ? ? ? ? ? ? ? ? ? ? ?"position",
 ? ? ? ? ? ? ? ? ? ? ? ?"id"
 ? ? ? ? ? ? ? ? ? ? ?] /* key_parts */
 ? ? ? ? ? ? ? ? ? ?}
 ? ? ? ? ? ? ? ? ?] /* potential_range_indexes */,
 ? ? ? ? ? ? ? ? ?"setup_range_conditions": [
 ? ? ? ? ? ? ? ? ?] /* setup_range_conditions */,
 ? ? ? ? ? ? ? ? ?"group_index_range": {
 ? ? ? ? ? ? ? ? ? ?"chosen": false,
 ? ? ? ? ? ? ? ? ? ?"cause": "not_group_by_or_distinct"
 ? ? ? ? ? ? ? ? ?} /* group_index_range */,
 ? ? ? ? ? ? ? ? ?"skip_scan_range": {
 ? ? ? ? ? ? ? ? ? ?"potential_skip_scan_indexes": [
 ? ? ? ? ? ? ? ? ? ? ?{
 ? ? ? ? ? ? ? ? ? ? ? ?"index": "idx_name_age_position",
 ? ? ? ? ? ? ? ? ? ? ? ?"usable": false,
 ? ? ? ? ? ? ? ? ? ? ? ?"cause": "query_references_nonkey_column"
 ? ? ? ? ? ? ? ? ? ? ?}
 ? ? ? ? ? ? ? ? ? ?] /* potential_skip_scan_indexes */
 ? ? ? ? ? ? ? ? ?} /* skip_scan_range */,
 ? ? ? ? ? ? ? ? ?#分析各個(gè)索引使用成本
 ? ? ? ? ? ? ? ? ?"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": 46602, --索引掃描行數(shù)
 ? ? ? ? ? ? ? ? ? ? ? ?"cost": 16311, --索引使用成本
 ? ? ? ? ? ? ? ? ? ? ? ?"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": { --最優(yōu)訪(fǎng)問(wèn)路徑
 ? ? ? ? ? ? ? ? ?"considered_access_paths": [ --最終選擇的訪(fǎng)問(wèn)路徑
 ? ? ? ? ? ? ? ? ? ?{
 ? ? ? ? ? ? ? ? ? ? ?"rows_to_scan": 93205,
 ? ? ? ? ? ? ? ? ? ? ?"filtering_effect": [
 ? ? ? ? ? ? ? ? ? ? ?] /* filtering_effect */,
 ? ? ? ? ? ? ? ? ? ? ?"final_filtering_effect": 0.5,
 ? ? ? ? ? ? ? ? ? ? ?"access_type": "scan", --訪(fǎng)問(wèn)類(lèi)型:為scan,全表掃描
 ? ? ? ? ? ? ? ? ? ? ?"resulting_rows": 46602,
 ? ? ? ? ? ? ? ? ? ? ?"cost": 9392.8,
 ? ? ? ? ? ? ? ? ? ? ?"chosen": true ?--確定選擇
 ? ? ? ? ? ? ? ? ? ?}
 ? ? ? ? ? ? ? ? ?] /* considered_access_paths */
 ? ? ? ? ? ? ? ?} /* best_access_path */,
 ? ? ? ? ? ? ? ?"condition_filtering_pct": 100,
 ? ? ? ? ? ? ? ?"rows_for_plan": 46602,
 ? ? ? ? ? ? ? ?"cost_for_plan": 9392.8,
 ? ? ? ? ? ? ? ?"chosen": true
 ? ? ? ? ? ? ?}
 ? ? ? ? ? ?] /* considered_execution_plans */
 ? ? ? ? ?},
 ? ? ? ? ?{
 ? ? ? ? ? ?"attaching_conditions_to_tables": {
 ? ? ? ? ? ? ?"original_condition": "(`employees`.`name` > 'a')",
 ? ? ? ? ? ? ?"attached_conditions_computation": [
 ? ? ? ? ? ? ? ?{
 ? ? ? ? ? ? ? ? ?"table": "`employees`",
 ? ? ? ? ? ? ? ? ?"rechecking_index_usage": {
 ? ? ? ? ? ? ? ? ? ?"recheck_reason": "low_limit",
 ? ? ? ? ? ? ? ? ? ?"limit": 200,
 ? ? ? ? ? ? ? ? ? ?"row_estimate": 46602
 ? ? ? ? ? ? ? ? ?} /* rechecking_index_usage */
 ? ? ? ? ? ? ? ?}
 ? ? ? ? ? ? ?] /* attached_conditions_computation */,
 ? ? ? ? ? ? ?"attached_conditions_summary": [
 ? ? ? ? ? ? ? ?{
 ? ? ? ? ? ? ? ? ?"table": "`employees`",
 ? ? ? ? ? ? ? ? ?"attached": "(`employees`.`name` > 'a')"
 ? ? ? ? ? ? ? ?}
 ? ? ? ? ? ? ?] /* attached_conditions_summary */
 ? ? ? ? ? ?} /* attaching_conditions_to_tables */
 ? ? ? ? ?},
 ? ? ? ? ?{
 ? ? ? ? ? ?"optimizing_distinct_group_by_order_by": {
 ? ? ? ? ? ? ?"simplifying_order_by": {
 ? ? ? ? ? ? ? ?"original_clause": "`employees`.`position`",
 ? ? ? ? ? ? ? ?"items": [
 ? ? ? ? ? ? ? ? ?{
 ? ? ? ? ? ? ? ? ? ?"item": "`employees`.`position`"
 ? ? ? ? ? ? ? ? ?}
 ? ? ? ? ? ? ? ?] /* items */,
 ? ? ? ? ? ? ? ?"resulting_clause_is_simple": true,
 ? ? ? ? ? ? ? ?"resulting_clause": "`employees`.`position`"
 ? ? ? ? ? ? ?} /* simplifying_order_by */
 ? ? ? ? ? ?} /* optimizing_distinct_group_by_order_by */
 ? ? ? ? ?},
 ? ? ? ? ?{
 ? ? ? ? ? ?"reconsidering_access_paths_for_index_ordering": {
 ? ? ? ? ? ? ?"clause": "ORDER BY",
 ? ? ? ? ? ? ?"steps": [
 ? ? ? ? ? ? ?] /* steps */,
 ? ? ? ? ? ? ?"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 */
 ? ? ? ? ?},
 ? ? ? ? ?{
 ? ? ? ? ? ?"finalizing_table_conditions": [
 ? ? ? ? ? ? ?{
 ? ? ? ? ? ? ? ?"table": "`employees`",
 ? ? ? ? ? ? ? ?"original_table_condition": "(`employees`.`name` > 'a')",
 ? ? ? ? ? ? ? ?"final_table_condition ? ": "(`employees`.`name` > 'a')"
 ? ? ? ? ? ? ?}
 ? ? ? ? ? ?] /* finalizing_table_conditions */
 ? ? ? ? ?},
 ? ? ? ? ?{
 ? ? ? ? ? ?"refine_plan": [
 ? ? ? ? ? ? ?{
 ? ? ? ? ? ? ? ?"table": "`employees`"
 ? ? ? ? ? ? ?}
 ? ? ? ? ? ?] /* refine_plan */
 ? ? ? ? ?},
 ? ? ? ? ?{
 ? ? ? ? ? ?"considering_tmp_tables": [
 ? ? ? ? ? ? ?{
 ? ? ? ? ? ? ? ?"adding_sort_to_table_in_plan_at_position": 0
 ? ? ? ? ? ? ?} /* filesort */
 ? ? ? ? ? ?] /* considering_tmp_tables */
 ? ? ? ? ?}
 ? ? ? ?] /* steps */
 ? ? ?} /* join_optimization */
 ? ?},
 ? ?{
 ? ? ?#第三階段:SQL執(zhí)行階段
 ? ? ?"join_execution": {
 ? ? ? ?"select#": 1,
 ? ? ? ?"steps": [
 ? ? ? ? ?{
 ? ? ? ? ? ?"sorting_table_in_plan_at_position": 0,
 ? ? ? ? ? ?"filesort_information": [
 ? ? ? ? ? ? ?{
 ? ? ? ? ? ? ? ?"direction": "asc",
 ? ? ? ? ? ? ? ?"table": "`employees`",
 ? ? ? ? ? ? ? ?"field": "position"
 ? ? ? ? ? ? ?}
 ? ? ? ? ? ?] /* filesort_information */,
 ? ? ? ? ? ?"filesort_priority_queue_optimization": {
 ? ? ? ? ? ? ?"limit": 200,
 ? ? ? ? ? ? ?"chosen": true
 ? ? ? ? ? ?} /* filesort_priority_queue_optimization */,
 ? ? ? ? ? ?"filesort_execution": [
 ? ? ? ? ? ?] /* filesort_execution */,
 ? ? ? ? ? ?"filesort_summary": {
 ? ? ? ? ? ? ?"memory_available": 262144,
 ? ? ? ? ? ? ?"key_size": 40,
 ? ? ? ? ? ? ?"row_size": 186,
 ? ? ? ? ? ? ?"max_rows_per_buffer": 201,
 ? ? ? ? ? ? ?"num_rows_estimate": 285696,
 ? ? ? ? ? ? ?"num_rows_found": 100003,
 ? ? ? ? ? ? ?"num_initial_chunks_spilled_to_disk": 0,
 ? ? ? ? ? ? ?"peak_memory_used": 38994,
 ? ? ? ? ? ? ?"sort_algorithm": "std::stable_sort",
 ? ? ? ? ? ? ?"unpacked_addon_fields": "using_priority_queue",
 ? ? ? ? ? ? ?"sort_mode": "<fixed_sort_key, additional_fields>"
 ? ? ? ? ? ?} /* filesort_summary */
 ? ? ? ? ?}
 ? ? ? ?] /* steps */
 ? ? ?} /* join_execution */
 ? ?}
 ?] /* steps */
}

由 Trace字段可知,全表掃描的 cost_for_plan = 9394.9 小于使用索引 cost_for_plan = 16311,故最終選擇全表掃描。

4.常見(jiàn) SQL 深入優(yōu)化

4.1.Order by與Group by優(yōu)化

# 案例1
explain select * from employees where name = 'Lucy' and position = 'dev' order by age;

分析:  案例1 由最左前綴法則分析出索引中間不能出現(xiàn)斷層,只使用了 name 索引前綴,也可以從key_len = 3n + 2 看出。age 索引列用在排序過(guò)程中,因?yàn)镋xtra字段里沒(méi)有 Using filesort 而是 Using index condition 。 

#案例2
explain select * from employees where name = 'Lucy'  order by position;

分析:  案例2 索引查詢(xún)使用了 name 索引前綴,但排序由于跳過(guò)了 age 所以Extra字段出現(xiàn)了 Using filesort 。

#案例3
explain select * from employees where name = 'Lucy' ?order by age, position;

分析:  案例3 查詢(xún)時(shí)使用了 name 索引,age 和 postion 用于排序,不會(huì)出現(xiàn) Using filesort

#案例4
explain select * from employees where name = 'Lucy' ?order by position,age;

分析:  案例4 查詢(xún)時(shí)使用了 name 索引,age 和 postion 順序與創(chuàng)建索引樹(shù)不一致,出現(xiàn)了 Using filesort

#案例5
explain
select * from employees
where name = 'Lucy'
and age = 22
order by position,age;

分析:  案例5 查詢(xún)時(shí)使用了 name 索引,age 和 postion 順序與創(chuàng)建索引樹(shù)不一致,但 name、age 為常量,MySQL 會(huì)自動(dòng)優(yōu)化,不會(huì)出現(xiàn) Using filesort

#案例6
explain select * from employees where name = 'Lucy' order byage,position desc;

分析:  案例6 排序順序一樣,但 order by 默認(rèn)升序,導(dǎo)致與索引的排序方式不同,出現(xiàn)了 Using filesort 。 MySQL8.0 以上版本有降序索引可以支持這種查詢(xún)。

#案例7
explain select * from employees where name = 'Lucy' or name = 'LiLei' order by age;

 分析:  案例7 對(duì)于排序來(lái)說(shuō),多個(gè)相等條件也是范圍查詢(xún),出現(xiàn)了 Using filesort 。

#案例8
#SQL-1
explain select * from employees where name > 'zzz' order by name;
#SQL-2
explain select * from employees where name > 'a' order by name;

  分析:  案例8 原因同前面的例子,可以使用覆蓋索引優(yōu)化。

MySQL排序總結(jié):

1、MySQL支持兩種方式的排序 filesort 和 index,Using index是指MySQL掃描索引本身完成排序。Using filesort 是指MySQL掃描聚簇索引(整張表)進(jìn)行排序。index效率高,filesort效率低。

2、order by 滿(mǎn)足兩種情況會(huì)使用 Using index(不絕對(duì))

  • a.order by 語(yǔ)句使用索引最左前列。
  • b.使用 where 子句與 order by 子句條件列組合滿(mǎn)足索引最左前列。

3、盡量在索引列上完成排序,遵循最左前綴法則。

4、如果 order by 的條件不在索引列上,就會(huì)產(chǎn)生Using filesort。

5、能用覆蓋索引盡量用覆蓋索引

6、group by 與 order by 很類(lèi)似,其實(shí)質(zhì)是先排序后分組(group by 底層:先執(zhí)行一次 order by 再進(jìn)行分組),遵照索引創(chuàng)建順序的最左前綴法則。對(duì)于group by的優(yōu)化如果不需要排序的可以加上order by null 禁止排序。注意,where高于having,能寫(xiě)在where中的限定條件就不要去having限定了。

Using filesort 文件排序原理 filesort文件排序方式有:

  • 單路排序是一次性取出滿(mǎn)足條件行的所有字段,然后在 sort buffer 中進(jìn)行排序。用trace工具得到sort_mode信息顯示< sort_key, additional_fields >或者< sort_key, packed_additional_fields >
  • 雙路排序(又叫回表排序模式) :先根據(jù)相應(yīng)的條件取出相應(yīng)的排序字段和可以直接定位行數(shù)據(jù)的行 ID,然后在 sort buffer 中進(jìn)行排序,排序完后需要再次取回其它需要的字段。用trace工具得到sort_mode信息顯示< sort_key, rowid >

MySQL 通過(guò)比較系統(tǒng)變量 max_length_for_sort_data(默認(rèn)1024字節(jié)) 的大小和需要查詢(xún)的字段總大小來(lái)判斷使用哪種排序模式。

  • 字段的總長(zhǎng)度 < max_length_for_sort_data ,使用單路排序
  • 字段的總長(zhǎng)度 >max_length_for_sort_data ,使用雙路排序
 select * from employees where name = 'Lucy' order by position;
"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": 10000, ? ? ? ? ? ? ? ? ? ? ? ? ? --預(yù)計(jì)掃描行數(shù)
 ? ? ? ? ? ? ?"examined_rows": 10000, ? ? ? ? ? ? ? ? ?--參與排序的行
 ? ? ? ? ? ? ?"number_of_tmp_files": 3, ? ? ? ? ? ? ? ?--使用臨時(shí)文件的個(gè)數(shù),如果為0代表全部使用的sort_buffer內(nèi)存排序,否則使用的磁盤(pán)文件排序
 ? ? ? ? ? ? ?"sort_buffer_size": 262056, ? ? ? ? ? ? ?--排序緩存的大小,單位Byte
 ? ? ? ? ? ? ?"sort_mode": "<sort_key, packed_additional_fields>" ? ? ? --排序方式,此處是路排序
 ? ? ? ? ? ?} /* filesort_summary */
 ? ? ? ? ?}
 ? ? ? ?] /* steps */
 ? ? ?} /* join_execution */

單路排序會(huì)把所有需要查詢(xún)的字段都放到 sort buffer 中排序,而雙路排序只會(huì)把主鍵和需要排序的字段放到 sort buffer 中進(jìn)行排序,然后再通過(guò)主鍵回到原表查詢(xún)需要的字段。

單路排序過(guò)程:

  • a.從索引 name 找到第一個(gè)滿(mǎn)足 name = 'Lucy' 條件的主鍵 id
  • b.回表根據(jù)主鍵 id 取出整行,取出所有字段的值,存入 sort_buffer 中
  • c.從索引name找到下一個(gè)滿(mǎn)足 name = 'Lucy' 條件的主鍵 id
  • d.重復(fù)步驟 2、3 直到不滿(mǎn)足 name = 'Lucy'
  • e.對(duì) sort_buffer 中的數(shù)據(jù)按照字段 position 進(jìn)行排序
  • f.返回結(jié)果

雙路排序過(guò)程:

  • a.從索引 name 找到第一個(gè)滿(mǎn)足 name ='Lucy' 的主鍵 id
  • b.根據(jù)主鍵 id 取出整行,把排序字段 position 和主鍵 id 這兩個(gè)字段放到 sort buffer 中
  • c.從索引 name 取下一個(gè)滿(mǎn)足 name = 'Lucy' 記錄的主鍵 id
  • d.重復(fù) 3、4 直到不滿(mǎn)足 name = 'Lucy'
  • e.對(duì) sort_buffer 中的字段 position 和主鍵 id 按照字段 position 進(jìn)行排序
  • f.遍歷排序好的 id 和字段 position,按照 id 的值回到原表中取出所有字段的值返回

4.2.分頁(yè)查詢(xún)優(yōu)化

 select * from employees limit 10000,10

這條 SQL 語(yǔ)句實(shí)際查詢(xún)了 10010 條記錄,然后丟棄了前面的 10000 條記錄,所以,在 數(shù)據(jù)量很大時(shí),執(zhí)行效率是非常非常低的。一般需要對(duì)分頁(yè)查詢(xún)進(jìn)行優(yōu)化。 優(yōu)化方法: 1.根據(jù)自增且連續(xù)的主鍵排序的分頁(yè)查詢(xún)

 select * from employees where id > 90000 limit 5;

當(dāng)一個(gè)表的主鍵連續(xù)且自增時(shí),可以使用該方法進(jìn)行優(yōu)化,但如果自增不連續(xù)會(huì)造成數(shù)據(jù)丟失。

2.根據(jù)非主鍵字段排序的分頁(yè)查詢(xún)

#優(yōu)化前
select * from employees ORDER BY name limit 90000,5;
#優(yōu)化后
select * from employees e 
inner join (select id from employees order by name limit 90000,5) ed 
on e.id = ed.id;

先通過(guò)排序和分頁(yè)操作先查出主鍵,然后根據(jù)主鍵查出對(duì)應(yīng)的記錄。 

4.3.join關(guān)聯(lián)查詢(xún)優(yōu)化

4.3.1.數(shù)據(jù)準(zhǔn)備

#示例表
# 創(chuàng)建t1,t2表,主鍵id,單值索引a
CREATE TABLE `t1` (
 ?`id` int(11) NOT NULL AUTO_INCREMENT,
 ?`a` int(11) DEFAULT NULL,
 ?`b` int(11) DEFAULT NULL,
 ?PRIMARY KEY (`id`),
 ?KEY `idx_a` (`a`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
create table t2 like t1;
#存儲(chǔ)過(guò)程往t1,t2表插入數(shù)據(jù)
DELIMITER $$
USE `zhebase`$$
DROP PROCEDURE IF EXISTS `batch_insert_t1`$$
CREATE PROCEDURE `batch_insert_t1`(IN `start_number` BIGINT,IN `counts` BIGINT)
BEGIN 
 ? ?DECLARE start_number BIGINT DEFAULT start_number;
 ? ?DECLARE stop_number BIGINT DEFAULT start_number;
 ? ?SET stop_number=start_number + counts;
 ? ?WHILE start_number < stop_number DO
 ? ? ? ?INSERT INTO t1(a,b) VALUES(start_number,start_number); 
 ? ? ? ?SET start_number=start_number+1; 
 ? ?END WHILE ; 
 ? ?COMMIT; 
END$$
DELIMITER ;
DELIMITER $$
USE `zhebase`$$
DROP PROCEDURE IF EXISTS `batch_insert_t2`$$
CREATE PROCEDURE `batch_insert_t2`(IN `start_number` BIGINT,IN `counts` BIGINT)
BEGIN 
 ? ?DECLARE start_number BIGINT DEFAULT start_number;
 ? ?DECLARE stop_number BIGINT DEFAULT start_number;
 ? ?SET stop_number=start_number + counts;
 ? ?WHILE start_number < stop_number DO
 ? ? ? ?INSERT INTO t2(a,b) VALUES(start_number,start_number); 
 ? ? ? ?SET start_number=start_number+1; 
 ? ?END WHILE ; 
 ? ?COMMIT; 
END$$
DELIMITER ;
#執(zhí)行存儲(chǔ)過(guò)程往t1表插入10000條記錄,t2表插入100條記錄
CALL batch_insert_t1(1,10000);
CALL batch_insert_t2(1,100);

4.3.2.MySQL 表關(guān)聯(lián)常見(jiàn)的兩種算法

  • 嵌套循環(huán)連接 Nested-Loop Join(NLJ) 算法
  • 基于塊的嵌套循環(huán)連接 Block Nested-Loop Join(BNL)算法
  • MySQL對(duì)于被驅(qū)動(dòng)表的關(guān)聯(lián)字段沒(méi)索引的關(guān)聯(lián)查詢(xún),一般都會(huì)使用 BNL 算法。如果有索引一般選擇 NLJ 算法,有索引的情況下 NLJ 算法比 BNL算法性能更高

1.嵌套循環(huán)連接 Nested-Loop Join(NLJ) 算法 原理:一次一行循環(huán)地從第一張表(驅(qū)動(dòng)表)中讀取行,在這行數(shù)據(jù)中取到關(guān)聯(lián)字段,根據(jù)關(guān)聯(lián)字段在另一張表(被驅(qū)動(dòng)表)里取出滿(mǎn)足條件的行,然后取出兩張表的結(jié)果合集。

explain select * from t1 inner join t2 on t1.a= t2.a;

 從執(zhí)行計(jì)劃可以了解的信息:

  • a.驅(qū)動(dòng)表是 t2,被驅(qū)動(dòng)表是 t1( inner join 時(shí) SQL優(yōu)化器會(huì)小表驅(qū)動(dòng)大表,外連接則根據(jù)連接類(lèi)型區(qū)分)
  • b.使用了 NLJ 算法。如果執(zhí)行計(jì)劃 Extra 中未出現(xiàn) Using join buffer 則表示使用的 join 算法是 NLJ

整個(gè)過(guò)程會(huì)讀取 t2 表的所有數(shù)據(jù)(掃描100行),然后遍歷這每行數(shù)據(jù)中字段 a 的值,根據(jù) t2 表中 a 的值索引掃描 t1 表中的對(duì)應(yīng)行(掃描100次 t1 表的索引,1次掃描可以認(rèn)為最終只掃描 t1 表一行完整數(shù)據(jù),也就是總共 t1 表也掃描了100行)。因此整個(gè)過(guò)程掃描了 200 行 。

2. 基于塊的嵌套循環(huán)連接 Block Nested-Loop Join(BNL)算法 原理:把驅(qū)動(dòng)表的數(shù)據(jù)讀入到 join_buffer 中,然后掃描被驅(qū)動(dòng)表,把被驅(qū)動(dòng)表每一行取出來(lái)跟 join_buffer 中的數(shù)據(jù)做對(duì)比

explain select * from t1 inner join t2 on t1.b= t2.b;

 整個(gè)過(guò)程對(duì)表 t1 和 t2 都做了一次全表掃描,因此掃描的總行數(shù)為10000(表 t1 的數(shù)據(jù)總量) + 100(表 t2 的數(shù)據(jù)總量) = 10100。并且 join_buffer 里的數(shù)據(jù)是無(wú)序的,因此對(duì)表 t1 中的每一行,都要做 100 次判斷,所以?xún)?nèi)存中的判斷次數(shù)是 100 * 10000= 100 萬(wàn)次(非掃描次數(shù)) 。 注意: join_buffer 的大小是由參數(shù) join_buffer_size 控制,默認(rèn)256k。如果 t2 放不下就會(huì)使用分段策略(先從 t2 表取出部分?jǐn)?shù)據(jù),比對(duì)完就清空 join_buffer,再重新拿出來(lái)余下的部分進(jìn)行比對(duì))。

被驅(qū)動(dòng)表的關(guān)聯(lián)字段無(wú)索引為什么要選擇使用 BNL 算法而不使用 NLJ 算法?          如第二條 SQL,如果使用 NLJ 算法掃描行數(shù)為 100 * 10000 = 100萬(wàn),這個(gè)是磁盤(pán)掃描。使用 BNL 算法僅需掃描 100100 行。

對(duì)于表關(guān)聯(lián) SQL 的優(yōu)化

  • 盡量少關(guān)聯(lián)(在阿里規(guī)范中,關(guān)聯(lián)表不能超過(guò)三種,可以后端代碼單獨(dú)查詢(xún),循環(huán)關(guān)聯(lián))
  • 小表驅(qū)動(dòng)大表,寫(xiě)多表連接 SQL 時(shí)如果明確知道哪張表是小表可以用straight_join寫(xiě)法固定連接驅(qū)動(dòng)方式,節(jié)約 MySQL 優(yōu)化器判斷時(shí)間.select * from t2 straight_join t1 on t2.a = t1.a; 代表指定mysql選著 t2 表作為驅(qū)動(dòng)表
  • 關(guān)聯(lián)字段加索引,大表關(guān)聯(lián)字段一定要加索引,盡量使得 MySQL 在進(jìn)行 join 操作時(shí)選擇NLJ算法
  • 多表連接是非常難以?xún)?yōu)化的,最好95%的場(chǎng)景都使用單表來(lái)完成,復(fù)雜場(chǎng)景交個(gè)JAVA代碼,大規(guī)模計(jì)算交給大數(shù)據(jù)工具,無(wú)需效率才考慮連接

4.4.in和exsits優(yōu)化

原則:小表驅(qū)動(dòng)大表

# in 先執(zhí)行括號(hào)里面的
select * from A where id in (select id from B) ?
#exists 先執(zhí)行括號(hào)外面的
#select * 可以用 select 1 替換,沒(méi)有區(qū)別
#exists 子查詢(xún)內(nèi)部會(huì)進(jìn)行優(yōu)化,并非逐條對(duì)比
#exists 子查詢(xún)往往也可以用 jion 來(lái)代替,何種最優(yōu)需要具體問(wèn)題具體分析
select * from A where exists (select 1 from B where B.id = A.id)

4.5.count(*)查詢(xún)優(yōu)化

注意:根據(jù)某個(gè)字段 count 不會(huì)統(tǒng)計(jì)字段為 null 的行

#掃描二級(jí)索引,按行累加
explain select count(1) from employees;
#掃描輔助索引按行累加(輔助索引比聚簇索引小)
explain select count(id) from employees;
#把 name 拿到內(nèi)存,不為 null 就累加
explain select count(name) from employees;
#不取值,按行累加
explain select count(*) from employees;

四條語(yǔ)句的效率幾乎可以忽略,效率對(duì)比如下: 字段有索引: count(* )≈count(1)>count(字段)>count(主鍵 id) 段)>count(主鍵 id)  字段無(wú)索引: count(*)≈count(1)>count(主鍵 id)>count(字段)

常見(jiàn)優(yōu)化方法:

  • 1.對(duì)于 MyISAM 存儲(chǔ)引擎的表做不帶 where 條件的 count 查詢(xún)性能是很高的,數(shù)據(jù)總行數(shù)直接寫(xiě)在磁盤(pán)上,查詢(xún)不需要計(jì)算。innodb 存儲(chǔ)引擎的表則不會(huì)記錄(因?yàn)橛蠱VCC機(jī)制)
  • 2.對(duì)與不用知道確切行的可以直接使用show table status,它是一個(gè)估值,使用該查詢(xún)效率很高
  • 3.將總數(shù)維護(hù)到 Redis 里面,插入或刪除表數(shù)據(jù)行的時(shí)候同時(shí)維護(hù) Redis 里的表總行數(shù) key 的計(jì)數(shù)值(用 incr 或 decr 命令),但是這種方式可能不準(zhǔn),很難保證表操作和Redis 操作的事務(wù)一致性。
  • 4.增加數(shù)據(jù)庫(kù)計(jì)數(shù)表,插入或刪除表數(shù)據(jù)行的時(shí)候同時(shí)維護(hù)計(jì)數(shù)表,且它們?cè)谕粋€(gè)事務(wù)里操作

5.索引設(shè)計(jì)原則

  • 1、代碼先行,索引后上,先開(kāi)發(fā)完主體業(yè)務(wù)代碼,再把涉及到該表相關(guān)sql都要拿出來(lái)分析之后再建立索引。
  • 2、聯(lián)合索引盡量覆蓋條件,可以設(shè)計(jì)一個(gè)或者兩三個(gè)聯(lián)合索引(單值索引要少建),讓每一個(gè)聯(lián)合索引都盡量去包含SQL語(yǔ)句里的 where、order by、group by 的字段,且這些聯(lián)合索引字段順序盡量滿(mǎn)足 SQL查詢(xún)的最左前綴原則。
  • 3、不要在小基數(shù)字段上建立索引,無(wú)法進(jìn)行快速的二分查找,不能能發(fā)揮出B+樹(shù)快速二分查找的優(yōu)勢(shì)來(lái),沒(méi)有意義
  • 4、盡量對(duì)字段類(lèi)型較小的列設(shè)計(jì)索引,盡量對(duì)字段類(lèi)型較小的列設(shè)計(jì)索引,比如 Tinyint 之類(lèi),字段類(lèi)型較小的話(huà),占用磁盤(pán)空間小,搜索的時(shí)性能更好。
  • 5、長(zhǎng)字符串可以采用前綴索引,比如針對(duì)某個(gè)字段的前20個(gè)字符建立索引,即:每個(gè)值的前20個(gè)字符放入索引樹(shù)中,搜索時(shí)會(huì)先匹配前而是個(gè)字符,再回表到聚簇索引取出來(lái)完整的 name 字段值進(jìn)行比較。但排序(order by 和 group by)時(shí)無(wú)法使用該索引。
  • 6、where 與 order by 沖突時(shí)優(yōu)先 where,大多數(shù)情況下根據(jù)索引進(jìn)行 where 篩選一般篩選出來(lái)的數(shù)據(jù)比較少,然后做排序成本會(huì)更低。
  • 7、基于慢SQL查詢(xún)做優(yōu)化,可以根據(jù)監(jiān)控后臺(tái)的一些慢SQL,針對(duì)這些慢 SQL 查詢(xún)做特定的索引優(yōu)化(MySQL有提供,只需設(shè)置具體參數(shù))。

到此這篇關(guān)于MySQL索引優(yōu)化實(shí)例分析的文章就介紹到這了,更多相關(guān)MySQL索引優(yōu)化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論