创建一个he_user_info表,添加6000条数据
1.写出要查询的具体参数
比如:
select * from he_user_info //耗时0.004s
select username,`password` from he_user_info //耗时0.002s
从上可以看出指明要查询的参数会节省将近一倍的时间
2.使用索引查询
select username,`password` from he_user_info where username='hzh' //耗时0.001s
select username,`password` from he_user_info where id=1 //耗时0s
从上可以看出使用索引查询速度比不使用快
以上只是最最最基本的
重点
下来我要讲一下Navicat的**“解释“**功能
type列,连接类型。分为(从垃圾到牛逼)all,index,range,ref,ref_eq,cons
t一个好的SQL语句至少要达到range级别。杜绝出现all级别。
key列,使用到的索引名。如果没有选择索引,值是NULL。可以采取强制索引方式。
key_len列,索引长度。
rows列,扫描行数。该值是个预估值。
extra列,详细说明。注意,常见的不太友好的值,如下:Using filesort,Using temporary。
接下来列举几个常见的优化
1.分页优化
select * from he_user_info LIMIT 4000,10
优化为
select * from he_user_info where id > 4000 LIMIT 10 //下一页
select * from he_user_info where id < 4000 order by id desc LIMIT 10 //上一页