Rumah > pangkalan data > tutorial mysql > mysql 优化(4)索引覆盖和最优索引

mysql 优化(4)索引覆盖和最优索引

黄舟
Lepaskan: 2016-12-29 16:09:12
asal
1353 orang telah melayarinya

索引覆盖** 很重要的 一个概念 就是在索引上查找!!!

如果查询的列恰好是索引的一部分,那么查询只需要在索引文件上进行,不需要回行到磁盘再找数据.

这种查询速度非常快,称为”索引覆盖”

非聚促索引 索引文件对应了数据要回行 浪费掉了时间 

索引和数据区别 

索引是高效组织起来的树 , 节点 ,查找树叶 结构更优于数据

索引可以被导入内存中进行查询 

索引本身数据结构简单 再放入内存 很快很快 

。。。。。。。。。结果一回行 就满了。。。我擦。。。

所以呢 如果建立两条索引 index( uid , aid) 

如果在索引树上已经覆盖了你想要的数据 如 select uid from msg where aid = 1 

索引已经覆盖了想要查的 则直接在内存中的索引取出来 速度效率都非常快 自然不用回行查找

最优索引:

查询频繁,区分度高 ,长度小

尽量覆盖常用字段 , ------》索引覆盖

左前缀不易区分 逆转url 把内容到过来 ; 左前缀区分度大 

伪哈希 crc32() 函数 来唯一区分 crc32('http://wwww.baidu.com');给urlcrc 建索引 而不是去给url 避免了索引

学会 explain 来查看效果 ;

伪hash索引效果

同时存 url_hash列

create table t10 (
id int primary key,
url char(60) not null default ''
);
Salin selepas log masuk
insert into t10 values 
(1,'http://www.baidu.com'),
(2,'http://www.sina.com'),
(3,'http://www.sohu.com.cn'),
(4,'http://www.onlinedown.net'),
(5,'http://www.gov.cn');
alter table t10 add urlcrc int unsigned not null;
Salin selepas log masuk

在sql存储时, crcurl== crc32(url),

因为crc的结果是32位int无符号数,因此当数据超过40亿,也会有重复,但这是值得的.

(索引长度为int4个字节)



多咧索引的设计

但从 商城的实际业务业务看, 顾客一般先选大分类->小分类->品牌,

最终选择 (1)index(cat_id,brand_id), + index(cat_id,shop_price)来建立索引

甚至可以再加 (3)index(cat_id,brand_id,shop_price),3个冗余索引 冗余字段 。。。



但(3)中的前2列和(1)中的前2列一样,再去掉(1)



index(cat_id,price) 和 index(cat_id,brand_id,shop_price); 也就是可以冗余 但是不能能重复 左前缀原则


 以上就是mysql 优化(4)索引覆盖和最优索引的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan