
# 索引使用约束
下面是一个使用索引的例子，由于SQL_ASCII的数据库编码格式不支持中文字符，请在Encoding为UTF8/GBK的数据库中执行以下示例：
```
create table table1 (c_int int,c_bigint bigint,c_varchar varchar,c_text text) with(orientation=row);
create text search configuration ts_conf_1(parser=POUND);
create text search configuration ts_conf_2(parser=POUND) with(split_flag='%');
set default_text_search_config='ts_conf_1';
create index idx1 on table1 using gin(to_tsvector(c_text));
set default_text_search_config='ts_conf_2';
create index idx2 on table1 using gin(to_tsvector(c_text));
select c_varchar,to_tsvector(c_varchar) from table1 where to_tsvector(c_text) @@ plainto_tsquery('￥#@……&**')   and to_tsvector(c_text) @@ plainto_tsquery('某公司 ')   and c_varchar is not null order by 1 desc limit 3;
```
该例子的关键点是表table1的同一个列c_text上建立了两个GIN索引：idx1和idx2，但这两个索引是在不同default_text_search_config的设置下建立的。该例子和同一张表的同一个列上建立普通索引的不同之处在于：
- GIN索引使用了不同的parser（即分隔符不同），那么idx1和idx2的索引数据是不同的；
- 在同一张表的同一个列上建立的多个普通索引的索引数据是相同的；
因此当执行同一个查询时，使用idx1和idx2查询出的结果是不同的。
#### 使用约束
通过上面的例子，索引使用满足如下条件时：
- 在同一个表的同一个列上建立了多个GIN索引；
- 这些GIN索引使用了不同的parser（即分隔符不同）；
- 在查询中使用了该列，且执行计划中使用索引进行扫描； 为了避免使用不同GIN索引导致查询结果不同的问题，需要保证在物理表的一列上只有一个gin索引可用。
  
