【问题标题】:Postgres DB: SQL statement in production is taking much longer than locallyPostgres DB:生产中的 SQL 语句花费的时间比本地长得多
【发布时间】:2019-04-14 18:38:07
【问题描述】:

我在 Heroku 上运行 Postgres 11.2,在本地运行 Postgres 11.2。

我有一个特定查询在生产中超时。

这是一个查询示例:

SELECT u0.id, u0.app, u0.email, u0.gender, u0.fcm_token,
u0.flagged_user_ids, u0.flags, u0.gems_balance, u0.hearts_balance,
u0.hearts_received, u0.last_hearts_refreshed, u0.looking, u0.matches,
u0.meta_tags, u0.name, u0.online, u0.profile_image, u0.purchase_count,
u0.recents_history, u0.inserted_at, u0.updated_at
FROM users AS u0 WHERE ((u0.email != 'ready234@yahoo.com') AND (u0.looking = TRUE))
AND (NOT (u0.email IN ('charlie_pevijom_angel@tfbnw.net','indzdugsyh_1487230198@tfbnw.net','iznngyqvfh_1484692514@tfbnw.net','android@test.com','ios@test.com','ios@banana.com','android@banana.com','justinb@banana.com','herb.perv@banana.com')))
AND (NOT ('apple_review' = ANY(u0.meta_tags)))
AND (NOT ('ready234@yahoo.com' = ANY(u0.matches)))
AND (NOT (584065 = ANY(u0.flagged_user_ids)))
ORDER BY u0.updated_at LIMIT 1;

当我在本地下载生产数据库时,pg_dump,进入 SQL 控制台,然后在本地运行查询,速度非常快。

生产: Time: 46307.569 ms (00:46.308) 46 秒!

本地: Time: 176.106 ms

我已验证该表的本地副本与生产中的相同。

生产:

Indexes:
    "users_pkey" PRIMARY KEY, btree (id)
    "users_app_index" btree (app)
    "users_email_index" btree (email)
    "users_matches_index" btree (matches)
Referenced by:
    TABLE "chat_users" CONSTRAINT "chat_users_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id)
    TABLE "messages" CONSTRAINT "messages_from_id_fkey" FOREIGN KEY (from_id) REFERENCES users(id)

本地:

Indexes:
    "users_pkey" PRIMARY KEY, btree (id)
    "users_app_index" btree (app)
    "users_email_index" btree (email)
    "users_matches_index" btree (matches)
Referenced by:
    TABLE "chat_users" CONSTRAINT "chat_users_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id)
    TABLE "messages" CONSTRAINT "messages_from_id_fkey" FOREIGN KEY (from_id) REFERENCES users(id)

我不明白为什么生产中的那个要花这么长时间。有没有办法调试这个?

更新:

根据要求,以下是两者的执行计划:

生产:

QUERY PLAN                                                                                                                                                                                         
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=443802.88..443802.88 rows=1 width=834) (actual time=40956.176..40956.176 rows=0 loops=1)
   Buffers: shared hit=5074 read=431010 dirtied=20
   I/O Timings: read=39940.500
   ->  Sort  (cost=443802.88..443802.88 rows=1 width=834) (actual time=40956.174..40956.175 rows=0 loops=1)
         Sort Key: updated_at
         Sort Method: quicksort  Memory: 25kB
         Buffers: shared hit=5074 read=431010 dirtied=20
         I/O Timings: read=39940.500
         ->  Seq Scan on users u0  (cost=0.00..443802.88 rows=1 width=834) (actual time=40956.166..40956.166 rows=0 loops=1)
               Filter: (looking AND ((email)::text <> 'ready234@yahoo.com'::text) AND ((email)::text <> ALL ('{charlie_pevijom_angel@tfbnw.net,indzdugsyh_1487230198@tfbnw.net,iznngyqvfh_1484692514@tfbnw.net,android@test.com,ios@test.com,ios@banana.com,android@banana.com,justinb@banana.com,herb.perv@banana.com}'::text[])) AND ('apple_review'::text <> ALL ((meta_tags)::text[])) AND ('ready234@yahoo.com'::text <> ALL ((matches)::text[])) AND (584065 <> ALL (flagged_user_ids)))
               Rows Removed by Filter: 583206
               Buffers: shared hit=5074 read=431010 dirtied=20
               I/O Timings: read=39940.500
 Planning Time: 0.259 ms
 Execution Time: 40956.221 ms
