Postgresql 9.4:索引无法在模式搜索中工作

时间:2015-10-09 17:50:23

标签: postgresql postgresql-9.4

我有一个名为“医生”的表和一个名为“fullname”的字段,它将存储带有重音符号的名称。 我需要做的是“重音不敏感+不区分大小写”搜索,例如:

SELECT * 
FROM doctors
WHERE unaccent_t(fullname) ~* 'unaccented_and_lowercase_string';

其中要搜索的值将是非重音+小写,而unaccent_t是一个定义为的函数:

CREATE FUNCTION unaccent_t(text, lowercase boolean DEFAULT false)
RETURNS text AS
$BODY$
SELECT CASE
  WHEN $2 THEN unaccent('unaccent', lower(trim($1)))
  ELSE unaccent('unaccent', trim($1))
END;
$BODY$ LANGUAGE sql IMMUTABLE SET search_path = public, pg_temp;

(我已经安装了'unaccent'扩展名。)

所以,我继续为“fullname”字段创建了索引:

CREATE INDEX doctors_fullname ON doctors (unaccent_t(fullname) text_pattern_ops);

(我也试过varchar_pattern_ops,也没有指定操作)

在医生表中,我有大约15K行。

查询有效,我得到了预期的结果,但是当我向查询添加explain analyze时,我没有看到使用了索引:

Seq Scan on doctors  (cost=0.00..4201.76 rows=5 width=395) (actual time=0.282..182.025 rows=15000 loops=1)
  Filter: (unaccent_t((fullname)::text, false) ~* 'garcia'::text)
  Rows Removed by Filter: 1
Planning time: 0.207 ms
Execution time: 183.387 ms

我也尝试从unaccent_t中删除可选参数,但结果相同。

在这样的场景中,我应该如何定义索引以便在上面的查询中使用它?

1 个答案:

答案 0 :(得分:1)

仅当模式保持锚定时,Btree索引才可用于加速操作。

从PostgreSQL 9.3开始,您可以使用GIN或GiST索引以及pg_trgm contrib模块提供的运算符类来加速通用正则表达式搜索。

您可以在http://www.postgresql.org/docs/9.4/static/pgtrgm.html#AEN163078

的PostgreSQL手册上阅读更多相关信息