前缀索引

前言:

    前缀索引有助于提高索引效率。索引选择性越高则查询效率越高。

   我们应该选择合适长度的前缀索引,保证索引选择性,又不会增加索引空间。

   适用场景:对于blog,text,varchar类型的列,必须使用前缀索引来保证索引的查询效率.

  

   如何计算合适的前缀索引长度:

          完整列的选择性=前缀索引的选择性

  demo:

         首先计算出完整列的选择性

select count(distinct card_id)/count(*) from card
--------------

+----------------------------------+
| count(distinct card_id)/count(*) |
+----------------------------------+
|                           1.0000 |
+----------------------------------+

1 row in set (14.72 sec)


计算前缀索引的选择性

select count(distinct left(card_id,5))/count(*),
count(distinct left(card_id,6))/count(*),
count(distinct left(card_id,7))/count(*),
count(distinct left(card_id,8))/count(*) from card
--------------


+------------------------------------------+------------------------------------------+------------------------------------------+------------------------------------------+
| count(distinct left(card_id,5))/count(*) | count(distinct left(card_id,6))/count(*) | count(distinct left(card_id,7))/count(*) | count(distinct left(card_id,8))/count(*) |
+------------------------------------------+------------------------------------------+------------------------------------------+------------------------------------------+
|                                   0.0100 |                                   0.1000 |                                   1.0000 |                                   1.0000 |
+------------------------------------------+------------------------------------------+------------------------------------------+------------------------------------------+
1 row in set (3 min 4.62 sec)


总结:当前缀索引长度为7时,更接近完整列的选择性.而当索引长度为8时,选择性增加不明显.所以索引长度应设置为7.


mysql> alter table card add index(card_id(7));
--------------
alter table card add index(card_id(7))
--------------


Query OK, 0 rows affected, 2 warnings (1 min 21.63 sec)
Records: 0  Duplicates: 0  Warnings: 2


mysql> show index from card;
--------------
show index from card
--------------


+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| card  |          1 | card_id  |            1 | card_id     | A         |     9719226 |        7 | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)





      


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值