对于DBA而言,索引是数据库调优的常用手段,合理的使用索引可以加速SQL语句的执行速度,帮助数据库更加高效的利用系统资源。同时,当前数据库业务场景的复杂多变性对索引提出了更高的要求。
为了支撑这些复杂多变的应用场景,Kingbase ES提供了6种类型的索引,每种类型的索引对应不同的算法实现,下文将一一介绍其功能及应用场景。
Kingbase ES六种索引方式及适用场景
01
BTREE
BTREE是Kingbase ES的默认索引,采用B+树( N叉排序树)来做实现,由于树状结构每一层节点都有序列,因此非常适合用来做范围查询和优化排序操作。BTREE索引支持的操作符有 >, <, >=, <=, =, IN, LIKE等,同时,优化器也会优先选择BTREE来对ORDER BY、MIN、MAX、MERGE JOIN进行有序操作。
TEST=# create table t1 (id int, info text);
CREATE TABLE
TEST=# insert into t1 values(generate_series(1,100000), md5(random()::text));
INSERT 0 100000
TEST=# analyze;
ANALYZE
TEST=# explain analyze select * from t1 where id < 10; --无索引
QUERY PLAN
Seq Scan on T1 (cost=0.00…2084.00 rows=10 width=37) (actual time=0.024…20.771 rows=9 loops=1)
Filter: (ID < 10)
Rows Removed by Filter: 99991
Planning time: 0.216 ms
Execution time: 20.804 ms
(5 rows)
TEST=# create index i_btree on t1 using btree(id);
CREATE INDEX
TEST=# explain analyze select * from t1 where id < 10; --有btree索引
QUERY PLAN
Index Scan using I_BTREE on T1 (cost=0.29…8.45 rows=9 width=37) (actual time=0.006…0.011 rows=9 loops=1)
Index Cond: (ID < 10)
Planning time: 0.250 ms
Execution time: 0.036 ms
(4 rows)
TEST=# explain analyze select min(id) from t1; --BTREE索引优化聚集函数min
QUERY PLAN
Result (cost=0.33…0.34 rows=1 width=4) (actual time=0.144…0.144 rows=1 loops=1)
InitPlan 1 (retur