1.什么是索引html
1)索引就比如一本书的目录,它能让你更快的找到本身想要的内容。
2)让获取的数据更有目的性,从而提升数据库检索数据的性能。mysql
2.索引类型介绍linux
1)BTREE:B+树索引
2)HASH:HASH索引
3)FULLTEXT:全文索引
4)RTREE:R树索引nginx
web
索引创建在表的列上(字段)的。
在where后面的列创建索引才会加快查询速度。
pages<---索引(属性)<----查数据。redis
主键索引
普通索引*****
惟一索引sql
#建立索引
alter table test add index index_name(name); #建立索引 create index index_name on test(name); #查看索引 desc table; #查看索引 show index from table; #删除索引 alter table test drop key index_name; #添加主键索引(略) #添加惟一性索引 alter table student add unique key uni_xxx(xxx); #查看表中数据行数 select count(*) from city; #查看去重数据行数 select count(distinct name) from city;
前缀索引数据库
根据字段的前N个字符创建索引服务器
alter table test add index idx_name(name(10));
避免对大列建索引
若是有,就使用前缀索引函数
联合索引
多个字段创建一个索引
例:
where a.女生 and b.身高 and c.体重 and d.身材好
index(a,b,c)
特色:前缀生效特性
a,ab,ac,abc,abcd 能够走索引或部分走索引
b bc bcd cd c d ba ... 不走索引
原则:把最经常使用来作为条件查询的列放在最前面
#建立people表
create table people (id int,name varchar(20),age tinyint,money int ,gender enum('m','f')); #建立联合索引 alter table people add index idx_gam(gender,age,money);
explain命令使用方法
mysql> explain select name,countrycode from city where id=1;
explain命令应用
查询数据的方式
生产中,mysql在使用全表扫描时的性能是极其差的,因此MySQL尽可能避免出现全表扫描
2.1 常见的索引扫描类型:
1)index
2)range
3)ref
4)eq_ref
5)const
6)system
7)null
从上到下,性能从最差到最好,咱们认为至少要达到range级别
index:Full Index Scan,index与ALL区别为index类型只遍历索引树。
range:索引范围扫描,对索引的扫描开始于某一点,返回匹配值域的行。显而易见的索引范围扫描是带有between或者where子句里带有<,>查询。
mysql> alter table city add index idx_city(population); mysql> explain select * from city where population>30000000;
ref:使用非惟一索引扫描或者惟一索引的前缀扫描,返回匹配某个单独值的记录行。
mysql> alter table city drop key idx_code;
mysql> explain select * from city where countrycode='chn'; mysql> explain select * from city where countrycode in ('CHN','USA'); mysql> explain select * from city where countrycode='CHN' union all select * from city where countrycode='USA';
eq_ref:相似ref,区别就在使用的索引是惟一索引,对于每一个索引键值,表中只有一条记录匹配,简单来讲,就是多表链接中使用primary key或者 unique key做为关联条件A
join B on A.sid=B.sid
const、system:当MySQL对查询某部分进行优化,并转换为一个常量时,使用这些类型访问。
如将主键置于where列表中,MySQL就能将该查询转换为一个常量
mysql> explain select * from city where id=1000;
NULL:MySQL在优化过程当中分解语句,执行时甚至不用访问表或索引,例如从一个索引列里选取最小值能够经过单独索引查找完成。
mysql> explain select * from city where id=1000000000000000000000000000;
Extra(扩展)
Using temporary
Using filesort 使用了默认的文件排序(若是使用了索引,会避免这类排序)
Using join buffer
若是出现Using filesort请检查order by ,group by ,distinct,join 条件列上没有索引
mysql> explain select * from city where countrycode='CHN' order by population;
当order by语句中出现Using filesort,那就尽可能让排序值在where条件中出现
mysql> explain select * from city where population>30000000 order by population; mysql> select * from city where population=2870300 order by population;
key_len: 越小越好
rows: 越小越好
为了使索引的使用效率更高,在建立索引时,必须考虑在哪些字段上建立索引和建立什么类型的索引。
那么索引设计原则又是怎样的?
惟一性索引的值是惟一的,能够更快速的经过该索引来肯定某条记录。
例如:
学生表中学号是具备惟一性的字段。为该字段创建惟一性索引能够很快的肯定某个学生的信息。
若是使用姓名的话,可能存在同名现象,从而下降查询速度。
主键索引和惟一键索引,在查询中使用是效率最高的。
select count(*) from world.city; select count(distinct countrycode) from world.city; select count(distinct countrycode,population ) from world.city;
注意:若是重复值较多,能够考虑采用联合索引
例如:
常常须要ORDER BY、GROUP BY、DISTINCT和UNION等操做的字段,排序操做会浪费不少时间。
若是为其创建索引,能够有效地避免排序操做
注:若是常常做为条件的列,重复值特别多,能够创建联合索引
若是索引字段的值很长,最好使用值的前缀来索引。例如,TEXT和BLOG类型的字段,进行全文检索
会很浪费时间。若是只检索字段的前面的若干个字符,这样能够提升检索速度。
----------------------------------------------------------------------------- 我是华丽的分割线 ---------------------------------------------------------------------------
---------------------------------------------------------------------------- 是的,没错,又是我 -------------------------------------------------------------------------
重点关注:
#全表扫描
select * from table; select * from tab where 1=1;
在业务数据库中,特别是数据量比较大的表,是没有全表扫描这种需求。
1)对用户查看是很是痛苦的。
2)对服务器来说毁灭性的。
3)SQL改写成如下语句:
#状况1 #全表扫描 select * from table; #须要在price列上创建索引 selec * from tab order by price limit 10; #状况2 #name列没有索引 select * from table where name='zhangsan'; 一、换成有索引的列做为查询条件 二、将name列创建索引
mysql> explain select * from city where population>3000 order by population;
1)若是业务容许,可使用limit控制。
2)结合业务判断,有没有更好的方式。若是没有更好的改写方案就尽可能不要在mysql存放这个数据了,放到redis里面。
索引有自我维护的能力。
对于表内容变化比较频繁的状况下,有可能会出现索引失效。
重建索引就能够解决
#例子
错误的例子:select * from test where id-1=9; 正确的例子:select * from test where id=10;
mysql> create table test (id int ,name varchar(20),telnum varchar(10)); mysql> insert into test values(1,'zs','110'),(2,'l4',120),(3,'w5',119),(4,'z4',112); mysql> explain select * from test where telnum=120; mysql> alter table test add index idx_tel(telnum); mysql> explain select * from test where telnum=120; mysql> explain select * from test where telnum=120; mysql> explain select * from test where telnum='120';
mysql> select * from tab where telnum <> '1555555'; mysql> explain select * from tab where telnum <> '1555555';
单独的>,<,in 有可能走,也有可能不走,和结果集有关,尽可能结合业务添加limit
or或in尽可能改为union
EXPLAIN SELECT * FROM teltab WHERE telnum IN ('110','119'); #改写成 EXPLAIN SELECT * FROM teltab WHERE telnum='110' UNION ALL SELECT * FROM teltab WHERE telnum='119'
#走range索引扫描
EXPLAIN SELECT * FROM teltab WHERE telnum LIKE '31%'; #不走索引 EXPLAIN SELECT * FROM teltab WHERE telnum LIKE '%110';
%linux%类的搜索需求,可使用Elasticsearch -------> ELK
CREATE TABLE t1 (id INT,NAME VARCHAR(20),age INT ,sex ENUM('m','f'),money INT); ALTER TABLE t1 ADD INDEX t1_idx(money,age,sex); DESC t1 SHOW INDEX FROM t1 #走索引的状况测试 EXPLAIN SELECT NAME,age,sex,money FROM t1 WHERE money=30 AND age=30 AND sex='m'; #部分走索引 EXPLAIN SELECT NAME,age,sex,money FROM t1 WHERE money=30 AND age=30; EXPLAIN SELECT NAME,age,sex,money FROM t1 WHERE money=30 AND sex='m'; #不走索引 EXPLAIN SELECT NAME,age,sex,money FROM t1 WHERE age=20 EXPLAIN SELECT NAME,age,sex,money FROM t1 WHERE age=30 AND sex='m'; EXPLAIN SELECT NAME,age,sex,money FROM t1 WHERE sex='m';