(15 rows)

本地:

QUERY PLAN                                                                                                                                                                                   
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=78520.41..78520.41 rows=1 width=827) (actual time=164.434..164.434 rows=0 loops=1)
   Buffers: shared hit=11616 read=51044
   ->  Sort  (cost=78520.41..78520.41 rows=1 width=827) (actual time=164.433..164.433 rows=0 loops=1)
         Sort Key: updated_at
         Sort Method: quicksort  Memory: 25kB
         Buffers: shared hit=11616 read=51044
         ->  Gather  (cost=1000.00..78520.40 rows=1 width=827) (actual time=164.414..167.228 rows=0 loops=1)
               Workers Planned: 2
               Workers Launched: 2
               Buffers: shared hit=11616 read=51044
               ->  Parallel Seq Scan on users u0  (cost=0.00..77520.30 rows=1 width=827) (actual time=159.986..159.986 rows=0 loops=3)
                     Filter: (looking AND ((email)::text <> 'ready234@yahoo.com'::text) AND ((email)::text <> ALL ('{charlie_pevijom_angel@tfbnw.net,indzdugsyh_1487230198@tfbnw.net,iznngyqvfh_1484692514@tfbnw.net,android@test.com,ios@test.com,ios@banana.com,android@banana.com,justinb@banana.com,herb.perv@banana.com}'::text[])) AND ('apple_review'::text <> ALL ((meta_tags)::text[])) AND ('ready234@yahoo.com'::text <> ALL ((matches)::text[])) AND (584065 <> ALL (flagged_user_ids)))
                     Rows Removed by Filter: 194158
                     Buffers: shared hit=11616 read=51044
 Planning Time: 0.376 ms
 Execution Time: 167.300 ms
(16 rows)

ext ALL ((matches)::text[])) AND (584065 ALL (flagged_user_ids))) 过滤器删除的行:194158 规划时间:0.389 ms 执行时间:167.745 毫秒 (12 行)

更新 2

生产:

 SELECT reltuples, relpages
[more] - > FROM pg_class
[more] - > WHERE relname = 'users';
 reltuples | relpages
-----------+----------
    582557 |   436084
(1 row)

Time: 27.967 ms

本地:

 SELECT reltuples, relpages
[more] - > FROM pg_class
[more] - > WHERE relname = 'users';
 reltuples | relpages
-----------+----------
    582281 |    62660
(1 row)

Time: 1.816 ms

更新 3:

看了执行计划,四处窥探,我想可能是因为Parallel Seq Scan。但是,我尝试了SET max_parallel_workers_per_gather TO 4;,即使使用Parallel Seq Scan,它似乎仍然很慢。看起来 I/O 需要很长时间:

生产:

