.. _使用索引:

使用索引
========

索引是一种有序的存储结构，也是一项极为重要的SQL优化手段，可以提高数据检索的速度。通过在表中的一个或多个列上创建索引，很多SQL语句的执行效率可以得到极大的提高。

  .. figure:: images/sql-optimization_image19.png
     :scale: 100 % 
     :alt: map to buried treasure

如图所示，使用索引根据索引仅读取想要查找的块，从而减少代价。

- 索引类型_
- 索引使用技巧_
- 使用索引建议工具_


索引类型
--------------

Btree索引
^^^^^^^^^^^^^^^^

Btree是索引是最常见的索引类型，也是KingbaseES的默认索引，采用B+树(N叉排序树)实现，由于树状结构每一层节点都有序列，因此非常适合用来做范围查询和优化排序操作。Btree索引支持的操作符有 >，<，>=，<=，=，IN，LIKE等 ，同时，优化器也会优先选择Btree来对ORDER BY、MIN、MAX、MERGE JOIN进行有序操作。

精确匹配为类似：id = 1005，name =‘abc’之类的条件。而范围匹配则为类似：id < 1005，id > 10 and id <= 100等条件。

如果SQL语句仅访问被索引的列，则整个查询只需要通过索引即可完成，无需查找表数据。如果该语句还需要访问索引列以外的其他列数据，则会使用索引指向的tid来查找表中的行数据。


  .. code::

   create table t1 (id int, info text);
   insert into t1 values(generate_series(1,100000), md5(random()::text));
   analyze;
   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)
   
   
   
   create index i_btree on t1 using btree(id); 
   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)
   
   
   
   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 (returns $0)
       Limit (cost=0.29..0.33 rows=1 width=4) (actual time=0.132..0.135 rows=1 loops=1)
           -> Index Only Scan using I_BTREE on T1 (cost=0.29..3691.29 rows=100000 width=4) (actual time=0.129..0.130 rows=1 loops=1)
               Index Cond: (ID IS NOT NULL)
               Heap Fetches: 1
   Planning time: 0.309 ms
   Execution time: 0.221 ms
   (8 rows)


Hash索引
^^^^^^^^^^^^^

Hash索引查询效率比Btree高得多，相比Btree索引需要从根节点查询到叶子节点，且需要多次IO及CPU操作，Hash索引因其索引结构的特殊性，理想情况下仅需一次检索即可定位到数据。

与此同时，Hash也存在一定的局限性，只适合做\ **等值查询**\ 。由于Hash索引是通过比较哈希值来做定位，因此当查询列上有较大比例的重复值时，会带来严重的哈希冲突，进而影响查询速度，这种情况下就不建议使用Hash索引。



  .. code::

   create table t2 (id int, info text);
   insert into t2 values(generate_series(1,100000), md5(random()::text));
   analyze;
   create index i_hash on t2 using hash(id);
   explain analyze select * from t2 where id = 10;  --HASH索引
   
   QUERY PLAN
   ----------------------------------------------------------------------------
   Index Scan using I_HASH on T2  (cost=0.00..8.02 rows=1 width=37) (actual   time=0.013..0.014 rows=1 loops=1)
       Index Cond: (ID = 10)
   Planning time: 0.167 ms
   Execution time: 0.033 ms
   (4 rows)
   
   
   
   explain analyze select * from t2 where id < 10;  --HASH索引不支持非等值操作
   
   QUERY PLAN
   ----------------------------------------------------------------------------
   Seq Scan on T2  (cost=0.00..2084.00 rows=10 width=37)  (actual time=0.015..26.223 rows=9 loops=1)
       Filter: (ID < 10)
       Rows Removed by Filter: 99991
   Planning time: 0.070 ms
   Execution time: 26.245 ms
   (5 rows)




Bitmap索引(执行节点)
^^^^^^^^^^^^^^^

Bitmap索引是用一个位图来存放记录索引信息的结构。它用一个bit位来表示某个属性对应的值是否存在，存在为1，不存在为0，比较适合那种值比较单一（比如：性别字段）的情况。Bitmap索引有很大的压缩比空间，比较适合OLAP应用。


