给定一个查询,您如何知道要添加哪些索引?
有一些工具可以做到这一点吗?你给它一个 SQL 查询,它会给出建议。例如,我正在尝试优化以下查询:
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.chosen_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)
我们应该如何解释该输出?
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.chosen_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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要表中的数据。
然后你可以使用 EXPLAIN ANALYZE
http://www.postgresql.org/docs /9.0/interactive/using-explain.html
您可以添加和删除索引并比较时间。
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.