簡體   English   中英

提高我的psql查詢性能

[英]Improve my psql query performance

我有一個執行奇數的psql查詢。 我在我現在用於搜索的2列上定義了一個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)

任何想法都非常感謝! 謝謝!

編輯:

我認為與結果數量有關,沒有結果的查詢需要更長時間?

發現了問題。

改變了這樣的索引:

"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)

和:

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 ms到總運行時間:26.433 ms。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM