0

我在本地(OS X)和 Heroku 上执行不同的查询,在同一个数据库上,相同的 PG 版本 9.4.5。

我运行 ANALYZE 来更新 Heroku 和本地(最近的转储)上所有两个相关表的统计信息。

询问:

SELECT "likes"."post_id" AS post_id
FROM likes
INNER JOIN "follows"  on "follows"."followee_id"  = "likes"."user_id"
WHERE "follows"."follower_id" = 1416077
ORDER BY "likes"."id" DESC ;

喜欢的索引:

"likes_pkey" PRIMARY KEY, btree (id)
"likes_post_id_user_id_key" UNIQUE CONSTRAINT, btree (user_id, post_id)
"index_likes_on_created_at" btree (created_at)
"index_likes_on_post_id" btree (post_id)

索引如下:

"follows_pkey" PRIMARY KEY, btree (id)
"follows_follower_id_followee_id_key" UNIQUE CONSTRAINT, btree (follower_id, followee_id)
"index_follows_on_followee_id" btree (followee_id)

enable_* pg_settings 中的设置是相同的。 work_mem是一样的

seq_page_cost = 1在两台机器上

random_page_cost= 4 本地和 2 在 Heroku 上,但在 heroku 上将其设置为 4 不会改变任何东西

explain(analyze, verbose)本地

Sort  (cost=2611609.82..2631198.92 rows=7835638 width=8) (actual time=177.588..190.331 rows=96766 loops=1)
  Output: likes.post_id, likes.id
  Sort Key: likes.id
  Sort Method: external merge  Disk: 1704kB
  ->  Nested Loop  (cost=1.00..1500109.83 rows=7835638 width=8) (actual time=0.050..129.308 rows=96766 loops=1)
        Output: likes.post_id, likes.id
        ->  Index Only Scan using follows_follower_id_followee_id_key on public.follows  (cost=0.44..1253.60 rows=435 width=4) (actual time=0.029..0.074 rows=16 loops=1)
              Output: follows.follower_id, follows.followee_id
              Index Cond: (follows.follower_id = 1416077)
              Heap Fetches: 16
        ->  Index Scan using likes_post_id_user_id_key on public.likes  (cost=0.56..3405.71 rows=3994 width=12) (actual time=0.011..7.258 rows=6048 loops=16)
              Output: likes.id, likes.user_id, likes.post_id, likes.created_at
              Index Cond: (likes.user_id = follows.followee_id)
Planning time: 0.500 ms
Execution time: 200.648 ms

explain(analyze, verbose)在 Heroku 上:

Sort  (cost=1532085.93..1536475.21 rows=8778573 width=8) (actual time=52403.641..52413.461 rows=96763 loops=1)
  Output: likes.post_id, likes.id
  Sort Key: likes.id
  Sort Method: quicksort  Memory: 7608kB
  ->  Hash Join  (cost=926122.23..1243873.27 rows=8778573 width=8) (actual time=40392.996..52357.989 rows=96763 loops=1)
        Output: likes.post_id, likes.id
        Hash Cond: (follows.followee_id = likes.user_id)
        ->  Index Only Scan using follows_follower_id_followee_id_key on public.follows  (cost=0.09..9.69 rows=490 width=4) (actual time=0.048..0.086 rows=16 loops=1)
              Output: follows.followee_id
              Index Cond: (follows.follower_id = 1416077)
              Heap Fetches: 0
        ->  Hash  (cost=516037.68..516037.68 rows=48919560 width=12) (actual time=40392.575..40392.575 rows=48932091 loops=1)
              Output: likes.post_id, likes.id, likes.user_id
              Buckets: 65536  Batches: 256 (originally 128)  Memory Usage: 30721kB
              ->  Seq Scan on public.likes  (cost=0.00..516037.68 rows=48919560 width=12) (actual time=0.013..18437.681 rows=48932091 loops=1)
                    Output: likes.post_id, likes.id, likes.user_id
Planning time: 0.713 ms
Execution time: 52434.805 ms

为什么这些执行计划不同?我应该对 Heroku 进行什么调整以使其像在本地工作一样工作?

4

0 回答 0