新聞中心
一、MySQL索引介紹
索引是一種數(shù)據(jù)結(jié)構(gòu),以其特有的記錄數(shù)據(jù)的方式,為用戶提供高性能的查詢。索引就像是一本新華字典的目錄,通過目錄可以快速的找到我們想要找到的數(shù)據(jù)。
廣西ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!
二、MySQL主要使用的索引
普通索引:MySQL中基本索引類型,僅有加速查詢功能,允許在定義索引的列中插入重復(fù)的值和空值。
主鍵索引:有兩個功能:加速查詢 和 唯一約束(不可含null)
唯一索引:有兩個功能:加速查詢 和 唯一約束(可含null)
組合索引:組合索引是將n個列組合成一個索引
三、普通索引
1、創(chuàng)建索引
part1:創(chuàng)建表時創(chuàng)建索引
create table user(
nid int not null auto_increment primary key,
name varchar(50) not null,
passwd varchar(100) not null,
extra text,
index idx1(name)
)
note:如果是CHAR,VARCHAR類型,length可以小于字段實際長度;如果是BLOB和TEXT類型,必須指定 length。
part2:創(chuàng)建索引
create index idx2 on user(name);
2、查看索引
show index from user;
3、刪除索引
drop index idx2 on user;
四、唯一索引
1、創(chuàng)建索引
part1:創(chuàng)建表時創(chuàng)建索引
create table user(
nid int not null auto_increment primary key,
name varchar(50) not null,
passwd varchar(100) not null,
extra text,
unique idx1(name)
)
part2:創(chuàng)建索引
create unique idx2 on user(name);
2、查看索引
show index from user;
3、刪除索引
drop index idx2 on user;
五、主鍵索引
1、創(chuàng)建主鍵索引
part1:創(chuàng)建表時創(chuàng)建索引
create table user(
nid int not null auto_increment ,
name varchar(50) not null,
passwd varchar(100) not null,
extra text,
primary key(nid),
index idx1(name)
)
part2:創(chuàng)建索引
alter table idx2 add primary key(nid);
2、刪除主鍵
alter table user drop primary key;
alter table user modify nid int, drop primary key;
六、組合索引
1、創(chuàng)建組合索引
part1:創(chuàng)建表時創(chuàng)建索引
create table user(
nid int not null auto_increment primary key,
name varchar(50) not null,
passwd varchar(100) not null,
index idx(nid,name,passwd)
)
part2:創(chuàng)建索引
create index idx on user(nid,name,passwd);
2、查看索引
show index from user;
3、刪除索引
drop index idx on user;
組合索引查詢遵循最左匹配:如查詢:
nid and name :使用索引
nid and passwd :使用索引
passwd:不適用索引
七、覆蓋索引
上述4種MySQL常用的索引,其查找數(shù)據(jù)的順序都是先在索引中找,再去數(shù)據(jù)表中找。如果一個查詢只在索引中便能完成,而不需要去數(shù)據(jù)表中找,這種索引被稱為覆蓋索引。覆蓋索引必須要求存儲索引的列,所以只有btree索引能使用更為高效的覆蓋索引。
查看覆蓋索引(Using index)
八、執(zhí)行計劃
explain用于顯示SQL執(zhí)行信息參數(shù),根據(jù)參考信息可以進(jìn)行SQL優(yōu)化
mysql> explain select * from t1;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| 1 | SIMPLE | t1 | NULL | ALL | NULL | NULL | NULL | NULL | 2 | 100.00 | NULL |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.01 sec)
EXPLAIN列的解釋:
select_type :查詢類型
table:正在訪問的表名
type:連接類型,性能:all < index < range < index_merge < ref_or_null < ref < eq_ref < system/const
possible_keys:顯示可能應(yīng)用在這張表中的索引,也可以是where語句后合適的語句
key:實際使用的索引
key_len:實際使用的索引的字節(jié)長度
rows:為了找到所需的內(nèi)容預(yù)估要讀取的行數(shù)
extra:描述信息如下
Using index:要查詢的列數(shù)據(jù)僅使用索引中的信息,而沒有讀取數(shù)據(jù)表中的信息
Using temporary:mysql需要創(chuàng)建一個臨時表來存儲結(jié)果,這通常發(fā)生在對不同的列集進(jìn)行order by上,而不是group by上,這種情況下,需要優(yōu)化查詢
Using filesort:這意味著mysql會對結(jié)果使用一個外部索引排序,而不是按索引次序從表里讀取行。mysql有兩種文件排序算法,這兩種排序方式都可以在內(nèi)存或者磁盤上完成,explain不會告訴你mysql將使用哪一種文件排序,也不會告訴你排序會在內(nèi)存里還是磁盤上完成。
Range checked for each record(index map: #):沒有找到合適的索引,因此對從前面表中來的每一個行組合,#是顯示在possible_keys列中索引的位圖,并且是冗余的。
更多詳細(xì)信息參見:MySQL官方文檔
當(dāng)前名稱:MySQL-索引
分享網(wǎng)址:http://ef60e0e.cn/article/jigjjs.html