]> git.xonotic.org Git - xonotic/xonstatdb.git/blob - scripts/refresh_recent_game_stats_mv.sql
Add a script to create/renew recent game stats for performance.
[xonotic/xonstatdb.git] / scripts / refresh_recent_game_stats_mv.sql
1 BEGIN;
2     -- Populate the table using a different name.
3     CREATE TABLE recent_game_stats_mv_new AS
4
5     SELECT 
6     pgs.player_game_stat_id, g.game_id, g.server_id, g.map_id, p.player_id, p.nick, pgs.score, pgs.alivetime
7
8     FROM player_game_stats pgs
9     INNER JOIN players p USING (player_id)
10     INNER JOIN games g USING (game_id)
11
12     WHERE
13     p.player_id > 2 
14     AND p.active_ind = true
15     AND pgs.create_dt BETWEEN (now() at time zone 'UTC' - interval '30 days') AND (now() at time zone 'UTC' + interval '1 day')
16     AND g.create_dt BETWEEN (now() at time zone 'UTC' - interval '30 days') AND (now() at time zone 'UTC' + interval '1 day');
17
18     -- Index it
19     CREATE INDEX recent_game_stats_mv_new_ix001 on recent_game_stats(server_id);
20     CREATE INDEX recent_game_stats_mv_new_ix002 on recent_game_stats(map_id);
21
22     -- Drop the old stuff, rename the stuff
23     DROP TABLE IF EXISTS recent_game_stats_mv CASCADE;
24     ALTER TABLE recent_game_stats_mv_new RENAME TO recent_game_stats_mv;
25
26     DROP INDEX IF EXISTS recent_game_stats_mv_ix001;
27     ALTER INDEX recent_game_stats_mv_new_ix001 RENAME to recent_game_stats_mv_ix001;
28
29     DROP INDEX IF EXISTS recent_game_stats_mv_ix002;
30     ALTER INDEX recent_game_stats_mv_new_ix002 RENAME to recent_game_stats_mv_ix002;
31
32 COMMIT;