Kingbase在为where x=33 or x=44 or x=
55类似的查询创建查询计划的时候，将where过滤分解成3个独立的基于x字段索引的扫描，每个扫描使用一个查询子句，之后再将这些扫描结果OR在一起并生成最终的结果。另外一个例子是，如果我们在x和y上分别存在独立的索引，那么一个类似WHERE
x = 5 AND y =
6的查询，就会分别基于这两个字段的索引进行扫描，之后再将各自扫描的结果进行AND操作并生成最终的结果行。


为了组合多个索引，系统扫描每个需要的索引，然后在内存里组织一个BITMAP，它将给出索引扫描出的数据在数据表中的物理位置。然后，再根据查询的需要，把这些位图进行AND或者OR的操作并得出最终的BITMAP。最后，检索数据表并返回数据行。表的数据行是按照物理顺序进行访问的，因为这是位图的布局，这就意味着任何原来的索引的排序都将消失。如果查询中有ORDER
BY子句，那么还将会有一个额外的排序步骤。因为这个原因，以及每个额外的索引扫描都会增加额外的时间，这样规划器有时候就会选择使用简单的索引扫描，即使有多个索引可用也会如此。

Bitmap索引主要适用于\ **当表具有很多属性且查询可能会涉及其中任意组合时的情况**\ 。

优化器为普通索引建立位图表，即Bitmap heap table，主要包括2种扫描方式：

-  Bitmap Index
   Scan：用来在内存中创建一个位图表，每一个Bit表示一个与过滤条件有关的页面。Bit上有数据为1，不可能为0。通过位图扫描，能够快速确定某个值在哪些记录上存在与否。

-  Bitmap Heap
   Scan：Bitmap上检索到的记录都对应其rowid，用这个rowid到关系表上去查找完整的记录信息并过滤。


  Bitmap index/heap scan示例：

  .. code::

   create table t1(id int);
   create index idx_t1 on t1(id);
   explain select * from t1 where id = 200;
   
   QUERY PLAN
   ----------------------------------------------------------------------
   Bitmap Heap Scan on t1 (cost=4.26..14.95 rows=13 width=4)
       Recheck Cond: (id = 200)
       -> Bitmap Index Scan on idx_t1 (cost=0.00..4.25 rows=13 width=0)
           Index Cond: (id = 200)
   (4 行记录)

Bitmap索引(索引AM)
^^^^^^^^^^^^^^^

Bitmap索引使用保存在磁盘上的位图结构保存索引信息。与执行节点的Bitmap索引的区别是：执行节点的Bitmap索引是将计算过程需要的数据以位图的形式进行运算，而索引AM的Bitmap索引是将索引信息压缩存储在磁盘上。

在创建Bitmap索引时，系统将扫描目标列，创建内部表并保存列中每一个独特值。然后针对每一个独特值，按其原始数据在表中的位置创建位图并压缩存储在磁盘中。在查询过程中，系统将先查询内部表中记录的目标值位图页的位置，然后在该位置读取目标值位图信息。需要注意的是，考虑到Bitmap索引的存储结构，对Bitmap索引相关列的Update操作将会导致Bitmap索引的解压缩和重压缩，因此性能会较为缓慢。

Bitmap索引适用的限制为：
- 不会进行Update操作的的数据列。
- 列的基数值在1-10000之间。（若为多列索引，则基数值为各列的基数的乘积）

