我们知道,MySQL 不支持条件索引。 什么是条件索引呢? 条件索引就是在索引列上根据WHERE条件进行一定的过滤后产生的索引。 这样的索引有以下优势:
第一点, 比基于这个列的全部索引占用空间来的小。
第二点, 特别是基于FULL INDEX SCAN 的时候,占用空间小的索引对内存占用也小很多。
PostgreSQL,SqlServer等都支持条件索引,所以我们先来看下条件索引的实际情况。
表结构如下,记录大概有10W行:
Table "ytt.girl1"
Column | Type | Modifiers
--------+---------+--------------------
id | integer | not null
rank | integer | not null default 0
Indexes:
"girl1_pkey" PRIMARY KEY, btree (id)
"idx_girl1_rank" btree (rank) WHERE rank >= 10 AND rank <= 100
执行的查询语句为:
select * from girl1 where rank between 20 and 60 limit 20;
用了全部索引的查询计划:
QUERY PLAN
------------------------------------------------------------------------------------------------------------------