mysql select_type simple_mysql explain
前言
數據準備
mysql優化器在數據量不同的情況下,也會到結果產生影響
create table `user`
(
`id` bigint(20) unsigned not null auto_increment,
`name` varchar(64) default null,
`age` bigint(20) unsigned default '0',
`param` varchar(32) default null,
`a` int(11) default '0',
`b` int(11) default '0',
`c` int(11) default '0',
primary key (`id`),
key `idx_a_b_c_d` (`a`, `b`, `c`),
key `idx_age` (`age`),
key `idx_name` (`name`)
);
create table `order_info`
(
`id` bigint(16) unsigned not null auto_increment,
`ref_user_id` bigint(20) unsigned not null,
`serial_no` varchar(32) not null,
primary key (`id`),
key `uk_user_id` (`ref_user_id`)
);
INSERT INTO hlj_sql.user (id, name, age, param, a, b, c) VALUES (1, 'xiaoming', 11, 'a', 1, 2, 3);
INSERT INTO hlj_sql.user (id, name, age, param, a, b, c) VALUES (2, 'xiaohong', 23, 'b', 21, 21, 21);
INSERT INTO hlj_sql.user (id, name, age, param, a, b, c) VALUES (3, 'liuqiangdong', 45, 'c', 56, 23, 23);
INSERT INTO hlj_sql.user (id, name, age, param, a, b, c) VALUES (4, 'mayun', 123, 'd', 45, 12, 3);
INSERT INTO hlj_sql.user (id, name, age, param, a, b, c) VALUES (5, 'leijun', 5, 'e', 12, 322, 1);
INSERT INTO hlj_sql.order_info (id, ref_user_id, serial_no) VALUES (1, 1, '2');
INSERT INTO hlj_sql.order_info (id, ref_user_id, serial_no) VALUES (2, 2, '3');
INSERT INTO hlj_sql.order_info (id, ref_user_id, serial_no) VALUES (3, 3, '5');
INSERT INTO hlj_sql.order_info (id, ref_user_id, serial_no) VALUES (4, 2, '3');
INSERT INTO hlj_sql.order_info (id, ref_user_id, serial_no) VALUES (5, 4, '3');
INSERT INTO hlj_sql.order_info (id, ref_user_id, serial_no) VALUES (6, 5, '6');
INSERT INTO hlj_sql.order_info (id, ref_user_id, serial_no) VALUES (7, 4, '3');
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
參數
含義
id
查詢的標識符
select_type
SELECT 查詢的類型.
table
查詢的是哪個表
partitions
匹配的分區
type
判斷是什么掃描查詢 比如:ALL,Index,Rank
possible_keys
可能選用的索引
key
確切使用到的索引
key_len
索引長度(通過觀察這個可以判斷聯合索引使用了幾列,很有用)
ref
哪個字段或常數與 key 一起被使用
rows
顯示此查詢一共掃描了多少行. 這個是一個估計值.
filtered
表示此查詢條件所過濾的數據的百分比
extra
額外的信息
1、select_type
select_type
說明
SIMPLE
簡單查詢
UNION
聯合查詢
SUBQUERY
子查詢
UNION RESULT
聯合查詢的結果
PRIMARY
最外層查詢
1.1、SIMPLE簡單查詢
解釋:此查詢不包含 UNION 查詢或子查詢
explain select * from user ;
1
id
select_type
table
partitions
type
possible_keys
key
key_len
ref
rows
filtered
Extra
1
SIMPLE
user
NULL
ALL
NULL
NULL
NULL
NULL
5
100
NULL
1.2、UNION聯合查詢
解釋:表示此查詢是 UNION 的第二或隨后的查詢
explain select * from user union select * from user ;
id = 1 PRIMARY 外層查詢
id = 2 UNION 聯合查詢
id = 3 UNION RESULT 很明顯為聯合查詢的結果
1
2
3
4
5
id
select_type
table
partitions
type
possible_keys
key
key_len
ref
rows
filtered
Extra
1
PRIMARY
user
NULL
ALL
NULL
NULL
NULL
NULL
5
100
NULL
2
UNION
user
NULL
ALL
NULL
NULL
NULL
NULL
5
100
NULL
NULL
UNION RESULT
NULL
ALL
NULL
NULL
NULL
NULL
NULL
NULL
Using temporary
1.3、SUBQUERY子查詢
explain
select *
from order_info o
where id > (select b.id from user b where b.id = 1);
1、第一個 select 為 PRIMARY 最外層查詢
2、第二個 select 為 SUBQUERY 子查詢
1
2
3
4
5
6
7
8
id
select_type
table
partitions
type
possible_keys
key
key_len
ref
rows
filtered
Extra
1
PRIMARY
o
NULL
ALL
PRIMARY
NULL
NULL
NULL
7
85.71
Using where
2
SUBQUERY
b
NULL
const
PRIMARY
PRIMARY
8
const
1
100
Using index
1.4、UNION RESULT聯合查詢的結果
解釋:在1.2中介紹過了
1.5、PRIMARY最外層查詢
解釋:在1.2和1.3中介紹過
2、type
解釋:它提供了判斷查詢是否高效的重要依據依據. 通過type字段, 我們判斷此次查詢是全表掃描還是索引掃描等,要和Extra同時觀察會更好
性能:ALL < index < range ~ index_merge < ref < eq_ref < const < system
2.1、ALL
解釋:全表掃描
explain select * from user ;
1
id
select_type
table
partitions
type
possible_keys
key
key_len
ref
rows
filtered
Extra
1
SIMPLE
user
NULL
ALL
NULL
NULL
NULL
NULL
5
100
NULL
2.2、index
解釋:表示全索引掃描 (索引覆蓋)和ALL類似
1、index: 表示全索引掃描, 和 ALL 類型類似, 只不過 ALL 類型是全表掃描,而 index 類型則僅僅掃描所有的索引, 而不掃描數據. 其實就是講 查詢條件 寫上索引的字段
2、index類型通常出現在:所要查詢的數據直接在索引樹中就可以獲取到, 而不需要掃描數據.當是這種情況時, Extra 字段 會顯示Using index
3、index類型的查詢雖然不是全表掃描, 但是它掃描了所有的索引, 因此比 ALL 類型的稍快.
explain select name from user ;
1
id
select_type
table
partitions
type
possible_keys
key
key_len
ref
rows
filtered
Extra
1
SIMPLE
user
NULL
index
NULL
idx_name
195
NULL
5
100
Using index
2.3、range
解釋:索引范圍內查詢,通過索引字段范圍獲取表中部分數據記錄. 這個類型通常出現在 =, <>, >, >=, , BETWEEN, IN() 操作中
explain select * from user where id > 2;
1
id
select_type
table
partitions
type
possible_keys
key
key_len
ref
rows
filtered
Extra
1
SIMPLE
user
NULL
range
PRIMARY
PRIMARY
8
NULL
3
100
Using index condition
2.4、INDEX_MERGE
解釋:合并索引,使用多個單列索引搜索
explain select id from user where id = 2 or name = 'xiaoming';
1
id
select_type
table
partitions
type
possible_keys
key
key_len
ref
rows
filtered
Extra
1
SIMPLE
user
NULL
index_merge
PRIMARY,idx_name
PRIMARY,idx_name
8,195
NULL
2
100
Using union(PRIMARY,idx_name); Using where
2.5、REF
解釋:根據索引查找一個或多個值
explain select id from user where name = 'xiaoming';
1
id
select_type
table
partitions
type
possible_keys
key
key_len
ref
rows
filtered
Extra
1
SIMPLE
user
NULL
ref
idx_name
idx_name
195
const
1
100
NULL
2.6、eq_ref
解釋:連接join查詢時,使用primary key 或 unique類型,其實就是說索引唯一的關聯查詢
explain
select *
from order_info o
join user u on u.id = o.ref_user_id;
1
2
3
4
id
select_type
table
partitions
type
possible_keys
key
key_len
ref
rows
filtered
Extra
1
SIMPLE
o
NULL
ALL
uk_user_id
NULL
NULL
NULL
7
100
NULL
1
SIMPLE
u
NULL
eq_ref
PRIMARY
PRIMARY
8
hlj_sql.o.ref_user_id
1
100
NULL
2.7、const
解釋:針對主鍵或唯一索引的等值查詢掃描,只有一行
explain select id from user where id = 1;
1
id
select_type
table
partitions
type
possible_keys
key
key_len
ref
rows
filtered
Extra
1
SIMPLE
user
NULL
const
PRIMARY
PRIMARY
8
const
1
100
Using index
2.8、system
解釋:表中僅僅有一條數據,這個是特殊的const查詢
3、possible_keys
可能用到的索引,看 4
4、key
**解釋:表示 MySQL 在查詢時, 真實使用到的索引, **
即使有些索引在possible_keys中出現, 但是并不表示此索引會真正地被 MySQL 使用到. MySQL 在查詢時具體使用了哪些索引, 由key字段決定…
下面這個條件中使用了 索引id 和 聯合索引 ref_user_id
實際上我們只使用了 索引id 進行查詢,所以 key是id ,possible_keys 是id和 ref_user_id
explain select id from order_info where id = 1 and ref_user_id = 1;
1
2
3
4
id
select_type
table
partitions
type
possible_keys
key
key_len
ref
rows
filtered
Extra
1
SIMPLE
order_info
NULL
const
PRIMARY,uk_user_id
PRIMARY
8
const
1
100
NULL
5、key_len
解釋: 使用索引字節長度,這個字段可以評估聯合索引是否完全被使用
5.1、字符串
類型
索引長度
char(n)
n
varchar(n)
如果是 utf8,3 n + 2
varchar(n)
如果是 utf8mb4 ,則是 4 n + 2 字節.
5.2、數值類型:
類型
索引長度
TINYINT
1
SMALLINT
2
MEDIUMINT
3
INT
4
BIGINT
8
float
4
double
8
decimal
5.3、時間類型
類型
長度
year
1
date
4
time
3
datetime
8
timestamp
4
6、rows
顯示此查詢一共掃描了多少行. 這個是一個估計值.
7、Extra
解釋 : 額外信息,優化器會在索引存在的情況下,通過符合 RANGE 范圍的條數和總數的比例來選擇是使用索引還是進行全表遍歷,
具體案例例具體分析,不要把這里想復雜了,就是一個額外的信息而已
名詞解釋:
回表:表示即使使用索引篩選了,但是查詢的字段不是全部都是索引列
Extra
說明
NULL
查詢的不全都是索引
using index
使用覆蓋索引的時候就會出現
using index condition
查詢條件是索引的一個范圍
using where
查詢條件包含普通的條件
Using filesort
排序 不能通過索引達到排序效果
using index>using where>using index condition,如果不需要回表查詢數據,效率上應該比較快的
總結
以上是生活随笔為你收集整理的mysql select_type simple_mysql explain的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CSS 3 五光十色的变色龙动画的制作
- 下一篇: 安防领域名词explain