.. code::

    test=# create table test(a int4, b int4);
    CREATE TABLE
    test=# create index idx_t on test using bitmap(a);
    CREATE INDEX
    test=# insert into test select round(random()*3),round(random()*100) from generate_series(1,1000000);
    INSERT 0 1000000
    test=# analyze;
    ANALYZE
    test=# explain analyze select count(*) from test where a = 1;  --使用Bitmap索引查询
    QUERY PLAN                                                                     
    -------------------------------------------------------------------------------------------------------
    Finalize Aggregate  (cost=10198.22..10198.23 rows=1 width=8) (actual time=164.351..166.177 rows=1 loops=1)
    ->  Gather  (cost=10198.00..10198.21 rows=2 width=8) (actual time=164.090..166.161 rows=3 loops=1)
         Workers Planned: 2
         Workers Launched: 2
         ->  Partial Aggregate  (cost=9198.00..9198.01 rows=1 width=8) (actual time=144.485..144.487 rows=1 loops=3)
               ->  Parallel Bitmap Heap Scan on test  (cost=2701.12..8852.69 rows=138125 width=0) (actual time=82.470..129.104 rows=111086 loops=3)
                     Recheck Cond: (a = 1)
                     Heap Blocks: exact=1646
                     ->  Bitmap Index Scan on idx_t  (cost=0.00..2618.25 rows=331500 width=0) (actual time=99.981..99.981 rows=333258 loops=1)
                           Index Cond: (a = 1)
    Planning Time: 0.515 ms
    Execution Time: 166.334 ms
    (12 rows)


GIN索引
^^^^^^^^^^^^^^^^


GIN是通用倒排序索引（Generalized Inverted
Index），它是一个存储对（Key，postion
list）的集合，其中Key是一个键值，而postion
list是包含Key的位置值。比如（'Tom'，'10:25 14:3 29:5'）
就表示关键字'Tom'在这些位置（元组TID）上存在。
当我们用关键字'Tom'去查询的时候，能一下就定位到包含关键字的元组有这三个。

‘Tom’-> TID5->TID10->TID100

‘Ada’-> TID5->TID99

  …

‘Bob’-> TID99->TID110->TID200

给定查询的关键字‘Tom’，立即就能知道包含该关键字的元组在哪里能找到。

通用倒排序索引（GIN）主要适用于\ **包含多个组合值的查询，如数组、全文检索**\ 等。


.. code::

   create table t3(id int, info text);
   insert into t3 values(generate_series(1,10000), md5(random()::text));
   create index i_t3_gin on t3 using gin(to_tsvector('english',info));
   analyze;
   explain analyze select * from t3 where to_tsvector('english', info) @@ plainto_tsquery( 'hello');  --使用GIN索引的全文检索
   
   QUERY PLAN
   ----------------------------------------------------------------------------
   Bitmap Heap Scan on T3  (cost=12.25..16.77 rows=1 width=37) (actual time=0.029..0.029 rows=0 loops=1)
       Recheck Cond: (TO_TSVECTOR('ENGLISH'::REGCONFIG, INFO) @@ PLAINTO_TSQUERY('hello'::TEXT))
       -> Bitmap Index Scan on I_T3_GIN  (cost=0.00..12.25 rows=1 width=0) (actual time=0.026..0.026 rows=0 loops=1)
           Index Cond: (TO_TSVECTOR('ENGLISH'::REGCONFIG, INFO) @@ PLAINTO_TSQUERY('hello'::TEXT))
   Planning time: 0.460 ms
   Execution time: 0.068 ms
   (6 rows)

   
GiST索引
^^^^^^^^^^


GiST是通用的搜索树(Generalized Search Tree)。
它是一种平衡树结构的访问方法，在系统中作为一个基本模版，可以使用它实现任意索引模式。B-trees，R-trees和许多其它的索引模式都可以用GiST实现。

GiST索引适用于多维数据类型和集合数据类型，和Btree索引类似，同样适用于其他的数据类型。GiST可以用来做\ **位置搜索，如包含、相交、左边、右边**\ 等。

以下是一个GiST索引使用示例：


  .. code::

    create table t1(id int, pos1 point);
    insert into t1 select generate_series(1,100000),point(round((random()*1000)::numeric, 2), round((random()*1000)::numeric, 2));
    create index idx_t1_gist on t1 using gist(pos1);
    analyze t1;
    explain select * from t1 where circle '((100,100) 10)' @> pos1;
                                 QUERY PLAN                                 
    ----------------------------------------------------------------------------
    Bitmap Heap Scan on t1  (cost=5.06..272.12 rows=100 width=20)
    Recheck Cond: ('<(100,100),10>'::circle @> pos1)
    ->  Bitmap Index Scan on idx_t1_gist  (cost=0.00..5.03 rows=100 width=0)
         Index Cond: (pos1 <@ '<(100,100),10>'::circle)
    (4 rows)


