索引测试
- 通过存储过程往数据库中插入300w 条数据
- 分别测试使用索引和没有使用索引的情况下,where 查询的一个效率对比。
-- 创建表
DROP TABLE IF EXISTS person;
CREATE TABLE person(
PID int(11) auto_increment comment '编号',
PNAME varchar(50) comment '姓名',
PSEX varchar(10) comment '性别',
PAGE int(11) comment '年龄',
SAL decimal(7, 2) comment '工资',
PRIMARY KEY (PID)
);
# 创建存储过程
create procedure insert_person(in max_num int(10))
begin
declare i int default 0
set autocommit = 0;
repeat
set i = i + 1;
insert into person (PID,PNAME,PSEX,PAGE,SAL) values (i, concat('test', floor(rand()*10000000)),IF(RAND()>0.5,'男','女'), FLOOR((RAND()*100)+10),FLOOR((RAND()*19000)+1000));
until i = max_num
end repeat;
commit;
end;
-- 调用存储过程
call insert_person(3000000);
-- 不使用索引,根据PNAME进行查询
select * from person where PNAME = 'test1209325'; #10.813秒
-- 给PNAME建立普通索引
alter table person add index idx_pname(PNAME);
select * from person where PNAME = 'test1209325'; #5.032秒
-- 主键索引
select * from person where PID = 2800000; #0.021秒
explain 关键字
使用EXPLAIN关键字可以模拟优化器执行SQL查询语句,从而知道MySQL是如何处理SQL语句的。可以用来分析查询语句或是表的结构的性能瓶颈。其作用如下:
- 表的读取顺序
- 哪些索引可以使用
- 数据读取操作的操作类型
- 哪些索引被实际使用
- 表之间的引用
- 每张表有多少行被优化器查询
EXPLAIN 关键字使用起来比较简单: explain + SQL语句
准备测试数据:
-- 创建四张测试表
CREATE TABLE t1(
id int(10) auto_increment,
content varchar(100),
primary key (id)
);
CREATE TABLE t2(
id int(10) auto_increment,
content varchar(100),
primary key (id)
);
CREATE TABLE t3(
id int(10) auto_increment,
content varchar(100),
primary key (id)
);
CREATE TABLE t4(
id int(10) auto_increment,
content varchar(100),
primary key (id)
);
-- 每张表中添加一条数据
INSERT INTO t1(content) values (CONCAT('t1_',FLOOR(1+RAND()*1000)));
INSERT INTO t2(content) values (CONCAT('t2_',FLOOR(1+RAND()*1000)));
INSERT INTO t3(content) values (CONCAT('t3_',FLOOR(1+RAND()*1000)));
INSERT INTO t4(content) values (CONCAT('t4_',FLOOR(1+RAND()*1000)));
explain之id介绍
select 查询的序列号,表示查询中执行select子句或操作表的顺序。
# id相同时,执行顺序由上至下。
explain select * from t1,t2,t3 where t1.id = t2.id and t2.id=t3.id; #得到表格1
# id不同,如果是子查询,id的序号会递增,id值越大优先级越高,则先被执行。
explain select t1.id from t1 where t1.id in (
select t2.id from t2 where t2.id in (
select t3.id from t3 where t3.id = 1
)
) # 得到表格2
# id相同和不同都存在时,id相同的可以理解为一组,从上往下顺序执行,所有组中,id值越大,优先级越高越先执行。
explain select t2.* from t2,(select * from t3) s3 where s3.id = t2.id; #得到表3
表格1:
id | select_type | table | partitions | type | possible_keys | key | ken_len | ref | rows | filtered | Extra |
---|---|---|---|---|---|---|---|---|---|---|---|
1 | SIMPLE | t1 | null | system | PRIMARY | null | null | null | 1 | 100 | null |
1 | SIMPLE | t2 | null | system | PRIMARY | null | null | null | 1 | 100 | null |
1 | SIMPLE | t3 | null | system | PRIMARY | null | null | null | 1 | 100 | null |
表格2:
id | select_type | table | type | possible_keys | key | ken_len | ref | rows | Extra |
---|---|---|---|---|---|---|---|---|---|
1 | PRIMARY | t1 | index | null | PRIMARY | null | null | 1 | |
2 | DEPENDENT | t2 | unique_sub | PRIMARY | PRIMARY | 4 | func | 1 | Using index |
3 | SIMPLE | t3 | const | PRIMARY | PRIMARY | 4 | const | 1 | Using index |
表格3:
id | select_type | table | type | possible_keys | key | ken_len | ref | rows | Extra |
---|---|---|---|---|---|---|---|---|---|
1 | PRIMARY | <derived2> | system | null | null | null | null | 1 | |
1 | PRIMARY | t2 | const | PRIMARY | PRIMARY | 4 | const | 1 | |
2 | DERIVED | t3 | ALL | null | null | null | null | 1 |
Explain之select_type介绍
查询的类型,常见值有:
SIMPLE:简单的select查询,查询中不包含子查询或者UNION
PRIMARY:查询中若包含任何复杂的子部分,最外层查询则被标记为Primary
DERIVED:在from列表中包含的子查询被标记为DERIVED(衍生),MySQL会递归执行这些子查询,把结果放在临时表里。
SUBQUERY:在select或where列表中包含了子查询
Explain之table介绍
显示这一行的数据是关于哪张表的。
Explain之type介绍
-
system:表只有一行记录(等于系统表),这是const类型的特列,平时不会出现,这个也可以忽略不计。
-
const:表示通过索引一次就找到了,const用于比较primary key或者unique索引。因为只匹配一行数据,所以很快,如将主键置于where列表中,MySQL就能将该查询转换为一个常量。
-
eq_ref:唯一性索引扫描,对于每个索引键,表中只有一条记录与之匹配。常见于主键或唯一索引扫描。
-
ref:非唯一性索引扫描,返回匹配某个单独值的所有行。本质上也是一种索引访问,它返回所有匹配某个单独值的行,然而,它可能会找到多个符合条件的行,所以他应该属于查找和扫描的混合体。
-
range:只检索给定范围的行,使用一个索引来选择行。key 列显示使用了哪个索引
一般就是在你的where语句中出现了between、<、>、in等的查询这种范围扫描索引扫描比全表扫描要好,因为它只需要开始于索引的某一点,而结束语另一点,不用扫描全部索引。 -
index:Full Index Scan,index与ALL区别为index类型只遍历索引树。这通常比ALL快,因为索引文件通常比数据文件小。也就是说虽然all和Index都是读全表,但index是从索引中读取的,而all是从硬盘中读的。
-
All:Full Table Scan,将遍历全表以找到匹配的行。
从最好到最差依次是:system>const>eq_ref>ref>range>index>All 。一般来说,最好保证查询能达到range级别,最好能达到ref。
Explain之prossible key和key介绍
prossible key: 显示可能应用在这张表中的索引,一个或多个。查询涉及到的字段上如果存在索引,则改索引将会被列出来,但不一定会被查询实际使用上。
key: 查询中实际使用的索引,如果为NULL,则没有使用索引。
Explain之ref介绍
显示索引的哪一列被使用了。哪些列或常量被用于查找索引列上的值。
Explain之rows介绍
rows列显示MySQL认为它执行查询时必须检查的行数。一般越少越好。
Explain之extra介绍
一些常见的重要的额外信息:
Using filesort:MySQL无法利用索引完成的排序操作称为“文件排序”。
Using temporary:Mysql在对查询结果排序时使用临时表,常见于排序order by和分组查询group by。
Using index:表示索引被用来执行索引键值的查找,避免访问了表的数据行,效率不错。
Using where:表示使用了where过滤。
完整案列
-- 建表
DROP TABLE IF EXISTS person;
CREATE TABLE person (
PID int(11) AUTO_INCREMENT COMMENT '编号',
PNAME varchar(50) COMMENT '姓名',
PSEX varchar(10) COMMENT '性别',
PAGE int(11) COMMENT '年龄',
SAL decimal(7, 2) COMMENT '工资',
PRIMARY KEY (PID)
);
-- 创建存储过程
create procedure insert_person(in max_num int(10))
begin
declare i int default 0;
set autocommit = 0;
repeat
set i = i +1;
insert into person (PID,PNAME,PSEX,PAGE,SAL) values (i,concat('test',floor(rand()*10000000)),IF(RAND()>0.5,'男','女'),FLOOR((RAND()*100)+10),FLOOR((RAND()*19000)+1000));
until i = max_num
end repeat;
commit;
end;
-- 调用存储过程
call insert_person(30000000);
-- 不使用索引,根据pName进行查询
select * from person where PNAME = "test1209325"; #10.813S
-- 给PNAME建立索引
alter table person add index idx_pname(PNAME);
select * from person where PNAME = "test1209325"; # 0.032S
select * from person where PID = 2800000; #0.021
-- 创建四张测试表
CREATE TABLE t1(
id INT(10) AUTO_INCREMENT,
content VARCHAR(100),
PRIMARY KEY (id)
);
CREATE TABLE t2(
id INT(10) AUTO_INCREMENT,
content VARCHAR(100),
PRIMARY KEY (id)
);
CREATE TABLE t3(
id INT(10) AUTO_INCREMENT,
content VARCHAR(100),
PRIMARY KEY (id)
);
CREATE TABLE t4(
id INT(10) AUTO_INCREMENT,
content VARCHAR(100),
PRIMARY KEY (id)
);
-- 每张表中添加一条数据
INSERT INTO t1(content) VALUES(CONCAT('t1_',FLOOR(1+RAND()*1000)));
INSERT INTO t2(content) VALUES(CONCAT('t2_',FLOOR(1+RAND()*1000)));
INSERT INTO t3(content) VALUES(CONCAT('t3_',FLOOR(1+RAND()*1000)));
INSERT INTO t4(content) VALUES(CONCAT('t4_',FLOOR(1+RAND()*1000)));
#id相同时,执行顺序是从上往下
explain select * from t1,t2,t3 where t1.id=t2.id and t2.id = t3.id;
#id不同时
explain select t1.id from t1 where t1.id in
(select t2.id from t2 where t2.id in
(select t3.id from t3 where t3.id = 1)
);
#id相同和id不同
explain select t2.* from t2,(select * from t3) s3 where s3.id = t2.id;
-- select_type
#simple 简单的select查询
explain select * from t1;
#derived primary
explain select * from (select t1.content from t1) s1;
#subquery
explain select t2.* from t2 where t2.id = (select t3.id from t3);
-- tpye
#All
explain select * from t1 where t1.content = "abc";
#system
explain select * from (select t1.id from t1 where id = 1) t;
#const 索引一次就找到了
explain select * from t1 where id = 1;
#eq_ref
explain select t1.*,t2.*
from t1
join t2
on t1.id = t2.id;
#ref非唯一索引扫描
alter table t1 add index idx_t1_content(content);
explain select * from t1 where t1.content = "abc";
#range
explain select * from t2 where t2.id >0;
#index
explain select id,content from t1;
-- possible keys key
explain select * from t2 where t2.id is not null;
-- ref
explain select t2.*,t3.* from t2,t3 where t2.id = t3.id;
-- rows
#删除person上的索引
drop index idx_pname on person;
alter table person add index idx_pname(PNAME);
explain select person.* from person where pname = "test3263609";
drop table if exists emps;
CREATE TABLE emps (
id INT PRIMARY KEY AUTO_INCREMENT COMMENT "主键id",
name VARCHAR (24) COMMENT '姓名',
age INT COMMENT '年龄',
job VARCHAR (20) COMMENT '职位'
);
INSERT INTO emps(name,age,job) VALUES('zhangsan',22,'manager');
INSERT INTO emps(name,age,job) VALUES('lisi',23,'clerk');
INSERT INTO emps(name,age,job) VALUES('wangwu',24,'salsman');
INSERT INTO emps(name,age,job) VALUES('赵六',23,'salsman');
-- extra
-- using filesort 排序时没有使用索引
explain select * from emps order by age;
-- using temporary 分组时没有使用索引
explain select count(*),job from emps group by job;
-- using index
explain select id,name from emps;
-- using where
explain select * from emps where name = "张三";