设为首页 加入收藏

TOP

mysql优化之mysql查询性能排序分析
2018-06-12 08:43:15 】 浏览:87
Tags:mysql 优化 查询 性能 排序 分析

mysql 查询性能排序,从左至右,性能由最差到最好

ALL index range ref eq_ref const/system NULL

1.ALL 全表扫描

例:select * from user

2.index 索引全扫描

例:select id from user

3.range 索引范围扫描(>、<、>=、<=、=、between等)

例:select * from user where id >= 5

4.ref 使用非唯一索引扫描,或唯一索引的前缀扫描

例:select * from user where id = 5

join中也常出现ref

5.eq_ref 使用唯一索引(多表连接用主键或unique key作关联条件)

例:select * from teacher a, student b where a.id = b.id

6.const/system 常量

例:select * from(select * from user where id = 1)

备注:临时表中最多有一个匹配行(主键不重复),mysql会当成常量

等于是select * from 常量

7.NULL 不用查就有结果

例:select 1 from user

-------------------------------------------------------------------------------------------

索引在存储引擎层实现,所以每种存储引擎的索引不一定相同,也不一定都互相支持

4种索引:

B-Tree、Hash、R-Tree、Full-text

B-Tree最常见,基本都支持

Hash索引适用于key-value查询,不适用范围查询(<、>、<=、>=),Memory支持

R-Tree空间索引,不常用

Full-Text全文索引,5.6开始支持

如果列名为索引,使用column_name is null 就会使用索引,此处区别oracle

-------------------------------------------------------------------------------------------

索引存在,但不能使用的情况

1. like '%值',(前任意后精确)

例:select * from user where name like '%WAR3'

2.数据类型隐式转换

例:select * from user where id = 1

id是字符型,这里写了数字

3.不满足最左原则匹配

例:alter table warcraftIII add index idx(id,team,race)

添加复合索引idx

select * from warcraftIII where id = 5 可用索引

select * from warcraftIII where id = 5 and team = 'WE' 可用索引

select * from warcraftIII where team = 'MYM' and race = 'HUM' 索引不可用

查id、id,team、id,race、id,team,race、都走索引,但是,查team,race不走

4.mysql估计用索引比全表扫描慢

(mysql内部有个sql优化器)

5.用or时,一个是索引,一个不是

是索引的那列,不会走索引

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇SQLServer事务隔离级别的选择如何.. 下一篇windows安装MySQL8.0的步骤教程

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目