SP-GiST索引
^^^^^^^^^^^^^^^


SP-GiST是“space-partitioned GiST”的缩写，即空间分区GiST索引。它主要是通过一些新的索引算法提高GiST索引在某个情况下的性能。它与GiST索引一样，是一个通用的索引框架，基于此框架可以开发出自定义的空间分区索引。

要实现一个自定义的SP-Gist索引，需要实现5个自定义函数：

-  Config：返回索引实现中的一些静态信息

-  Choose：选择如何把新的值插入到索引内部tuple中

-  Picksplit：决定如何在一些叶子tuple上创建一个新的内部tuple

-  Inner_consistent：在树的搜索过程中返回一系列树杈上的节点

-  Leaf_consistent：返回叶子节点是否满足查询

SP-GiST适用于\ **空间可以递归分割成不相交区域的结构，包括四叉树、k-D树和基数树。**

KingbaseES已经对一些数据类型实现了SP-GiST索引操作类，在这些类型上可以直接创建SP-GiST索引：

.. table::

   ============== ============ ======================================
   **操作类名称** **数据类型** **索引操作符**
   ============== ============ ======================================
   quad_point_ops Point        <<，<@，<^，>>，>^，~=
   kd_point_ops   Point        <<，<@，<^，>>，>^，~=
   range_ops      Range        &&，&<，&>，-|-，<<，<@，=，>>，@>
   text_ops       Text         <，<=，=，>，>=，~<=~，~<~，~>=~,~>~
   ============== ============ ======================================



以下是SP-GiST使用point的示例：

.. code::

   create table t1(p point);
   create index idx_t1 on t1 using spgist(p);
   analyze t1;
   explain select * from t1 where p << point(10, 10);
   QUERY PLAN
   -----------------------------------------------------------------------
   Bitmap Heap Scan on t1 (cost=5.57..17.87 rows=184 width=16)
       Recheck Cond: (p << '(10,10)'::point)
       -> Bitmap Index Scan on idx_t1 (cost=0.00..5.52 rows=184 width=0)
           Index Cond: (p << '(10,10)'::point)
   (4 rows)



BRIN索引
^^^^^^^^^^^^

BRIN索引是块范围索引的简称，由于其存储了表的连续数据块区间以及对应的数据取值范围，因此BRIN索引的体积和维护代价相比其他索引低很多。

BRIN索引适用于\ **存储流式的数据日志**\ 。例如：按照时间插入的数据。由于数据是按照时间插入，因此数据块上记录的范围信息很少会出现交叉情况，索引过滤后需要比较的数据块也会少很多；反之，如果数据交叉严重，通过索引无法过滤掉任何一个数据块时，操作起来会比全表扫描更加耗时。

.. code::

   create table t5(id int, name text); 
   insert into t5 values(generate_series(1,100000), md5(random()::text));
   analyze;
   create index i5_brin on t5 using brin(id);
   explain analyze select * from t5 where id  < 10; --使用BRIN索引，索引仅能过滤到连续的数据块级别，需要对块内的数据做再次比较
   
   QUERY PLAN
   --------------------------------------------------------------------
   Bitmap Heap Scan on T5  (cost=12.08..48.92 rows=10 width=37) (actual time=0.039..2.498 rows=9 loops=1)
       Recheck Cond: (ID < 10)
       Rows Removed by Index Recheck: 15351
       Heap Blocks: lossy=128
       -> Bitmap Index Scan on I5_BRIN  (cost=0.00..12.07 rows=10 width=0) (actual time=0.028..0.028 rows=1280 loops=1)
           Index Cond: (ID < 10)
   Planning time: 0.178 ms
   Execution time: 2.527 ms
   (8 rows)
   
   
   
   create index i5_btree on t5 using btree(id);
   \di+    --BRIN索引大小远小于BTREE
   
   List of relations
   Schema  |   Name   | Type  | Owner | Table |  Size   |  Description
   --------+----------+-------+-------+-------+---------+-------------
   PUBLIC  | I5_BRIN  | index | nimo  |  T5   | 48 kB   |
   PUBLIC  | I5_BTREE | index | nimo  |  T5   | 2208 kB |
   
   (2 rows)

