我使用 Postgresql 9.4,我有一个名为 foo 的大表。我想搜索它,但如果搜索文本很短(例如“v”)或很长(例如“这是一个在表 foo% 上使用 gin 的搜索示例”),我的执行时间会很长。在这种情况下,我的索引被忽略。这是我的搜索查询:
EXPLAIN (ANALYZE, TIMING)
SELECT "foo".* FROM "foo" WHERE "foo"."locale" = 'de'
AND f_unaccent(foo.name) ILIKE f_unaccent('v%')
AND foo.configuration->'bar' @> '{"is":["a"]}'
LIMIT 100;
Run Code Online (Sandbox Code Playgroud)
这是我的索引:
CREATE INDEX index_foo_on_name_de_gin ON foo USING gin(f_unaccent(name) gin_trgm_ops) WHERE locale = 'de';
Run Code Online (Sandbox Code Playgroud)
为什么索引被忽略并使用seq scan和/或Bitmap heap scan?如何添加其他索引来解决此问题?
为什么它会重新检查?
Recheck Cond: ((f_unaccent((name)::text) ~~* 'v%'::text) AND ((locale)::text = 'de'::text))
Run Code Online (Sandbox Code Playgroud)
功能f_unaccent:
CREATE OR REPLACE FUNCTION f_unaccent(text)
RETURNS text AS
$func$
SELECT unaccent('unaccent', $1)
$func$ LANGUAGE sql IMMUTABLE …Run Code Online (Sandbox Code Playgroud) postgresql performance index index-tuning postgresql-9.4 postgresql-performance