开发者

Given a query, how do you know which indices to add?

开发者 https://www.devze.com 2023-03-19 02:44 出处:网络
Is there some tool that does this? You give it a SQL query and it gives suggestions. For example, I\'m trying to optimize the following query:

Is there some tool that does this? You give it a SQL query and it gives suggestions. For example, I'm trying to optimize the following query:

Load (383.2ms)  SELECT COUNT(*) as plays, p.chosen_race as race, p.won as won, r.game_type as type FROM plays p, replays r WHERE p.replay_id = r.id AND r.is_public = 't' AND r.winner_known = 't' GROUP BY r.game_type, p.chosen_race, p.won ORDER BY r.game_type, p.chose开发者_JS百科n_race

EXPLAIN

 Aggregate  (cost=8389.92..8389.93 rows=1 width=4)
   ->  Hash Left Join  (cost=3570.69..8150.84 rows=95630 width=4)
         Hash Cond: (replays.id = players_replays_join.replay_id)
         ->  Hash Left Join  (cost=1785.34..3772.35 rows=38995 width=4)
               Hash Cond: (replays.id = plays.replay_id)
               ->  Seq Scan on replays  (cost=0.00..833.02 rows=15901 width=8)
                     Filter: (is_public AND ((state)::text = 'success'::text))
               ->  Hash  (cost=1110.82..1110.82 rows=41082 width=4)
                     ->  Seq Scan on plays  (cost=0.00..1110.82 rows=41082 width=4)
         ->  Hash  (cost=1110.82..1110.82 rows=41082 width=8)
               ->  Seq Scan on plays players_replays_join  (cost=0.00..1110.82 rows=41082 width=8)

How are we supposed to interpret that output?


You need data in tables.

Then you may use EXPLAIN ANALYZE

http://www.postgresql.org/docs/9.0/interactive/using-explain.html

You may add and drop indexes and compare times.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号