索引使用技巧
--------------

使用表达式索引
^^^^^^^^^^^^^^^^^^

KingbaseES支持创建基于标量表达式的索引。

最常见的就是创建基于函数的索引。比如在做大小写无关的比较时，经常使用lower(name)
或者upper(name)
函数。但是因为用了函数，无法利用字段name上的普通索引，所以此时需要一个函数索引：


.. code::

   create table t1(name text);
   create index idx_t1 on t1(upper(name)); 
   explain select * from t1 where upper(name) = 'ada';
   
   QUERY PLAN
   -------------------------------------------------------------
   Bitmap Heap Scan on t1 (cost=4.21..14.37 rows=7 width=32)
       Recheck Cond: (upper(name) = 'ada'::text)
       -> Bitmap Index Scan on idx_t1 (cost=0.00..4.21 rows=7 width=0)
           Index Cond: (upper(name) = 'ada'::text)
   (4 行记录)



也可以创建基于复杂表达式的函数索引（表达式需要用括号括起来）：

.. code::

   create table t1(id int, first_name text, last_name text);
   create index idx_t1 on t1((first_name || ' ' || last_name));
   explain select * from t1 where (first_name || ' ' || last_name) = 'Ada B';
   
   QUERY PLAN
   ---------------------------------------------------
   Bitmap Heap Scan on t1 (cost=4.18..12.66 rows=4 width=68)
       Recheck Cond: (((first_name || ' '::text) || last_name) = 'Ada B'::text)
       -> Bitmap Index Scan on idx_t1 (cost=0.00..4.18 rows=4 width=0)
           Index Cond: (((first_name || ' '::text) || last_name) = 'Ada B'::text)
   (4 行记录)


表达式索引里面存储的值是表达式的值，所以它并不是在进行索引查找时去计算表达式的，而是在插入数据行或者更新数据行时进行计算的。因此在插入或者更新数据时，函数索引会慢一些。

使用局部索引
^^^^^^^^^^^^^^^^

局部索引（partial index） 是建立在一个表的子集上的索引，而该子集是由一个条件表达式定义的（叫做局部索引的谓词）。该索引只包含表中那些满足这个谓词的行。
 
由于不是在所有的情况下都需要更新索引，只有新增的符合局部索引条件表达式的数据才会写入到索引里，因此局部索引会提高数据插入和数据更新的效率，减少了索引的维护成本。又因为局部索引比普通索引要小，因此可以更好的提高确实需要索引部分的查询效率。

比如，t1里面有100万行记录，SQL应用进程查询的是 id <
500的那批记录，则可以为它建立局部索引：

.. code::

   create table t1(id int);
   create index idx_t1 on t1(id) where id < 500;
   explain select * from t1 where id < 400;
   
   QUERY PLAN
   -------------------------------------------
   Bitmap Heap Scan on t1 (cost=4.35..24.97 rows=850 width=4)
       Recheck Cond: (id < 400)
       -> Bitmap Index Scan on idx_t1 (cost=0.00..4.13 rows=850 width=0)
           Index Cond: (id < 400)
   (4 行记录)
   
   
   
   explain select * from t1 where id > 400;
   
   QUERY PLAN
   -----------------------------------------------------
   Seq Scan on t1 (cost=0.00..41.88 rows=850 width=4)
       Filter: (id > 400)
   (2 行记录)


可以看出，符合索引条件范围的查询，能够使用创建的局部索引，不符合条件的则不能。

