简体   繁体   English

Postgres 查询有时需要很长时间

[英]Postgres query is sometimes taking a very long time

I have implemented a FTS engine in my website using GIN tsvector and it works quite well, but there are a few times when it seems to take a very long time, for no specific reason.我在我的网站上使用 GIN tsvector 实现了一个 FTS 引擎,它工作得很好,但有几次似乎需要很长时间,没有具体原因。 I am copying the output of the EXPLAIN ANALYE command below:我正在复制下面的 EXPLAIN ANALYE 命令的 output:

sitedb=# EXPLAIN ANALYZE SELECT id, title FROM post_1 WHERE search_vector @@   to_tsquery('quantum');
                                                      QUERY PLAN                                                           
-------------------------------------------------------------------------------------------------------------------------------
 Bitmap Heap Scan on post_1  (cost=315.68..105654.80 rows=32443 width=106) (actual   time=76.963..17281.184 rows=31925 loops=1)
   Recheck Cond: (search_vector @@ to_tsquery('quantum'::text))
   Heap Blocks: exact=29259
   ->  Bitmap Index Scan on index1_idx  (cost=0.00..307.57 rows=32443 width=0) (actual time=60.208..60.209 rows=31925 loops=1)
     Index Cond: (search_vector @@ to_tsquery('quantum'::text))
 Planning Time: 47.648 ms
 Execution Time: 17308.511 ms
(7 rows)

I thought at some point that changing work_mem would help.我曾一度认为更改 work_mem 会有所帮助。 I have set it up to 86MB and it is still the same.我已经将它设置为 86MB,它仍然是一样的。

The weird thing is that if I re-run the same command right after, it is much faster.奇怪的是,如果我在之后重新运行相同的命令,速度会快得多。 See below:见下文:

sitedb=# EXPLAIN ANALYZE SELECT id, title FROM post_1 WHERE search_vector @@ to_tsquery('quantum');
                                                      QUERY PLAN                                                           
-------------------------------------------------------------------------------------------------------------------------------
 Bitmap Heap Scan on post_1  (cost=315.68..105654.80 rows=32443 width=106) (actual time=44.542..495.525 rows=31925 loops=1)
   Recheck Cond: (search_vector @@ to_tsquery('quantum'::text))
   Heap Blocks: exact=29259
   ->  Bitmap Index Scan on index1_idx  (cost=0.00..307.57 rows=32443 width=0) (actual time=29.256..29.256 rows=31925 loops=1)
     Index Cond: (search_vector @@ to_tsquery('quantum'::text))
 Planning Time: 0.597 ms
 Execution Time: 502.296 ms
(7 rows)

Would anyone have an idea?有人会有想法吗?

Thank you very much.非常感谢你。

It is probably a cold cache.它可能是一个冷缓存。 Eg it had to read 29,259 pages and few of them were already in memory. The 2nd time you run it, they are in memory, so it is faster.例如,它必须读取 29,259 页,其中很少一部分已经在 memory 中。第二次运行时,它们在 memory 中,因此速度更快。 You could help confirm this by doing EXPLAIN (ANALYZE, BUFFERS) after turning track_io_timing on.您可以通过在打开 track_io_timing 后执行EXPLAIN (ANALYZE, BUFFERS)来帮助确认这一点。

You can increase effective_io_concurrency so that PostgreSQL will have multiple IO requests outstanding at once.您可以增加 effective_io_concurrency,这样 PostgreSQL 将同时处理多个 IO 请求。 How effective this is will depend on your IO hardware.这有多有效取决于您的 IO 硬件。 It should be more effective on striped RAID or JBOD, for example.例如,它在条带化 RAID 或 JBOD 上应该更有效。

If your cache was cold because you recently restarted, well, don't restart very often, or use pg_prewarm to warm up the cache when you do.如果你的缓存因为你最近重新启动而变冷,那么不要经常重启,或者在你这样做时使用 pg_prewarm 来预热缓存。 If it can't stay in cache because your frequently-used data is too big for it to stay in memory, then get more RAM, or get faster disks (like SSD, of if they already are SSD then get better ones).如果它不能保留在缓存中,因为您经常使用的数据太大而无法保留在 memory 中,则获得更多 RAM,或获得更快的磁盘(如 SSD,如果它们已经是 SSD,则获得更好的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM