索引使用约束

下面是一个使用索引的例子:

openGauss=# create table table1 (c_int int,c_bigint bigint,c_varchar varchar,c_text text) with(orientation=row);

openGauss=# create text search configuration ts_conf_1(parser=POUND);
openGauss=# create text search configuration ts_conf_2(parser=POUND) with(split_flag='%');

openGauss=# set default_text_search_config='ts_conf_1';
openGauss=# create index idx1 on table1 using gin(to_tsvector(c_text));

openGauss=# set default_text_search_config='ts_conf_2';
openGauss=# create index idx2 on table1 using gin(to_tsvector(c_text));

openGauss=# 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索引可用。

意见反馈
编组 3备份
openGauss 2024-03-19 00:47:31
取消