我对 Postgres 全文搜索比较陌生,但仍在尝试理解它。我正在研究如何在 PostgreSQL 全文搜索中优化此查询。查询如下所示:
SELECT articles.article_id, article_title, article_excerpt, article_author, article_link_perm, article_default_image, article_date_added, article_bias_avg, article_rating_avg, article_keywords,
ts_rank(search_vector, to_tsquery('snowden|obama|nsa')) AS rank
FROM development.articles
WHERE search_vector @@ to_tsquery('english', 'snowden|obama|nsa') AND ts_rank(search_vector, to_tsquery('snowden|obama|nsa')) > .045 ORDER BY article_date_added DESC, rank DESC LIMIT 20
EXPLAN ANAYLIZE 看起来像这样:
Limit (cost=20368.26..20368.31 rows=20 width=751) (actual time=276.006..276.101 rows=20 loops=1)
-> Sort (cost=20368.26..20376.91 rows=3459 width=751) (actual time=276.001..276.035 rows=20 loops=1)
Sort Key: article_date_added, (ts_rank(search_vector, to_tsquery('snowden|obama|nsa'::text)))
Sort Method: top-N heapsort Memory: 42kB
-> Bitmap Heap Scan on articles (cost=1136.19..20276.22 rows=3459 width=751) (actual time=22.735..273.558 rows=600 loops=1)
Recheck Cond: (search_vector @@ '( ''snowden'' | ''obama'' ) | ''nsa'''::tsquery)
Filter: (ts_rank(search_vector, to_tsquery('snowden|obama|nsa'::text)) > 0.045::double precision)
-> Bitmap Index Scan on article_search_vector_index (cost=0.00..1135.33 rows=10377 width=0) (actual time=20.512..20.512 rows=9392 loops=1)
Index Cond: (search_vector @@ '( ''snowden'' | ''obama'' ) | ''nsa'''::tsquery)
Total runtime: 276.674 ms
正在使用的索引是 GIN,因为我更关心搜索和更新。我注意到这个查询的一些问题是更多的'|' 我补充说,它变得越慢。有哪些方法可以优化此查询以仍然快速获得不错的结果?