ITPub博客

首页 > 数据库 > > 表和索引-索引使用约束

表和索引-索引使用约束

作者:GaussDB_数据库 时间:2020-01-15 17:40:28 0 删除 编辑

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

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 tscp_u_m_005_tbl 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索引可用。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69959250/viewspace-3170705/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2020-01-06

  • 博文量
    2907
  • 访问量
    19196