optimize stats using "materialized view"
This commit is contained in:
30
sqlpage/migrations/001_schema.sql
Normal file
30
sqlpage/migrations/001_schema.sql
Normal file
@@ -0,0 +1,30 @@
|
||||
CREATE TABLE if not exists instances (
|
||||
-- PK will be rowid
|
||||
domain TEXT UNIQUE
|
||||
, name TEXT
|
||||
, slogan TEXT
|
||||
, description TEXT
|
||||
, languages TEXT
|
||||
, open INT -- BOOLEAN
|
||||
, version TEXT
|
||||
, location TEXT -- from IP
|
||||
, failure INTEGER
|
||||
, createdAt INTEGER -- second since epoch
|
||||
, updatedAt INTEGER -- second since epoch
|
||||
) strict;
|
||||
|
||||
create table if not exists stats (
|
||||
insertedAt INTEGER
|
||||
, instance_id INTEGER -- rowid of instance
|
||||
, users INTEGER
|
||||
, local_groups INTEGER
|
||||
, total_groups INTEGER
|
||||
, local_events INTEGER
|
||||
, total_events INTEGER
|
||||
, local_comments INTEGER
|
||||
, total_comments INTEGER
|
||||
, following INTEGER
|
||||
, followers INTEGER
|
||||
-- PK, would have been (instance_id, date)
|
||||
-- to spare index, we declare no PK
|
||||
) strict;
|
2
sqlpage/migrations/002_index_stats.sql
Normal file
2
sqlpage/migrations/002_index_stats.sql
Normal file
@@ -0,0 +1,2 @@
|
||||
CREATE INDEX if not exists idx_stats_day ON stats(insertedAt / (3600*24));
|
||||
|
12
sqlpage/migrations/003_create_daystats.sql
Normal file
12
sqlpage/migrations/003_create_daystats.sql
Normal file
@@ -0,0 +1,12 @@
|
||||
DROP table if exists day_stats;
|
||||
|
||||
Create table day_stats as
|
||||
SELECT
|
||||
count(instance_id) as i
|
||||
, sum(users) as u
|
||||
, sum(local_events) as e
|
||||
, sum(local_groups) as g
|
||||
, insertedAt as x
|
||||
, insertedAt / (3600*24) as j
|
||||
FROM stats
|
||||
GROUP BY j
|
Reference in New Issue
Block a user