索引长度与区分度

1:查询频繁 2:区分度高  3:长度小  4: 尽量能覆盖常用查询字段.



1: 索引长度直接影响索引文件的大小,影响增删改的速度,并间接影响查询速度(占用内存多).


针对列中的值,从左往右截取部分,来建索引
1: 截的越短, 重复度越高,区分度越小, 索引效果越不好
2: 截的越长, 重复度越低,区分度越高, 索引效果越好,但带来的影响也越大--增删改变慢,并间影响查询速度.


所以, 要在  区分度 + 长度  两者上,取得一个平衡.


惯用手法: 截取不同长度,并测试其区分度,

 

[sql] view plain copy
 
  1. <span style="font-size:18px;">select count(distinct left(word,6))/count(*) from dict;   
  2. +---------------------------------------+  
  3. count(distinct left(word,6))/count(*) |  
  4. +---------------------------------------+  
  5. |                                0.9992 |  
  6. +---------------------------------------+</span>  

 

截取word字段长度,从1开始截取,计算字符前缀没有重复的字符占全部数据的比例


对于一般的系统应用: 区别度能达到0.1,索引的性能就可以接受.

 

3:多列索引
 3.1 多列索引的考虑因素---  
列的查询频率 , 列的区分度, 
以ecshop商城为例, goods表中的cat_id,brand_id,做多列索引
从区分度看,Brand_id区分度更高, 

 

[sql] view plain copy
 
  1. select count(distinct cat_id) / count(*) from  goods;  
  2. +-----------------------------------+  
  3. count(distinct cat_id) / count(*) |  
  4. +-----------------------------------+  
  5. |                            0.2903 |  
  6. +-----------------------------------+  
  7. 1 row in set (0.00 sec)  
  8.   
  9. mysql> select count(distinct brand_id) / count(*) from  goods;  
  10. +-------------------------------------+  
  11. count(distinct brand_id) / count(*) |  
  12. +-------------------------------------+  
  13. |                              0.3871 |  
  14. +-------------------------------------+  
  15. 1 row in set (0.00 sec)  


但从 商城的实际业务业务看, 顾客一般先选大分类->小分类->品牌,
最终选择 index(cat_id,brand_id)来建立索引
有如下表(innodb引擎), sql语句在笔记中, 
给定日照市,查询子地区, 且查询子地区的功能非常频繁,
如何优化索引及语句?

+------+-----------+------+
| id   | name      | pid  |
+------+-----------+------+
| .... | .... | .... |
| 1584 | 日照市 | 1476 |
| 1586 | 东港区 | 1584 |
| 1587 | 五莲县 | 1584 |
| 1588 | 莒县    | 1584 |
+------+-----------+------+


1: 不加任何索引,自身连接查询

 

[sql] view plain copy
 
  1. explain select s.id,s.name from it_area as p inner join it_area as s on p.id=s.pid   where p.name='日照市' \G  
  2. *************************** 1. row ***************************  
  3.            id: 1  
  4.   select_type: SIMPLE  
  5.         table: p  
  6.          type: ALL  
  7. possible_keys: NULL  
  8.           key: NULL  
  9.       key_len: NULL  
  10.           ref: NULL  
  11.          rows: 3263  
  12.         Extra: Using where  
  13. *************************** 2. row ***************************  
  14.            id: 1  
  15.   select_type: SIMPLE  
  16.         table: s  
  17.          type: ALL  
  18. possible_keys: NULL  
  19.           key: NULL  
  20.       key_len: NULL  
  21.           ref: NULL  
  22.          rows: 3263  
  23.         Extra: Using where; Using join buffer  
  24. rows in set (0.00 sec)  


2: 给name加索引

 

[sql] view plain copy
 
  1. explain select s.id,s.name from it_area as p inner join it_area as s on p.id=s.pid   where p.name='日照市' \G  
  2. *************************** 1. row ***************************  
  3.            id: 1  
  4.   select_type: SIMPLE  
  5.         table: p  
  6.          type: ref  
  7. possible_keys: name  
  8.           key: name  
  9.       key_len: 93  
  10.           ref: const  
  11.          rows: 1  
  12.         Extra: Using where  
  13. *************************** 2. row ***************************  
  14.            id: 1  
  15.   select_type: SIMPLE  
  16.         table: s  
  17.          type: ALL  
  18. possible_keys: NULL  
  19.           key: NULL  
  20.       key_len: NULL  
  21.           ref: NULL  
  22.          rows: 3243  
  23.         Extra: Using where; Using join buffer  
  24. rows in set (0.00 sec)  


3: 在Pid上也加索引

 

[sql] view plain copy
 
    1. explain select s.id,s.name from it_area as p inner join it_area as s on p.id=s.pid   where p.name='日照市' \G  
    2. *************************** 1. row ***************************  
    3.            id: 1  
    4.   select_type: SIMPLE  
    5.         table: p  
    6.          type: ref  
    7. possible_keys: name  
    8.           key: name  
    9.       key_len: 93  
    10.           ref: const  
    11.          rows: 1  
    12.         Extra: Using where  
    13. *************************** 2. row ***************************  
    14.            id: 1  
    15.   select_type: SIMPLE  
    16.         table: s  
    17.          type: ref  
    18. possible_keys: pid  
    19.           key: pid  
    20.       key_len: 5  
    21.           ref: big_data.p.id  
    22.          rows: 4  
    23.         Extra: Using where  
    24. rows in set (0.00 sec)  
posted @ 2018-01-05 10:15  叶落枫零  阅读(279)  评论(0编辑  收藏  举报