1.1 索引的作用
類似于一本書的目錄,起到優(yōu)化查詢的功能
1.2 索引的類型(筆試)
BTREE索引 (最常用)
RTREE索引
HASH索引
全文索引
1.3 BTREE索引的細(xì)分類(算法)
B-TREE
B+TREE
B*TREE(默認(rèn))
1.4 BTREE索引的功能分類
聚集索引(集群索引)
輔助索引(二級索引) ※※※※
1.5 Btree是如何構(gòu)建的扭勉?
1.5.1 輔助索引
建立索引:
alter table t1 add index idx_name(name);
1>將name列的所有值取出來,進(jìn)行自動排序
2>將排完序的值均勻的落到16KB葉子節(jié)點數(shù)據(jù)頁中,并將索引鍵值所對應(yīng)的數(shù)據(jù)行的聚集索引列值
3>向上生成枝節(jié)點和根節(jié)點
1.5.2 聚集索引
1>默認(rèn)是按照主鍵生成聚集索引.沒有主鍵,存儲引擎會使用唯一鍵;如果都沒有,會自動生成隱藏的聚集索引.
2>數(shù)據(jù)在存儲是,就會按照聚集索引的順序存儲到磁盤的數(shù)據(jù)頁.
3>由于本身數(shù)據(jù)就是有序的,所以聚集索引構(gòu)建時,不需要進(jìn)行排序.
4>聚集索引直接將磁盤的數(shù)據(jù)頁,作為葉子節(jié)點.
5>枝節(jié)點和根節(jié)點只會調(diào)取下層節(jié)點主鍵的最小值
1.5.3 輔助索引和聚集索引的區(qū)別吠裆?(重點)
1.6 輔助索引細(xì)分
單列輔助索引
聯(lián)合索引
唯一索引
1.7 索引樹的高度(越低越好)
1.7.1 表的數(shù)據(jù)量級大
(1)分區(qū)表
(2)分庫分表(分布式架構(gòu))----目前最流行的
1.7.2 索引鍵值的長度
(1)盡可能的選擇列值短的列創(chuàng)建索引
(2)采用前綴索引
1.7.3 數(shù)據(jù)類型的選擇(選擇合適)
1.8 索引的管理
1.8.1 準(zhǔn)備壓力測試的數(shù)據(jù)
create database test charset utf8mb4;
use test;
create table t100w (id int,num int,k1 char(2),k2 char(4),dt timestamp);
delimiter //
create procedure rand_data(in num int)
begin
declare str char(62) default 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
declare str2 char(2);
declare str4 char(4);
declare i int default 0;
while i<num do
set str2=concat(substring(str,1+floor(rand()*61),1),substring(str,1+floor(rand()*61),1));
set str4=concat(substring(str,1+floor(rand()*61),2),substring(str,1+floor(rand()*61),2));
set i=i+1;
insert into t100w values (i,floor(rand()*num),str2,str4,now());
end while;
end;
//
delimiter ;
插入100w條數(shù)據(jù):
call rand_data(1000000);
commit;
1.8.2 進(jìn)行壓力測試
1>沒有添加索引之前的測試
[root@db01 ~]# mysqlslap --defaults-file=/etc/my.cnf --concurrency=100 --iterations=1 --create-schema='test' --query="select * from test.t100w where k2='LMhi'" engine=innodb --number-of-queries=2000 -uroot -p123 -verbose
mysqlslap: [Warning] Using a password on the command line interface can be insecure.
Benchmark
Running for engine rbose
Average number of seconds to run all queries: 683.581 seconds
Minimum number of seconds to run all queries: 683.581 seconds
Maximum number of seconds to run all queries: 683.581 seconds
Number of clients running queries: 100
Average number of queries per client: 20
[root@db01 ~]#
2>添加索引之后的測試
alter table t100w add index idx_k2(k2);
[root@db01 ~]# mysqlslap --defaults-file=/etc/my.cnf --concurrency=100 --iterations=1 --create-schema='test' --query="select * from test.t100w where k2='LMhi'" engine=innodb --number-of-queries=2000 -uroot -p123 -verbose
mysqlslap: [Warning] Using a password on the command line interface can be insecure.
Benchmark
Running for engine rbose
Average number of seconds to run all queries: 4.542 seconds
Minimum number of seconds to run all queries: 4.542 seconds
Maximum number of seconds to run all queries: 4.542 seconds
Number of clients running queries: 100
Average number of queries per client: 20
[root@db01 ~]#
1.8.2 索引命令操作
1>查詢索引
desc student;
show index from student\G
Key:
??PRI(主鍵)
??UNI(唯一索引)
??MUL(輔助索引)
2>創(chuàng)建索引
(1)創(chuàng)建單列索引:
alter table student add index idx_name(sname);
(2)創(chuàng)建聯(lián)合索引:
alter table student add index idx_sname_sage_ssex(sname,sage,ssex);
(3)創(chuàng)建前綴索引:
alter table student add index idx_sname(sname(5));
(4)創(chuàng)建唯一索引:
alter table student add unique index idx_tel(telnum);
3>刪除索引
alter table student drop index idx_name;
1.9 explain(desc)-----重點
1.9.1 作用:
抓取優(yōu)化器優(yōu)化過的執(zhí)行計劃
1.9.2 執(zhí)行計劃的分析
wenjuan[test]>wenjuan[test]>desc select * from t100w where k2 != 'asdf';
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+
| 1 | SIMPLE | t100w | NULL | ALL | idx_k2 | NULL | NULL | NULL | 997381 | 86.34 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
wenjuan[test]>
說明:
table:以上SQL語句涉及到的表 ***
type:查詢的類型(全表掃描---ALL伐谈、索引掃描、查不到數(shù)據(jù)---NULL) *****
possible_keys:可能會用到的索引 ***
key:使用到的索引 ****
key_len:索引的覆蓋長度 *****
Extra:額外的信息 ****
1.9.3 type詳細(xì)說明(重點)
1> ALL: 全表掃描 , 不會走任何索引
(1)查詢條件,沒建索引
oldguo[test]>explain select * from test.t100w where k2='VWtu'
oldguo[test]>desc t100w;
+-------+-----------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------+------+-----+-------------------+-----------------------------+
| id | int(11) | YES | | NULL | |
| num | int(11) | YES | | NULL | |
| k1 | char(2) | YES | | NULL | |
| k2 | char(4) | YES | | NULL | |
| dt | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------+-----------+------+-----+-------------------+-----------------------------+
(2)有索引不走
以下這幾種情況:
desc select * from t100w where k2 != 'asdf';
desc select * from t100w where k2 like '%aa%';
desc select * from t100w where k2 not in ('asda','asas');
desc select * from t100w;
desc select * from t100w where 1=1;
注意: !=和not in 如果是主鍵列,是走range.
2> index 全索引掃描
wenjuan[test]>desc select k2 from t100w;
+----+-------------+-------+------------+-------+---------------+--------+---------+------+--------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+--------+---------+------+--------+----------+-------------+
| 1 | SIMPLE | t100w | NULL | index | NULL | idx_k2 | 17 | NULL | 997381 | 100.00 | Using index |
+----+-------------+-------+------------+-------+---------------+--------+---------+------+--------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
wenjuan[test]>
===========從range開始,索引才有價值的============
3> range 索引范圍查詢
(1)所有索引:> ,<, >=, <= ,like , between and
wenjuan[world]>desc select * from city where id<10;
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| 1 | SIMPLE | city | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 9 | 100.00 | Using where |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
wenjuan[world]>
wenjuan[world]>desc select * from city where countrycode like 'CH%';
+----+-------------+-------+------------+-------+---------------+-------------+---------+------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+-------------+---------+------+------+----------+-----------------------+
| 1 | SIMPLE | city | NULL | range | CountryCode | CountryCode | 3 | NULL | 397 | 100.00 | Using index condition |
+----+-------------+-------+------------+-------+---------------+-------------+---------+------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)
wenjuan[world]>
in , or
wenjuan[world]>desc select * from city where countrycode in ('CHN','USA');
+----+-------------+-------+------------+-------+---------------+-------------+---------+------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+-------------+---------+------+------+----------+-----------------------+
| 1 | SIMPLE | city | NULL | range | CountryCode | CountryCode | 3 | NULL | 637 | 100.00 | Using index condition |
+----+-------------+-------+------------+-------+---------------+-------------+---------+------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)
wenjuan[world]>
(2)聚集索引:!= 试疙、not in
wenjuan[test]>desc select * from world.city where id != 10;
wenjuan[test]>desc select * from world.city where id not in (10,20);
說明:
- B+tree 索引能額外優(yōu)化到:> ,<, >=, <= ,like , between and
- in 和 or 享受不到b+tree額外的優(yōu)化效果的,所以我一般情況會將in , or 進(jìn)行改寫:
desc select * from city where countrycode='CHN' union all select * from city where countrycode='USA'; wenjuan[world]>desc select * from city where countrycode='CHN' -> union all -> select * from city where countrycode='USA'; +----+-------------+-------+------------+------+---------------+-------------+---------+-------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+-------------+---------+-------+------+----------+-------+ | 1 | PRIMARY | city | NULL | ref | CountryCode | CountryCode | 3 | const | 363 | 100.00 | NULL | | 2 | UNION | city | NULL | ref | CountryCode | CountryCode | 3 | const | 274 | 100.00 | NULL | +----+-------------+-------+------------+------+---------------+-------------+---------+-------+------+----------+-------+ 2 rows in set, 1 warning (0.00 sec) wenjuan[world]>
4> ref(輔助索引等值查詢):
wenjuan[world]>desc select * from city where countrycode='CHN';
+----+-------------+-------+------------+------+---------------+-------------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+-------------+---------+-------+------+----------+-------+
| 1 | SIMPLE | city | NULL | ref | CountryCode | CountryCode | 3 | const | 363 | 100.00 | NULL |
+----+-------------+-------+------------+------+---------------+-------------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)
wenjuan[world]>
5> eq_ref:多表連接查詢中,非驅(qū)動表on的條件列是主鍵或者唯一鍵
wenjuan[world]>desc select a.name,b.name from city as a join country as b on a.countrycode=b.code where a.population<100;
6> const(system):主鍵或唯一鍵的等值
wenjuan[world]>desc select * from city where id=10;
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| 1 | SIMPLE | city | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)
wenjuan[world]>
7> NULL: 獲取不到數(shù)據(jù)
wenjuan[world]>desc select * from city where id=100000000000000;
1.9.4 possible_keys(可能會用到的索引)
NULL:沒有和查詢條件匹配的索引條目
有值:有查詢條件匹配的索引條目诵棵,但是沒走,大部分原因是語句查詢方式不符合索引應(yīng)用條件
1.9.5 key(使用到的索引)
最終使用的索引效斑,可以幫助我們判斷是否走了合適的索引
1.9.6 key_len(索引的覆蓋長度):在聯(lián)合索引應(yīng)用的判斷時非春,會經(jīng)常看
字符集 | 一個字符最大存儲長度占幾個字節(jié) |
---|---|
utf8 | 3 |
utfmb4 | 4 |
不同類型索引 | 有not nulll時(非空) | 沒有指定not null時(為空) |
---|---|---|
int | 最大4個字節(jié) | 最大4+1個字節(jié) |
tinyint | 最大1個字節(jié) | 最大1+1個字節(jié) |
char(10) | 最大4*10個字節(jié) | 最大4*10+1個字節(jié) |
varchar(10) | 最大4*10+2個字節(jié) | 最大4*10+2+1個字節(jié) |
說明:
- 有非空約束時,key_length就是最大字節(jié)長度
- 在沒有非空約束時: 字符最大長度+1
- varchar類型,需要額外在最大字符長度+2(存儲字符長度的最長度占位)
聯(lián)合索引準(zhǔn)備:
-- 創(chuàng)建測試表
create table t1(a int not null ,b char(10) not null ,c varchar(10) not null )charset utf8mb4;
wenjuan[test]>desc t1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| a | int(11) | NO | | NULL | |
| b | char(10) | NO | | NULL | |
| c | varchar(10) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
wenjuan[test]>
-- 創(chuàng)建索引
wenjuan[test]>alter table t1 add index idx(a,b,c);
Query OK, 0 rows affected (0.25 sec)
Records: 0 Duplicates: 0 Warnings: 0
wenjuan[test]>show index from t1;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| t1 | 1 | idx | 1 | a | A | 0 | NULL | NULL | | BTREE | | |
| t1 | 1 | idx | 2 | b | A | 0 | NULL | NULL | | BTREE | | |
| t1 | 1 | idx | 3 | c | A | 0 | NULL | NULL | | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)
wenjuan[test]>
(1)最完美的查詢情況
desc select * from t1 where a=1 and b='a' and c='a';
desc select * from t1 where b='1' and a=1 and c='a';
desc select * from t1 where c='1' and a=1 and b='a';
desc select * from t1 where c='1' and b='a' and a=1;
desc select * from t1 where a=1 and c='a' and b='a';
desc select * from t1 where b='1' and c='a' and a=1;
結(jié)論:
當(dāng)查詢條件中,包含了索引列中所有的列條件,并且都是等值的查詢,那么無關(guān)排列順序,都可以走全聯(lián)合索引優(yōu)化;
原因:優(yōu)化器會自動調(diào)整順序,達(dá)到最佳的優(yōu)化效果缓屠。所以,我們重點需要關(guān)注的是聯(lián)合索引建立的順序,從左到右,唯一值越多的列放在最左邊
(2)部分索引(查詢條件中,哪些因素會key_len長度)
--------1>按照索引的建立順序,在查詢條件中,少了任意一個中間列,后續(xù)列都無法走索引
wenjuan[test]>desc select * from t1 where a=1 and c='a';
+----+-------------+-------+------------+------+---------------+------+---------+-------+------+----------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+-------+------+----------+--------------------------+
| 1 | SIMPLE | t1 | NULL | ref | idx | idx | 4 | const | 1 | 100.00 | Using where; Using index |
+----+-------------+-------+------------+------+---------------+------+---------+-------+------+----------+--------------------------+
1 row in set, 1 warning (0.00 sec)
wenjuan[test]>
wenjuan[test]>desc select * from t1 where a=1 and b like 'a%' and c='a';
-------2>在條件查詢中間,出現(xiàn)不等值查詢時,從不等值列開始,所有列都無法使用聯(lián)合索引 (暫存)
優(yōu)化方法:將不等值列放在最后.
-------3>如果有多子句的條件查詢(必須是聯(lián)合索引)护侮,按照子句的執(zhí)行順序,建立聯(lián)合索引.
1.9.7 Extra:額外的信息
using filesort ===> 排序不走索引,走的額外排序
wenjuan[(none)]>use world;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
wenjuan[world]>desc select * from world.city where countrycode='CHN' order by population;
+----+-------------+-------+------------+------+---------------+-------------+---------+-------+------+----------+---------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+-------------+---------+-------+------+----------+---------------------------------------+
| 1 | SIMPLE | city | NULL | ref | CountryCode | CountryCode | 3 | const | 363 | 100.00 | Using index condition; Using filesort |
+----+-------------+-------+------------+------+---------------+-------------+---------+-------+------+----------+---------------------------------------+
1 row in set, 1 warning (0.00 sec)
wenjuan[world]>
原因:在 group by ,order by,distinct等.
一般優(yōu)化的方法是,和where條件的列建立聯(lián)合索引
擴(kuò)展:
以json的方式顯示執(zhí)行計劃敌完,可以通過used_key_parts查看使用的索引部分
wenjuan[test]>desc format=json select * from t1 where b='1' and c='a' and a=1;
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| EXPLAIN |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| {
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "1.20"
},
"table": {
"table_name": "t1",
"access_type": "ref",
"possible_keys": [
"idx"
],
"key": "idx",
"used_key_parts": [
"a",
"b",
"c"
],
"key_length": "86",
"ref": [
"const",
"const",
"const"
],
"rows_examined_per_scan": 1,
"rows_produced_per_join": 1,
"filtered": "100.00",
"using_index": true,
"cost_info": {
"read_cost": "1.00",
"eval_cost": "0.20",
"prefix_cost": "1.20",
"data_read_per_join": "88"
},
"used_columns": [
"a",
"b",
"c"
]
}
}
} |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)
wenjuan[test]>
未完……