mysql之explain关键字

1、 用mysql存储过程增加100万条测试数据

存储过程代码:

#创建存储过程,数据库名test,表名student
create procedure myinst(n int)
begin
declare i int default 0;
set autocommit = 0;
repeat
set i = i +1;
insert into student(id,name) values(i,'xiaoli');
until i= n end repeat;
commit;
set autocommit = 1;
end
#调用存储过程,n为创建数据条数
call myinst(1000000);#一百万条:45s
存储过程需要注意的事项:

1、参数如果不显式指定“in”、“out”、“inout”,则默认为“in”。习惯上,对于是“in”的参数,我们都不会显式指定。多个参数用','分割。
2、MySQL 存储过程名字后面的“()”是必须的,即使没有一个参数,也需要“()”。
3、MySQL 存储过程参数,不能在参数名称前加“@”,如:“@a int”。
4、 MySQL 存储过程中的变量,不需要在变量名字前加“@”,虽然 MySQL 客户端用户变量要加个“@”。
5、MySQL 存储过程的参数不能指定默认值。
6、MySQL 存储过程不需要在procedure body 前面加“as”。而SQL Server 存储过程必须加“as” 关键字。
7、如果 MySQL 存储过程中包含多条MySQL 语句,则需要begin end 关键字。
8、不能在 MySQL 存储过程中使用“return” 关键字。
9、调用 MySQL 存储过程时候,需要在过程名字后面加“()”,即使没有一个参数,也需要“()”

2、 explain关键字

如果在select语句前放上关键词explain,mysql将解释它如何处理select,提供有关表如何联接和联接的次序。

explain属性:

id:select查询的序列号

select_type:select查询的类型,主要是区别普通查询和联合查询、子查询之类的复杂查询

table: 输出的行所引用的表

type:

联合查询所使用的类型,type显示的是访问类型,是较为重要的一个指标,结果值从好到坏依次是:

system > const > eq_ref > ref >fulltext > ref_or_null > index_merge > unique_subquery >index_subquery > range > index > ALL

一般来说,得保证查询至少达到range级别,最好能达到ref。

         type=const表示通过索引一次就找到了;

         type=all,表示为全表扫描;

         type=ref,因为这时认为是多个匹配行,在联合查询中,一般为REF;

key:

显示MySQL实际决定使用的键。如果没有索引被选择,键是NULL。

         key=primary的话,表示使用了主键;

         key=null表示没用到索引。

possible_keys:

指出MySQL能使用哪个索引在该表中找到行。如果是空的,没有相关的索引。这时要提高性能,可通过检验WHERE子句,看是否引用某些字段,或者检查字段不是适合索引。

key_len:

显示MySQL决定使用的键长度。如果键是NULL,长度就是NULL。文档提示特别注意这个值可以得出一个多重主键里mysql实际使用了哪一部分。

ref:

显示哪个字段或常数与key一起被使用。

rows:

这个数表示mysql要遍历多少数据才能找到,在innodb上是不准确的。

Extra:

如果是Only index,这意味着信息只用索引树中的信息检索出的,这比扫描整个表要快。

如果是where used,就是使用上了where限制。

如果是impossible where 表示用不着where,一般就是没查出来啥。

示例:#student表-一百万条测试数据

1)      explain select * from student;//么有建立索引,没有使用索引,如果主键建立索引,*查询默认使用主键索引

2)      explain select id from student;//id作为索引

3)      select * from student  where id=100;

4)      explain select id from studentwhere name like ‘%xiaoli%’;

explain select id from student where namelike ‘xiaoli%’;

explain select id from student where namelike ‘%xiaoli’;

explain select id from student where name='xiaoli'

//加入where查询,以上四条执行索引均失效


5)      explain select id from student order by id;


6)      explain select * from studentorder by id;


7)      explain select id from studentorder by name;//以name排序,则id索引失效


8)      explain select * from studentgroup by name order by id;//以name分组,则id索引失效


9)      explain select u.id from u_useru ,u_user_role ur where u.ID=ur.user_id;

//关联表u_user_role没有建立索引,u_user表索引(unique)为主键id


//同样的sql,关联表u_user_role用字段user_id和role_id建立联合索引(unique)


1052 column id in field list ambiguous  EXCEPTION

列'ID'在字段列表中重复,其实就是两张表有相同的字段,但是使用时表字段的名称前没有加表名,导致指代不明

问题sql: explain select id from u_user u ,u_user_role urwhere u.ID=ur.user_id

修正sql:explain select u.id fromu_user u ,u_user_role ur where u.ID=ur.user_id
10)      explain select count(*) fromstudent;//看出count(*)用了主键id做索引


11)      explain select count(id) from student;//执行结果同上,直接使用主键id做索引

12)      explain select count(name) fromstudent;//name为表student中非主键字段,可见count(name)没有用到索引

猜你喜欢

转载自blog.csdn.net/qq_36838191/article/details/89155968
今日推荐