小编dsj*_*erg的帖子

使用索引扫描,PostgreSQL查询运行得更快,但引擎选择散列连接

查询:

SELECT "replays_game".*
FROM "replays_game"
INNER JOIN
 "replays_playeringame" ON "replays_game"."id" = "replays_playeringame"."game_id"
WHERE "replays_playeringame"."player_id" = 50027
Run Code Online (Sandbox Code Playgroud)

如果我设置SET enable_seqscan = off,那么它做的很快,这是:

QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Nested Loop  (cost=0.00..27349.80 rows=3395 width=72) (actual time=28.726..65.056 rows=3398 loops=1)
   ->  Index Scan using replays_playeringame_player_id on replays_playeringame  (cost=0.00..8934.43 rows=3395 width=4) (actual time=0.019..2.412 rows=3398 loops=1)
         Index Cond: (player_id = 50027)
   ->  Index Scan using replays_game_pkey on replays_game  (cost=0.00..5.41 rows=1 width=72) (actual time=0.017..0.017 rows=1 loops=3398)
         Index Cond: (id = replays_playeringame.game_id)
 Total runtime: 65.437 ms
Run Code Online (Sandbox Code Playgroud)

但是如果没有可怕的enable_seqscan,它会选择做一个更慢的事情:

QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------------- …
Run Code Online (Sandbox Code Playgroud)

postgresql indexing query-optimization postgresql-performance

18
推荐指数
2
解决办法
9105
查看次数