提高我的psql查询性能

3

我有一个在执行时表现奇怪的psql查询。我为现在用于搜索的两个列定义了一个GIN索引:

Indexes:
"pk_products" PRIMARY KEY, btree (id)
"fk_affiliate_affiliate_product_id" UNIQUE, btree (affiliate_id, affiliate_product_id)
"idx_products" btree (merchant_id)
"idx_products_affiliates" btree (affiliate_id)
"idx_products_brand_id" btree (brand_id)
"idx_products_ts" gin (to_tsvector('english'::regconfig, (COALESCE(title, ''::character varying)::text || ' '::text) || COALESCE(description, ''::text)))

如果我搜索一个长度为4个字符的短词,查询速度会很快:

EXPLAIN ANALYZE SELECT p.id, p.price, p.currency, p.images, p.merchant_id
FROM products AS p
WHERE deleted=false AND to_tsvector('english', p.title || coalesce(p.description, '')) @@ to_tsquery('blue:*')
LIMIT 30 OFFSET 0;

结果:

查询计划


(以下内容需要更多上下文信息才能进行翻译)
 Limit  (cost=0.00..219.47 rows=30 width=49) (actual time=3.138..40.914 rows=30 loops=1)
     ->  Seq Scan on products p  (cost=0.00..41120.86 rows=5621 width=49) (actual time=2.740..40.478 rows=30 loops=1)
     Filter: ((NOT deleted) AND (to_tsvector('english'::regconfig, ((title)::text || COALESCE(description, ''::text))) @@ to_tsquery('blue:*'::text)))
     Rows Removed by Filter: 153
  Total runtime: 40.986 ms
  (5 rows)

如果我使用一个更长的单词:

EXPLAIN ANALYZE SELECT p.id, p.price, p.currency, p.images, p.merchant_id
FROM products AS p
WHERE deleted=false AND to_tsvector('english', p.title || coalesce(p.description, '')) @@ to_tsquery('turquoise:*')
LIMIT 30 OFFSET 0;

时间增加了:

查询计划

 Limit  (cost=0.00..219.47 rows=30 width=49) (actual time=1.097..1579.187 rows=30 loops=1)
   ->  Seq Scan on products p  (cost=0.00..41120.86 rows=5621 width=49) (actual time=1.093..1579.129 rows=30 loops=1)
     Filter: ((NOT deleted) AND (to_tsvector('english'::regconfig, ((title)::text || COALESCE(description, ''::text))) @@ to_tsquery('turquoise:*'::text)))
     Rows Removed by Filter: 12697
 Total runtime: 1579.287 ms
 (5 rows)

如果我在单词中使用“-”,那么获取结果的时间会非常长:

EXPLAIN ANALYZE SELECT p.id, p.price, p.currency, p.images, p.merchant_id
FROM products AS p
WHERE deleted=false AND to_tsvector('english', p.title || coalesce(p.description, '')) @@ to_tsquery('turquoise-blue:*')
LIMIT 30 OFFSET 0;

结果:

查询计划

 Limit  (cost=0.00..41120.86 rows=2 width=49) (actual time=31400.164..31400.164 rows=0 loops=1)
   ->  Seq Scan on products p  (cost=0.00..41120.86 rows=2 width=49) (actual time=31400.158..31400.158 rows=0 loops=1)
     Filter: ((NOT deleted) AND (to_tsvector('english'::regconfig, ((title)::text || COALESCE(description, ''::text))) @@ to_tsquery('turquoise-blue:*'::text)))
     Rows Removed by Filter: 281510
 Total runtime: 31400.247 ms
 (5 rows)

非常感谢您的想法!谢谢!

编辑:

我认为这与结果数量有关,没有结果的查询会花费更长的时间吗?

1个回答

1
发现了问题。
将索引更改为如下所示:
"idx_products_ts" gin (to_tsvector('english'::regconfig, title::text || description))

现在查询非常快!

EXPLAIN ANALYZE SELECT p.id, p.price, p.currency, p.images, p.merchant_id
FROM products AS p
WHERE deleted=false AND to_tsvector('english', p.title || p.description) @@ to_tsquery('blue:*')
LIMIT 30 OFFSET 0;

查询计划

Limit  (cost=103.64..183.47 rows=30 width=49) (actual time=15.588..15.644 rows=30 loops=1)
 ->  Bitmap Heap Scan on products p  (cost=103.64..15084.42 rows=5630 width=49) (actual time=15.586..15.633 rows=30 loops=1)
     Recheck Cond: (to_tsvector('english'::regconfig, ((title)::text || description)) @@ to_tsquery('blue:*'::text))
     Filter: (NOT deleted)
     ->  Bitmap Index Scan on idx_products_ts  (cost=0.00..102.23 rows=5630 width=0) (actual time=12.955..12.955 rows=26747 loops=1)
           Index Cond: (to_tsvector('english'::regconfig, ((title)::text || description)) @@ to_tsquery('blue:*'::text))
Total runtime: 15.714 ms
(7 rows)

AND:

EXPLAIN ANALYZE SELECT p.id, p.price, p.currency, p.images, p.merchant_id
FROM products AS p
WHERE deleted=false AND to_tsvector('english', p.title || p.description) @@ to_tsquery('turquoise-blue:*')
LIMIT 30 OFFSET 0;

查询计划

Limit  (cost=108.02..116.02 rows=2 width=49) (actual time=26.234..26.234 rows=0 loops=1)
  ->  Bitmap Heap Scan on products p  (cost=108.02..116.02 rows=2 width=49) (actual time=26.226..26.226 rows=0 loops=1)
     Recheck Cond: (to_tsvector('english'::regconfig, ((title)::text || description)) @@ to_tsquery('turquoise-blue:*'::text))
     Filter: (NOT deleted)
     ->  Bitmap Index Scan on idx_products_ts  (cost=0.00..108.02 rows=2 width=0) (actual time=26.209..26.209 rows=0 loops=1)
           Index Cond: (to_tsvector('english'::regconfig, ((title)::text || description)) @@ to_tsquery('turquoise-blue:*'::text))
Total runtime: 26.433 ms
(7 rows)

从总运行时间31400.247毫秒到总运行时间26.433毫秒的巨大改进。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接