Limit  (cost=439013.82..439013.82 rows=1 width=834) (actual time=36739.071..36739.071 rows=0 loops=1)
   Buffers: shared hit=5176 read=430908
   I/O Timings: read=182157.859
   ->  Sort  (cost=439013.82..439013.82 rows=1 width=834) (actual time=36739.069..36739.069 rows=0 loops=1)
         Sort Key: updated_at
         Sort Method: quicksort  Memory: 25kB
         Buffers: shared hit=5176 read=430908
         I/O Timings: read=182157.859
         ->  Gather  (cost=1000.00..439013.82 rows=1 width=834) (actual time=36739.061..36744.559 rows=0 loops=1)
               Workers Planned: 4
               Workers Launched: 4
               Buffers: shared hit=5176 read=430908
               I/O Timings: read=182157.859
               ->  Parallel Seq Scan on users u0  (cost=0.00..438013.72 rows=1 width=834) (actual time=36727.506..36727.506 rows=0 loops=5)
                     Filter: (looking AND ((email)::text <> 'ready234@yahoo.com'::text) AND ((email)::text <> ALL ('{charlie_pevijom_angel@tfbnw.net,indzdugsyh_1487230198@tfbnw.net,iznngyqvfh_1484692514@tfbnw.net,android@test.com,ios@test.com,ios@banana.com,android@banana.com,justinb@banana.com,herb.perv@banana.com}'::text[])) AND ('apple_review'::text <> ALL ((meta_tags)::text[])) AND ('ready234@yahoo.com'::text <> ALL ((matches)::text[])) AND (584065 <> ALL (flagged_user_ids)))
                     Rows Removed by Filter: 116645
                     Buffers: shared hit=5176 read=430908
                     I/O Timings: read=182157.859
 Planning Time: 0.265 ms
 Execution Time: 36744.622 ms
(20 rows)

【问题讨论】:

  • 你需要看一下两个环境下的执行计划。
  • 嗨@GordonLinoff。你介意解释一下我是怎么做到的吗?
  • Nvm google了一下,我贴一下结果
  • 可能是陈旧的统计数据?尝试运行ANALYZE。
  • 好的,现在更新

标签: sql postgresql


【解决方案1】:

一些观察(不是你的主要问题):

  • 慢速服务器在表中的行数是原来的 2.5 倍,但这并不能说明 250 倍的减速。

  • 在慢速机器上,没有计划并行工作器,即使表更大。可能是您在慢速服务器上运行的 PostgreSQL 版本早于 9.6 吗?如果没有,也许你禁用了并行性(max_parallel_workers_per_gather 或 max_parallel_workers 设置为 0,或者其他一些相关的配置参数已更改)。但这也不能解释 250 倍的减速。

你真正的问题是:

  • 顺序扫描必须从慢速机器上的辅助存储中读取大部分块。 EXPLAIN (ANALYZE, BUFFERS) 表明几乎所有块都已读取,并且由于您启用了 track_io_timing,我们知道查询花费的 41 秒中有 40 秒用于执行 I/O。

以下两点加剧了这种情况:

  • 慢速系统上的表非常臃肿(表的 6/7 似乎是空的,因为在一个 7 倍大的表中大约有同样多的元组)。因此,查询必须读取 7 倍于所需的块。

    下次您可以考虑运行VACUUM (FULL) users 并减少autovacuum_vacuum_cost_delay 的设置。

  • 可能是慢速服务器上的慢速 I/O 子系统导致了问题。

您的WHERE 条件都是以无法使用索引的方式编写的。这确实是一个需要顺序扫描的查询。

您可以将内存用于问题(至少 4GB)并使用pg_prewarm 将表加载到缓存中。或者,您可以获得非常快速的 SSD 存储。我不认为你可以做更多的事情。

【讨论】:

  • 嗨劳伦兹。我已经粘贴了您请求的 reltuples / relpages 查询的结果。我会用 BUFFERS 更新解释
  • 是的,桌子很臃肿(其中 6/7 是空气)。这将造成部分的放缓。
  • 刚刚上传了带有缓冲区的解释。桌子臃肿是什么意思?除了 ID 字段之外,这些行是空的吗?
  • 是否有一个设置可以检查为什么生产数据库没有在本地数据库进行并行顺序扫描?
  • 我已经修改了答案,并使用您添加到问题中的信息使其更加明确。不要担心缺乏并行性,这不是你的问题。
猜你喜欢
  • 1970-01-01
  • 2020-09-07
  • 2018-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多