给定一个查询,您如何知道要添加哪些索引?

发布于 2024-11-19 02:10:59 字数 1224 浏览 4 评论 0原文

有一些工具可以做到这一点吗?你给它一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何以心动 2024-11-26 02:10:59

您需要表中的数据。

然后你可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文