KingbaseES支持带任意谓词的局部索引，只要涉及被索引的表的字段就可以。不过谓词必须和那些希望从该索引中获益的查询中的where条件相匹配。准确的说只有在系统识别出该查询的where条件简单地包含了该索引的谓词时，此局部索引才能用于该查询。KingbaseES还不可以完全识别形式不同但数学上相等的谓词，但可以识别简单的不相等的包含，谓词条件必须准确匹配查询的where条件，不然系统将无法识别该索引是否可用。

由于条件匹配发生在执行计划的规划期间而不是运行期间，因此带绑定变量的条件不能使用局部索引。

使用联合索引
^^^^^^^^^^^^^^^

联合索引是在建立在某个关系表上多列的索引，也叫复合索引。创建联合索引时，应该将最常被访问的列放在索引列表前面。当where子句中引用了联合索引中的所有列，或者前导列，联合索引可以加快检索速度。

.. code::

   create table student(id int, name text, school text);
   create index idx_student on student(id, name, school);



当查询条件为：1）id；2）id和name；3）id和school；4）id、name和school时，都可以使用idx_student联合索引。

当查询条件不包括id时，则无法使用联合索引。

使用索引提升Like模式匹配性能
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

KingbaseES产品支持基于Like表达式的通配符模式匹配，通过合理的应用索引，可以提高查询性能。

前匹配或者精确匹配使用Btree索引
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


普通的btree索引能够在2种情况下支持like操作符使用索引：

1. 精确匹配型like

  .. code::

   create table t1(id int, name text);
   create index idx_t1 on t1(name);
   explain select * from t1 where name like 'abc';
   
   QUERY PLAN
   ----------------------------------------------------------------------
   Bitmap Heap Scan on t1 (cost=4.67..61.54 rows=50 width=36)
       Filter: (name ~~ 'abc'::text)
       -> Bitmap Index Scan on idx_t1 (cost=0.00..4.66 rows=50 width=0)
           Index Cond: (name = 'abc'::text)
   (4 行记录)


  精确匹配下，优化器能够将 name like 'abc' 转换为 name = 'abc'的索引条件。

2. 索引字段为“C”collate

  .. code::

   create table t1(id int, name text collate "C");
   create index idx_t1 on t1(name);
   explain select * from t1 where name like 'abc%'; 
   
   QUERY PLAN
   ----------------------------------------------------------------------
   Bitmap Heap Scan on t1 (cost=4.21..13.68 rows=6 width=36)
       Filter: (name ~~ 'abc%'::text)
       -> Bitmap Index Scan on idx_t1 (cost=0.00..4.21 rows=6 width=0)
           Index Cond: ((name >= 'abc'::text) AND (name < 'abd'::text))
   (4 行记录)


  \Collate 为 "C"的字段做like操作时，也被转换为">= AND <"
  的一对索引条件。

  其他情况则需要使用text_pattern_ops，bpchar_pattern_ops指定的btree索引：

3. like const表达式：

  .. code::

   create table t1(id int, name text);
   create index idx_t1 on t1(name text_pattern_ops);
   explain select * from t1 where name like 'abc%';
   QUERY PLAN
   ---------------------------------------------------------
   Bitmap Heap Scan on t1 (cost=4.21..13.68 rows=6 width=36)
       Filter: (name ~~ 'abc%'::text)
       -> Bitmap Index Scan on idx_t1 (cost=0.00..4.21 rows=6 width=0)
           Index Cond: ((name ~>=~ 'abc'::text) AND (name ~<~ 'abd'::text))
   (4 行记录)



4. like var 表达式：

  .. code::

   create table t1(id int, name text);
   create table t2(id int, name text);
   create index idx_t2 on t2(name text_pattern_ops);
   explain select t1.name, (select count(*) from t2 where t2.name like t1.name || '%') as cnt from t1;


  KingbaseES产品也支持对a like b，其中b为一个变量表达式的情况，使用btree索引。


**限制：**

