Various query cleanups and optimizations based on chats with the team

This commit is contained in:
2026-02-04 22:24:53 -05:00
parent f9b57307de
commit 90c67a6797
7 changed files with 12892 additions and 17312 deletions

View File

@ -18,17 +18,28 @@ WITH config AS (
-- For features, we'll look at this as features used in the company's day-to-day, and not
-- evaluation attempts.
360 AS long_term,
-- We'll only look back a few years since we want to make fairish comparisons betwen users
-- We'll only look back a few years since we want to make fairish comparisons between users
-- exposed to "modern" ZenMaid tools.
CAST('2023-01-01' AS timestamp) AS date_cutoff
CAST('2023-01-01' AS timestamp) AS date_cutoff,
-- Not all users in our view will have had checklists available during the periods we're looking at,
-- so we want to flag each record as having them available or not.
CAST('2025-01-01' AS date) AS checklist_available_date
),
-- Then establish a standard core set of user data
users_with_churn_stats AS (
SELECT id, active, billing_state,
DATE_TRUNC('month', created_at) AS month_created,
created_at, free_trial_ends_at, updated_at,
updated_at::date - created_at::date AS lifespan
FROM users, config
churned_at, owner.max_owner_updated,
users.created_at::date + config.long_term AS content_cutoff,
GREATEST(owner.max_owner_updated, users.churned_at) AS last_update,
GREATEST(owner.max_owner_updated, users.churned_at)::date - created_at::date AS lifespan
FROM config, users
JOIN LATERAL (
SELECT user_id, max(updated_at) AS max_owner_updated
FROM employees
WHERE user_id = users.id
GROUP BY user_id
) owner ON users.id = owner.user_id
-- We'll only look at newish users.
WHERE created_at >= config.date_cutoff
),
@ -79,12 +90,10 @@ users_to_examine AS (
email_counts AS (
-- Establish a summary of records with ages
WITH summary AS (
SELECT sent_emails.user_id, sent_emails.created_at::date - users.created_at::date AS created_age
SELECT sent_emails.user_id, sent_emails.created_at::date - users_to_examine.created_at::date AS created_age
FROM config, sent_emails
JOIN users ON sent_emails.user_id = users.id
WHERE sent_emails.created_at >= config.date_cutoff
AND users.created_at >= config.date_cutoff
AND sent_emails.created_at <= users.created_at::date + config.long_term
JOIN users_to_examine ON sent_emails.user_id = users_to_examine.id
WHERE sent_emails.created_at <= users_to_examine.content_cutoff
),
-- Group by week for line charts
weekly_counts AS (
@ -120,7 +129,6 @@ SELECT users_to_examine.id,
users_to_examine.free_trial_ends_at,
users_to_examine.category,
users_to_examine.created_at,
users_to_examine.month_created,
users_to_examine.updated_at,
users_to_examine.lifespan,
COALESCE(email_counts.short_term, 0) AS emails_short_term,
@ -128,6 +136,6 @@ SELECT users_to_examine.id,
COALESCE(email_counts.long_term, 0) AS emails_long_term,
COALESCE(email_counts.total, 0) AS emails,
COALESCE(email_counts.weekly_counts, '{}'::jsonb) AS emails_weekly_counts
FROM users_to_examine
FROM config, users_to_examine
LEFT JOIN email_counts ON email_counts.user_id = users_to_examine.id
;