Change sim count materialized view into just view
Not so heavy that we need to store the table, it can just be computed on the fly.
This commit is contained in:
@@ -1,56 +0,0 @@
|
|||||||
-- NOTE(simkir): Materialized view called by the crosstab to pivot sim types to cols
|
|
||||||
create materialized view weekly_sim_submit_count_v2 as
|
|
||||||
with
|
|
||||||
-- NOTE(simkir): Select all groups and sim kinds
|
|
||||||
group_and_sims as (
|
|
||||||
select
|
|
||||||
"group",
|
|
||||||
sim_type
|
|
||||||
from
|
|
||||||
(
|
|
||||||
select substring(distinct_id similar '%#"@%#"' escape '#') as group
|
|
||||||
from session
|
|
||||||
where distinct_id is not null and distinct_id like '%@%'
|
|
||||||
group by substring(distinct_id similar '%#"@%#"' escape '#')
|
|
||||||
)
|
|
||||||
cross join
|
|
||||||
(
|
|
||||||
select distinct string_value as sim_type
|
|
||||||
from event_data
|
|
||||||
where event_data.data_key = 'kind'
|
|
||||||
)
|
|
||||||
),
|
|
||||||
events as (
|
|
||||||
select
|
|
||||||
substring(session.distinct_id similar '%#"@%#"' escape '#') as group,
|
|
||||||
event_data.string_value as sim_type,
|
|
||||||
event_data.website_event_id as event_id,
|
|
||||||
event_data.created_at
|
|
||||||
from
|
|
||||||
website_event
|
|
||||||
join session on session.session_id = website_event.session_id
|
|
||||||
left outer join event_data on event_data.website_event_id = website_event.event_id
|
|
||||||
where
|
|
||||||
website_event.website_id = '16e7d807-4db5-45fd-92a9-27393445a153'
|
|
||||||
and website_event.event_type = 2
|
|
||||||
and website_event.event_name = 'mapster-submit-drifters'
|
|
||||||
and event_data.data_key = 'kind'
|
|
||||||
)
|
|
||||||
|
|
||||||
select
|
|
||||||
group_and_sims.group,
|
|
||||||
group_and_sims.sim_type,
|
|
||||||
count(events.event_id)
|
|
||||||
from
|
|
||||||
group_and_sims
|
|
||||||
-- NOTE(simkir): Allow missing events
|
|
||||||
left outer join events on
|
|
||||||
-- NOTE(simkir): Join on the unique combinations of group name and sim
|
|
||||||
-- kind which we've already fetched in `group_and_sims`.
|
|
||||||
events.group = group_and_sims.group
|
|
||||||
and events.sim_type = group_and_sims.sim_type
|
|
||||||
-- NOTE(simkir): Filter here in the join to, again, allow missing rows
|
|
||||||
and events.created_at between current_timestamp - '7 days'::interval and current_timestamp
|
|
||||||
group by
|
|
||||||
group_and_sims.group,
|
|
||||||
group_and_sims.sim_type
|
|
||||||
@@ -1,20 +1,56 @@
|
|||||||
-- NOTE(simkir): Materialized view called by the crosstab to pivot sim types to cols
|
-- NOTE(simkir): Materialized view called by the crosstab to pivot sim types to cols
|
||||||
CREATE MATERIALIZED VIEW weekly_sim_submit_count AS
|
create view weekly_sim_submit_count as
|
||||||
SELECT
|
with
|
||||||
substring(session.distinct_id SIMILAR '%#"@%#"' ESCAPE '#') AS group,
|
-- NOTE(simkir): Select all groups and sim kinds
|
||||||
event_data.string_value as sim_type,
|
group_and_sims as (
|
||||||
COUNT(*)
|
select
|
||||||
FROM
|
"group",
|
||||||
website_event
|
sim_type
|
||||||
JOIN session ON session.session_id = website_event.session_id
|
from
|
||||||
LEFT OUTER JOIN event_data ON event_data.website_event_id = website_event.event_id
|
(
|
||||||
WHERE
|
select substring(distinct_id similar '%#"@%#"' escape '#') as group
|
||||||
website_event.website_id = '16e7d807-4db5-45fd-92a9-27393445a153'
|
from session
|
||||||
AND website_event.event_type = 2
|
where distinct_id is not null and distinct_id like '%@%'
|
||||||
AND website_event.event_name = 'mapster-submit-drifters'
|
group by substring(distinct_id similar '%#"@%#"' escape '#')
|
||||||
AND session.distinct_id IS NOT NULL
|
)
|
||||||
AND event_data.data_key = 'kind'
|
cross join
|
||||||
AND event_data.created_at BETWEEN CURRENT_TIMESTAMP - '7 days'::interval AND CURRENT_TIMESTAMP
|
(
|
||||||
GROUP BY
|
select distinct string_value as sim_type
|
||||||
substring(session.distinct_id SIMILAR '%#"@%#"' ESCAPE '#'),
|
from event_data
|
||||||
event_data.string_value;
|
where event_data.data_key = 'kind'
|
||||||
|
)
|
||||||
|
),
|
||||||
|
events as (
|
||||||
|
select
|
||||||
|
substring(session.distinct_id similar '%#"@%#"' escape '#') as group,
|
||||||
|
event_data.string_value as sim_type,
|
||||||
|
event_data.website_event_id as event_id,
|
||||||
|
event_data.created_at
|
||||||
|
from
|
||||||
|
website_event
|
||||||
|
join session on session.session_id = website_event.session_id
|
||||||
|
left outer join event_data on event_data.website_event_id = website_event.event_id
|
||||||
|
where
|
||||||
|
website_event.website_id = '16e7d807-4db5-45fd-92a9-27393445a153'
|
||||||
|
and website_event.event_type = 2
|
||||||
|
and website_event.event_name = 'mapster-submit-drifters'
|
||||||
|
and event_data.data_key = 'kind'
|
||||||
|
)
|
||||||
|
|
||||||
|
select
|
||||||
|
group_and_sims.group,
|
||||||
|
group_and_sims.sim_type,
|
||||||
|
count(events.event_id)
|
||||||
|
from
|
||||||
|
group_and_sims
|
||||||
|
-- NOTE(simkir): Allow missing events
|
||||||
|
left outer join events on
|
||||||
|
-- NOTE(simkir): Join on the unique combinations of group name and sim
|
||||||
|
-- kind which we've already fetched in `group_and_sims`.
|
||||||
|
events.group = group_and_sims.group
|
||||||
|
and events.sim_type = group_and_sims.sim_type
|
||||||
|
-- NOTE(simkir): Filter here in the join to, again, allow missing rows
|
||||||
|
and events.created_at between current_timestamp - '7 days'::interval and current_timestamp
|
||||||
|
group by
|
||||||
|
group_and_sims.group,
|
||||||
|
group_and_sims.sim_type
|
||||||
|
|||||||
Reference in New Issue
Block a user