【问题标题】:Postgresql gist index is not hit未命中 Postgresql 要点索引
【发布时间】:2014-03-24 12:17:44
【问题描述】:

我使用的是PostgreSQL 9.3 版。

我刚刚了解到普通的b-tree 索引不能用于带有'x%x%x' 通配符的类似查询,我发现gistgin 索引可以使我的通配符类似查询命中索引。

所以我决定使用这些索引,并在 PostgreSQL 中使用命令添加了btree-gist 扩展;

CREATE EXTENSION btree_gist;

之后我将我的要点索引添加到我的表中,就像;

CREATE INDEX ix_bras_interface_name ON bras_interface USING GIST (name);

我加了这个之后,才发现真的加了;

   table_name   |                index_name                 |    column_name
----------------+-------------------------------------------+-------------------
 bras_interface | bras_interface_context_id                 | context_id
 bras_interface | bras_interface_domain_id                  | domain_id
 bras_interface | bras_interface_managed_object_id          | managed_object_id
 bras_interface | bras_interface_name_59ec675d0b9537ac_uniq | context_id
 bras_interface | bras_interface_name_59ec675d0b9537ac_uniq | name
 bras_interface | bras_interface_name_idx                   | name
 bras_interface | bras_interface_pkey                       | id
 bras_interface | bras_interface_task_id                    | task_id
 **bras_interface | ix_bras_interface_name                    | name**
(9 rows)

但是,我的简单查询仍然没有命中索引。这是我的简单查询及其分析结果;

my_user=# explain ANALYZE select * from bras_interface where name like '%my_text%';
                                                 QUERY PLAN
-------------------------------------------------------------------------------------------------------------
 Seq Scan on bras_interface  (cost=0.00..764.95 rows=1 width=1420) (actual time=5.589..5.589 rows=0 loops=1)
   Filter: ((name)::text ~~ '%my_text%'::text)
   Rows Removed by Filter: 27596
 Total runtime: 5.613 ms
(4 rows)

我发现它没有达到索引。我遵循了有关该问题的教程,但看不到解决方案,甚至看不到问题。 我对 gist 索引缺少什么?

【问题讨论】:

  • 您需要模块pg_trgm 才能使用'%my_text%' 的索引:postgresql.org/docs/current/static/pgtrgm.html
  • 我没有完全理解。在您提供的文档中,它说; 从 PostgreSQL 9.1 开始,这些索引类型还支持 LIKE 和 ILIKE 的索引搜索。当你说'需要模块 pg_term 才能使用索引'时,你的意思是什么。使用 pg_term 不是另一种索引用法吗?
  • 你需要安装那个扩展,否则索引不会被使用。见这里depesz.com/2011/02/19/waiting-for-9-1-faster-likeilike

标签: postgresql


【解决方案1】:

就像 a_horse_with_no_name 所说,你选错了扩展名。

btree_gist 扩展允许您使用索引来支持“距离”运算符。它还允许您拥有一个跨越多个列的 gist 索引,其中一个列可能是例如一个 tsearch 向量和另一个简单的 int。

pg_trgm 扩展是匹配字符串索引的扩展。请注意,它是一个相当昂贵的索引。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-21
    • 1970-01-01
    • 2019-10-03
    • 2011-05-03
    • 2016-05-28
    • 2020-06-10
    相关资源
    最近更新 更多