**Btree索引应用于like模糊查询时，只能做前匹配（a like‘abc%’）或者精确匹配（a like ‘abc’），不能做后匹配（a like ‘%abc’）和中间匹配（a like ‘%abc%’），这是由btree索引只能做大于、大于等于、等于、小于、小于等于等操作决定的。**



后匹配reverse()表达式
~~~~~~~~~~~~~~~~~~~~~~~~~~~~


如果只有后匹配查询需求，比如字符串like ‘%abc’，使用collate “C”的reverse()表达式的Btree索引。字符串like ‘%abc’等价于reverse(字符串) like ‘cba%’，将其转换成类似前匹配的方式。

  .. code::

   explain select * from t1 where reverse(t1.name) like '2%';
   
   QUERY PLAN
   --------------------------------------------------------------------
   Seq Scan on t1 (cost=10000000000.00..10000000017.00 rows=5 width=18)
       Filter: (reverse((name)::text) ~~ '2%'::text)
   (2 rows)
   
   
   create index on t1(reverse(name) collate "C");
   analyze t1;
   explain select * from t1 where reverse(t1.name) like '2%';
   
   QUERY PLAN
   --------------------------------------------------------------------
   Bitmap Heap Scan on t1 (cost=6.20..11.20 rows=200 width=18)
       Filter: (reverse((name)::text) ~~ '2%'::text)
       -> Bitmap Index Scan on t1_reverse_idx (cost=0.00..6.15 rows=200 width=0)
           Index Cond: ((reverse((name)::text) >= '2'::text) AND (reverse((name)::text) < '3'::text))
   (4 rows)


中间匹配使用TRGM索引
~~~~~~~~~~~~~~~~~~~~~~~


sys_trgm是一个扩展模块，它提供函数和操作符测定字母数字文本基于三元模型匹配的相似性，还有支持快速搜索相似字符串的索引操作符类。

三元模型是一组从一个字符串中获得的三个连续的字符。我们可以通过计数两个字符串共享的三元模型的数量来测量它们的相似性。

sys_trgm从一个字符串提取三元模型时忽略非文字字符（非字母）。当确定包含在字符串中的三元模型集合时，
每个单词被认为有两个空格前缀和一个空格后缀。
例如，字符串"cat"中的三元模型的集合是 "c"，"ca"，"cat"和"at"。
字符串"foo|bar"中的三元模型的集合是 "f"，"fo"，"foo"，"oo"，"b"，"ba"，"bar"和"ar"。

sys_trgm模块提供GiST和GIN索引操作符类，
并且额外支持基于三元模型的索引搜索：LIKE，ILIKE，~ 和 ~*查询。

sys_trgm支持like模糊匹配示例：

  .. code::

   create extension sys_trgm;
   create table t1(id int, name varchar(20));
   create table t2(id int, name varchar(20));
   create index idx_t2_trgm on t2 using gin(name gin_trgm_ops);
   explain select t1.id, (select count(*) from t2 where t2.name like t1.name || '%') as cnt from t1;
   
   QUERY PLAN
   --------------------------------------------------------------
   Seq Scan on t1 (cost=0.00..14894.01 rows=900 width=12)
       SubPlan 1
       Aggregate (cost=16.52..16.53 rows=1 width=8)
           -> Bitmap Heap Scan on t2 (cost=8.04..16.51 rows=4 width=0)
               Recheck Cond: ((name)::text ~~ ((t1.name)::text || '%'::text))
               -> Bitmap Index Scan on idx_t2_trgm (cost=0.00..8.04 rows=4 width=0)
                   Index Cond: ((name)::text ~~ ((t1.name)::text || '%'::text))
   (7 行记录)


不过对于like的部分匹配和全匹配情况，trgm索引的性能可能会弱于btree索引。trgm索引并不支持相等也不支持简单的比较操作符，所以你可能也需要一个普通的B-tree索引。

删除不必要的索引
^^^^^^^^^^^^^^^^^^^^^^

通过查看系统表sys_stat_user_indexes，来观察哪些索引表从来没有被使用。对于长期不使用的索引可以删除它们，以便减少磁盘占用空间和维护索引的代价。

