优化postgres相似性查询(pg_trgm + gin索引)

时间:2017-05-09 10:33:09

标签: postgresql similarity postgresql-9.6 trigram pg-trgm

我定义了以下索引:

CREATE INDEX
    users_search_idx
ON
    auth_user
USING
    gin(
        username gin_trgm_ops,
        first_name gin_trgm_ops,
        last_name gin_trgm_ops
    );

我正在执行以下查询:

PREPARE user_search (TEXT, INT) AS
    SELECT
        username,
        email,
        first_name,
        last_name,
        ( -- would probably do per-field weightings here
            s_username + s_first_name + s_last_name
        ) rank
    FROM
        auth_user,
        similarity(username, $1) s_username,
        similarity(first_name, $1) s_first_name,
        similarity(last_name, $1) s_last_name
    WHERE
        username % $1 OR
        first_name % $1 OR
        last_name % $1
    ORDER BY
        rank DESC
    LIMIT $2;

auth_user表有620万行。

查询的速度似乎在很大程度上取决于similarity查询可能返回的结果数。

通过set_limit增加相似性阈值会有所帮助,但会通过消除部分匹配来降低结果的实用性。

有些搜索会在200毫秒后返回,其他搜索需要大约10秒钟。

我们使用Elasticsearch返回<任何查询都要200ms,同时做更复杂(更好)的排名。

我想知道是否有任何方法可以改善这一点以获得更一致的性能?

我的理解是GIN索引(倒排索引)与Elasticsearch使用的基本方法相同,所以我认为可以进行一些优化。

EXPLAIN ANALYZE EXECUTE user_search('mel', 20)显示:

Limit  (cost=54099.81..54099.86 rows=20 width=52) (actual time=10302.092..10302.104 rows=20 loops=1)
  ->  Sort  (cost=54099.81..54146.66 rows=18739 width=52) (actual time=10302.091..10302.095 rows=20 loops=1)
        Sort Key: (((s_username.s_username + s_first_name.s_first_name) + s_last_name.s_last_name)) DESC
        Sort Method: top-N heapsort  Memory: 26kB
        ->  Nested Loop  (cost=382.74..53601.17 rows=18739 width=52) (actual time=118.164..10293.765 rows=8380 loops=1)
              ->  Nested Loop  (cost=382.74..53132.69 rows=18739 width=56) (actual time=118.150..10262.804 rows=8380 loops=1)
                    ->  Nested Loop  (cost=382.74..52757.91 rows=18739 width=52) (actual time=118.142..10233.990 rows=8380 loops=1)
                          ->  Bitmap Heap Scan on auth_user  (cost=382.74..52383.13 rows=18739 width=48) (actual time=118.128..10186.816 rows=8380loops=1)"
                                Recheck Cond: (((username)::text % 'mel'::text) OR ((first_name)::text % 'mel'::text) OR ((last_name)::text %'mel'::text))"
                                Rows Removed by Index Recheck: 2434523
                                Heap Blocks: exact=49337 lossy=53104
                                ->  BitmapOr  (cost=382.74..382.74 rows=18757 width=0) (actual time=107.436..107.436 rows=0 loops=1)
                                      ->  Bitmap Index Scan on users_search_idx  (cost=0.00..122.89 rows=6252 width=0) (actual time=40.200..40.200rows=88908 loops=1)"
                                            Index Cond: ((username)::text % 'mel'::text)
                                      ->  Bitmap Index Scan on users_search_idx  (cost=0.00..122.89 rows=6252 width=0) (actual time=43.847..43.847rows=102028 loops=1)"
                                            Index Cond: ((first_name)::text % 'mel'::text)
                                      ->  Bitmap Index Scan on users_search_idx  (cost=0.00..122.89 rows=6252 width=0) (actual time=23.387..23.387rows=58740 loops=1)"
                                            Index Cond: ((last_name)::text % 'mel'::text)
                          ->  Function Scan on similarity s_username  (cost=0.00..0.01 rows=1 width=4) (actual time=0.004..0.004 rows=1 loops=8380)
                    ->  Function Scan on similarity s_first_name  (cost=0.00..0.01 rows=1 width=4) (actual time=0.002..0.002 rows=1 loops=8380)
              ->  Function Scan on similarity s_last_name  (cost=0.00..0.01 rows=1 width=4) (actual time=0.002..0.002 rows=1 loops=8380)
