PostgreSQL 全文搜索 : why search is sooo slow?

标签 postgresql full-text-search full-text-indexing

我有一个小型 PostgreSQL 数据库(~~3,000 行)。

我正在尝试在其中一个文本字段(“正文”)上设置全文搜索。

问题是任何查询都非常慢(35 秒以上!!!)。

我想问题出在数据库选择顺序扫描模式...

这是我的查询:

    SELECT
        ts_rank_cd(to_tsvector('italian', body), query),
        ts_headline('italian', body, to_tsquery('torino')),
        title,
        location,
        id_author
    FROM
        fulltextsearch.documents, to_tsquery('torino') as query
    WHERE
        (body_tsvector @@ query)
    OFFSET
        0

这是解释分析:

                                      QUERY PLAN                                    
----------------------------------------------------------------------------------------------------------------------------
Limit  (cost=0.00..1129.81 rows=19 width=468) (actual time=74.059..13630.114 rows=863 loops=1)
->  Nested Loop  (cost=0.00..1129.81 rows=19 width=468) (actual time=74.056..13629.342 rows=863 loops=1)
     Join Filter: (documents.body_tsvector @@ query.query)
     ->  Function Scan on to_tsquery query  (cost=0.00..0.01 rows=1 width=32) (actual time=4.606..4.608 rows=1 loops=1)
     ->  Seq Scan on documents  (cost=0.00..1082.09 rows=3809 width=591) (actual time=0.045..48.072 rows=3809 loops=1)
Total runtime: 13630.720 ms

这是我的 table :

mydb=# \d+ fulltextsearch.documents;
                                              Table "fulltextsearch.documents"
    Column     |       Type        |                               Modifiers                               | Storage  | Description
---------------+-------------------+-----------------------------------------------------------------------+----------+-------------
 id            | integer           | not null default nextval('fulltextsearch.documents_id_seq'::regclass) | plain    |
 id_author     | integer           |                                                                       | plain    |
 body          | character varying |                                                                       | extended |
 title         | character varying |                                                                       | extended |
 location      | character varying |                                                                       | extended |
 date_creation | date              |                                                                       | plain    |
 body_tsvector | tsvector          |                                                                       | extended |
Indexes:
    "fulltextsearch_documents_tsvector_idx" gin (to_tsvector('italian'::regconfig,     COALESCE(body, ''::character varying)::text))
    "id_idx" btree (id)
Triggers:
    body_tsvectorupdate BEFORE INSERT OR UPDATE ON fulltextsearch.documents FOR EACH ROW EXECUTE PROCEDURE tsvector_update_trigger('body_tsvector', 'pg_catalog.italian', 'body')
Has OIDs: no

我确定我遗漏了一些明显的东西......

有什么线索吗?

.

.

.

=== 更新 ==================================== ===================================

感谢您的建议,我想出了这个(更好的)查询:

SELECT
    ts_rank(body_tsvector, query),
    ts_headline('italian', body, query),
    title,
    location
FROM
    fulltextsearch.documents, to_tsquery('italian', 'torino') as query
WHERE
    to_tsvector('italian', coalesce(body,'')) @@ query

这是相当好,但总是很慢(13 秒以上...)。

我注意到注释掉“ts_headline()”行查询速度快如闪电。

这是 EXPLAIN ANALYZE,它最终使用了索引,但对我帮助不大...:

EXPLAIN ANALYZE SELECT
clock_timestamp() - statement_timestamp() as elapsed_time,
    ts_rank(body_tsvector, query),
    ts_headline('italian', body, query),
    title,
    location
FROM
    fulltextsearch.documents, to_tsquery('italian', 'torino') as query
WHERE
    to_tsvector('italian', coalesce(body,'')) @@ query

 Nested Loop  (cost=16.15..85.04 rows=19 width=605) (actual time=102.290..13392.161 rows=863 loops=1)
   ->  Function Scan on query  (cost=0.00..0.01 rows=1 width=32) (actual time=0.008..0.009 rows=1 loops=1)
   ->  Bitmap Heap Scan on documents  (cost=16.15..84.65 rows=19 width=573) (actual time=0.381..4.236 rows=863 loops=1)
         Recheck Cond: (to_tsvector('italian'::regconfig, (COALESCE(body, ''::character varying))::text) @@ query.query)
         ->  Bitmap Index Scan on fulltextsearch_documents_tsvector_idx  (cost=0.00..16.15 rows=19 width=0) (actual time=0.312..0.312 rows=863 loops=1)
               Index Cond: (to_tsvector('italian'::regconfig, (COALESCE(body, ''::character varying))::text) @@ query.query)
 Total runtime: 13392.717 ms

最佳答案

您遗漏了两件(相当明显的)事情:

1 您已经在 to_tsvector() 中设置了 'italian' 但您没有在 to_tsquery() 中指定它p>

保持一致。

2 您已将 COALESCE(body, ...) 编入索引,但这不是您要搜索的内容。

规划器并不神奇 - 如果您要搜索的是索引,则只能使用索引。

关于PostgreSQL 全文搜索 : why search is sooo slow?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16086398/

相关文章:

postgresql - 使用 EF Core 和 Npgsql 查询行(复合)值

postgresql - 通过方解石在 postgres 中创建物化 View

django - 带有django-haystack的Elasticsearch自动运行更新索引

mysql全文检索权索引

java - 适用于 Google App Engine 的最佳 Java 文本索引库是什么?

postgresql - Postgres全文忽略xml标签

postgresql - 更改数据库排序规则,Postgresql 中的 Ctype

ruby-on-rails - 为字段创建 PostgreSQL 序列(不是记录的 ID)

linq - 使用 "Contains"时忽略 LINQ 中的尖音符号

database - SQL Server 2008 问题上的全文搜索实现