.. code::

   select relname, indexrelname, idx_scan, idx_tup_read, idx_tup_fetch from sys_stat_user_indexes order by idx_scan, idx_tup_read, idx_tup_fetch;
   
   relname | indexrelname | idx_scan | idx_tup_read | idx_tup_fetch
   --------+--------------+----------+--------------+---------------
   bar     | idx_btree_bar| 0        | 0            | 0
   t1      | idx_t1_name  | 3        | 1            | 1
   
   (2 rows)

定期vacuum和重建索引
^^^^^^^^^^^^^^^^^^^^^^

KingbaseES数据库执行delete操作后，表中的记录只是被标示为删除状态，并没有释放空间，在以后的update或insert操作中该部分的空间不能够被重用。可以利用数据库磁盘空间工具VACUUM，其作用是删除那些已经标示为删除的数据并释放空间。但vacuum工具不能够对相应的索引进行清理，需要手动去重建索引。 

另外，对于Btree索引，只有那些已经完全清空的索引页才会得到重复使用，对于那些仅部分空间可用的索引页将不会得到重用，如果一个页面中大多数索引键值都被删除，只留下很少的一部分，那么该页将不会被释放并重用。在这种极端的情况下，由于每个索引页面的利用率极低，一旦数据量显著增加，将会导致索引文件变得极为庞大，不仅降低了查询效率，而且还存在整个磁盘空间被完全填满的危险。对于重建后的索引还存在另外一个性能上的优势，因为在新建立的索引上，逻辑上相互连接的页面在物理上往往也是连在一起的，这样可以提高磁盘页面被连续读取的几率，从而提高整个操作的IO效率。

可以在以下几个级别重建索引：

-  数据库： reindex database d1;

-  表：reindex table t1;

-  单个索引：reindex index idx1;

重置索引后，需要执行 ANALYZE命令重新分析一下表。

其他建议
^^^^^^^^^^^^^^

索引的使用通常能够提高select，update以及delete语句的性能，但会降低insert语句的性能，因此索引并非是越多越好，使用索引应该遵循以下原则：

-  仅当要通过索引访问表中很少的一部分记录（1%~20%）

-  更新较少的表可以考虑使用覆盖索引

覆盖索引扫描，允许直接从索引得到元组，
覆盖索引扫描要求查询中的某个表所需要的数据均可以从这个表上的同一个索引的索引页面中获得。

使用覆盖索引之前,需要先对表进行 VACUUM
才能使覆盖索引被优化器使用。对于更新频繁的表，需要多次 VACUUM。

**不合适建索引的场合：**

-  索引列上有函数（确定性的函数可以创建函数索引）

-  索引列选择率差，不如全表扫描

-  不要索引常用的小型表，其维护代价有时会高于其收益

-  不要索引大型字段（有很多字符），这样作会让索引占用太多的存储空间

使用索引建议工具
--------------------

索引建议根据数据库服务器运行过程中where和join里的统计信息，根据SQL语句提示哪些关系表如果创建了相应的索引后，性能会有很大提升。

索引建议能够针对：select、update、delete、insert
into…select、explain等语句，提供包括：btree、bitmap、hash、gin、gist、brin、spgist访问类型的索引建议。

索引建议列表里面包括的信息有：SQL语句、索引建议（create index
DDL）、当前无索引时cost、创建索引后的cost、cost提升百分比等信息。

索引建议的使用，需要使用 KingbaseES V8R6的版本。其基本使用方法如下：

1. 编辑kingbase.conf文件，添加以下内容后重启服务器：

  .. code::

   shared_preload_libraries = ‘sys_stat_statements, sys_qualstats’
   sys_qualstats.enabled = true



2. 创建相关扩展组件：

  .. code::

   create extension sys_qualstats;
   create extension sys_hypo;



3. 执行SQL，查询索引建议：

  .. code::

   select * from t1 where id = 100;
   select * from index_recommendation_by_qual;
   select * from index_recommendation_by_index;



具体使用情况，请参考 《 :ref:`KingbaseES插件参考手册` 》  。


