postgresql Postgres数据库在运行查询时非常慢

jgzswidk  于 2023-11-18  发布在  PostgreSQL
关注(0)|答案(1)|浏览(186)

我最近从一个单独托管的数据库迁移到DigitalOcean的托管数据库。这是一个Postgres 14数据库。我觉得我在迁移后注意到了一些减速,但我没有一个简单的方法来比较,所以我只是假设我错了。然而,我注意到它相当慢。
我有一个表“条目”与4,7米行.如果我运行一个select count(*) from entries查询,在19秒内完成.总数据库大小约为1.1GB.
我备份了我的整个数据库,并将其导入到本地数据库,并在本地完成896毫秒。
关于如何调试它有什么提示吗?我最初注意到它在一个比这个例子更复杂的查询中运行得很慢。但我认为这个路由太简单了,我不妨使用count 1作为示例。
我试着在条目表上运行ANALYZE,但这并没有真正起作用。

编辑:

这是我运行explain的查询:

本地:

Finalize Aggregate  (cost=65547.91..65547.92 rows=1 width=8)
  ->  Gather  (cost=65547.70..65547.91 rows=2 width=8)
        Workers Planned: 2
        ->  Partial Aggregate  (cost=64547.70..64547.71 rows=1 width=8)
              ->  Parallel Index Only Scan using index_entries_on_discarded_at on entries  (cost=0.43..59600.43 rows=1978909 width=0)

字符串

DigitalOcean:

Finalize Aggregate  (cost=340527.64..340527.65 rows=1 width=8)
  ->  Gather  (cost=340527.43..340527.64 rows=2 width=8)
        Workers Planned: 2
        ->  Partial Aggregate  (cost=339527.43..339527.44 rows=1 width=8)
              ->  Parallel Seq Scan on entries  (cost=0.00..334579.74 rows=1979074 width=0)
JIT:
  Functions: 5
  Options: Inlining false, Optimization false, Expressions true, Deforming true

Digital Ocean DB上的解释(分析、详细、缓冲区、设置)输出:

Finalize Aggregate  (cost=340527.64..340527.65 rows=1 width=8) (actual time=21958.167..22251.122 rows=1 loops=1)
  Output: count(*)
  Buffers: shared hit=19544 read=295245
  ->  Gather  (cost=340527.43..340527.64 rows=2 width=8) (actual time=21925.670..22239.158 rows=3 loops=1)
        Output: (PARTIAL count(*))
        Workers Planned: 2
        Workers Launched: 2
        Buffers: shared hit=19544 read=295245
        ->  Partial Aggregate  (cost=339527.43..339527.44 rows=1 width=8) (actual time=21667.850..21667.880 rows=1 loops=3)
              Output: PARTIAL count(*)
              Buffers: shared hit=19544 read=295245
              Worker 0:  actual time=21539.121..21539.141 rows=1 loops=1
                JIT:
                  Functions: 3
                  Options: Inlining false, Optimization false, Expressions true, Deforming true
                  Timing: Generation 0.375 ms, Inlining 0.000 ms, Optimization 0.351 ms, Emission 74.197 ms, Total 74.923 ms
                Buffers: shared hit=6507 read=102324
              Worker 1:  actual time=21546.859..21546.874 rows=1 loops=1
                JIT:
                  Functions: 3
                  Options: Inlining false, Optimization false, Expressions true, Deforming true
                  Timing: Generation 0.351 ms, Inlining 0.000 ms, Optimization 0.289 ms, Emission 82.109 ms, Total 82.749 ms
                Buffers: shared hit=6352 read=91199
              ->  Parallel Seq Scan on public.entries  (cost=0.00..334579.74 rows=1979074 width=0) (actual time=153.347..21059.836 rows=1581283 loops=3)
                    Buffers: shared hit=19544 read=295245
                    Worker 0:  actual time=40.834..20943.050 rows=1638108 loops=1
                      Buffers: shared hit=6507 read=102324
                    Worker 1:  actual time=44.950..20923.007 rows=1471456 loops=1
                      Buffers: shared hit=6352 read=91199
"Settings: effective_cache_size = '568MB', effective_io_concurrency = '2', random_page_cost = '1', work_mem = '1751kB'"
Query Identifier: 1230802159253045228
Planning Time: 0.641 ms
JIT:
  Functions: 11
  Options: Inlining false, Optimization false, Expressions true, Deforming true
  Timing: Generation 26.149 ms, Inlining 0.000 ms, Optimization 76.294 ms, Emission 515.557 ms, Total 618.000 ms
Execution Time: 22290.983 ms

nvbavucw

nvbavucw1#

DigitalOcean机器从磁盘读取800MB需要21秒(有三个并行进程)。我认为这足以解释差异。

相关问题