Execution time: 10302.559 ms

Server是在Amazon RDS上运行的Postgres 9.6.1

更新

1。

发布问题后不久,我发现了这个信息:https://www.postgresql.org/message-id/464F3C5D.2000700@enterprisedb.com

所以我试过

-> SHOW work_mem;
4MB
-> SET work_mem='12MB';
-> EXECUTE user_search('mel', 20);
(results returned in ~1.5s)

这取得了很大的进步(之前> 10s)!

对于类似的查询,

1.5s仍然比ES慢,所以我仍然希望听到有关优化查询的任何建议。

2。

在回复评论时,在看到这个问题(Postgresql GIN index slower than GIST for pg_trgm)之后,我尝试用GIST索引代替GIN索引完全相同的设置。

尝试上面的相同搜索,它使用默认work_mem='4MB'以~3.5秒返回。增加work_mem没有任何区别。

由此我得出结论,GIST索引的内存效率更高(没有像GIN那样遇到病态情况)但是当GIN正常工作时比GIN慢。这与推荐GIN索引的文档中描述的内容一致。

3。

我仍然不明白为什么花费这么多时间:

 ->  Bitmap Heap Scan on auth_user  (cost=382.74..52383.13 rows=18739 width=48) (actual time=118.128..10186.816 rows=8380loops=1)"
     Recheck Cond: (((username)::text % 'mel'::text) OR ((first_name)::text % 'mel'::text) OR ((last_name)::text %'mel'::text))"
     Rows Removed by Index Recheck: 2434523
     Heap Blocks: exact=49337 lossy=53104

我不明白为什么需要这个步骤或者它正在做什么。

每个Bitmap Index Scan子句下面都有三个username % $1 ...这些结果然后与BitmapOr步骤结合在一起。这些部分都很快。

但即使在我们没有用完工作的情况下,我们仍会在Bitmap Heap Scan花费近一秒钟。

1 个答案:

答案 0 :(得分:2)

我希望通过这种方法可以更快地获得

1。

创建一个GiST索引,其中1列保存连接值:

CREATE INDEX users_search_idx ON auth_user
USING gist((username || ' ' || first_name || ' ' || last_name) gist_trgm_ops);

这假设所有3列都被定义为NOT NULL(您没有指定)。否则你需要做更多。
为什么不简化concat_ws()

2

使用匹配上述索引的正确查询:

SELECT username, email, first_name, last_name
     , similarity(username  , $1) AS s_username
     , similarity(first_name, $1) AS s_first_name
     , similarity(last_name , $1) AS s_last_name
     , row_number() OVER () AS rank  -- greatest similarity first
FROM   auth_user
WHERE     (username || ' ' || first_name || ' ' || last_name) %   $1  -- !!
ORDER  BY (username || ' ' || first_name || ' ' || last_name) <-> $1  -- !!
LIMIT  $2;

WHEREORDER BY中的表达式必须与索引表达式匹配!

特别是ORDER BY rank(就像你拥有它一样)对于从更大的合格行池中进行小LIMIT选择总是表现不佳,因为它不能直接使用索引:背后的复杂表达必须为每个符合条件的行计算rank,然后必须对所有符号进行排序,然后才能返回小的最佳匹配项。与真正的最近邻居查询相比, 更多,更昂贵 ,可以直接从索引中选择最佳结果,而无需查看其余内容。

带有空窗口定义的

row_number()仅反映同一ORDER BY的{​​{1}}生成的排序。

相关答案:

至于您的商品SELECT,我添加了您引用的问题的答案,应该解释一下: