我使用的是PostgreSQL
9.3版本。
我刚刚了解到正常的b-tree
索引不能用于'x%x%x'通配符的查询,我发现gist
和gin
索引使我的通配符查询命中索引。
所以我决定使用这些索引,并通过在PostgreSQL中使用命令添加btree-gist
扩展;
CREATE EXTENSION btree_gist;
之后,我把我的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)
我看到它没有击中索引。我跟着教程走,却看不到解决方案,甚至看不到问题所在。关于主旨索引,我遗漏了什么?
就像a_horse_with_no_name说的,你选错了扩展名。
btree_gist扩展允许您使用索引来支持"距离"操作符。它还可以让你有一个跨多个列的要点索引,其中一个可能是一个搜索向量,另一个可能是一个简单的int。
pg_trgm扩展执行匹配字符串中的任意位置索引。注意,这是一个非常昂贵的索引。