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 3d67aed08a
7 changed files with 12200 additions and 41793 deletions

View File

@ -18,17 +18,27 @@ 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,
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
),
@ -89,12 +99,11 @@ users_to_examine AS (
booking_form_counts AS (
-- Establish a summary of records with ages
WITH summary AS (
SELECT booking_forms.user_id, booking_forms.created_at::date - users.created_at::date AS created_age
SELECT booking_forms.user_id, booking_forms.created_at::date - users_to_examine.created_at::date AS created_age
FROM config, booking_forms
JOIN users ON booking_forms.user_id = users.id
JOIN users_to_examine ON booking_forms.user_id = users_to_examine.id
WHERE booking_forms.created_at >= config.date_cutoff
AND users.created_at >= config.date_cutoff
AND booking_forms.created_at <= users.created_at::date + config.long_term
AND booking_forms.created_at <= users_to_examine.created_at::date + config.long_term
),
-- Group by week for line charts
weekly_counts AS (
@ -127,14 +136,13 @@ booking_form_counts AS (
booking_counts AS (
-- Establish a summary of records with ages
WITH summary AS (
SELECT booking_forms.user_id, bookings.created_at::date - users.created_at::date AS created_age
SELECT booking_forms.user_id, bookings.created_at::date - users_to_examine.created_at::date AS created_age
FROM config,
booking_forms
JOIN users ON booking_forms.user_id = users.id
JOIN users_to_examine ON booking_forms.user_id = users_to_examine.id
JOIN bookings ON bookings.booking_form_id = booking_forms.id
WHERE booking_forms.created_at >= config.date_cutoff
AND bookings.created_at >= config.date_cutoff
AND users.created_at >= config.date_cutoff
),
-- Group by week for line charts
weekly_counts AS (
@ -169,12 +177,11 @@ booking_counts AS (
employee_counts AS (
-- Establish a summary of records with ages
WITH summary AS (
SELECT employees.user_id, employees.created_at::date - users.created_at::date AS created_age
SELECT employees.user_id, employees.created_at::date - users_to_examine.created_at::date AS created_age
FROM config, employees
JOIN users ON employees.user_id = users.id
JOIN users_to_examine ON employees.user_id = users_to_examine.id
WHERE employees.created_at >= config.date_cutoff
AND users.created_at >= config.date_cutoff
AND employees.created_at <= users.created_at::date + config.long_term
AND employees.created_at <= users_to_examine.created_at::date + config.long_term
),
-- Group by week for line charts
weekly_counts AS (
@ -207,12 +214,11 @@ employee_counts AS (
contact_counts AS (
-- Establish a summary of records with ages
WITH summary AS (
SELECT customers.user_id, customers.created_at::date - users.created_at::date AS created_age
SELECT customers.user_id, customers.created_at::date - users_to_examine.created_at::date AS created_age
FROM config, customers
JOIN users ON customers.user_id = users.id
JOIN users_to_examine ON customers.user_id = users_to_examine.id
WHERE customers.created_at >= config.date_cutoff
AND users.created_at >= config.date_cutoff
AND customers.created_at <= users.created_at::date + config.long_term
AND customers.created_at <= users_to_examine.created_at::date + config.long_term
),
-- Group by week for line charts
weekly_counts AS (
@ -248,7 +254,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(booking_form_counts.short_term, 0) AS booking_forms_short_term,
@ -271,7 +276,7 @@ SELECT users_to_examine.id,
COALESCE(contact_counts.long_term, 0) AS contacts_long_term,
COALESCE(contact_counts.total, 0) AS contacts,
COALESCE(contact_counts.weekly_counts, '{}'::jsonb) AS contacts_weekly_counts
FROM users_to_examine
FROM config, users_to_examine
LEFT JOIN booking_form_counts ON booking_form_counts.user_id = users_to_examine.id
LEFT JOIN booking_counts ON booking_counts.user_id = users_to_examine.id
LEFT JOIN employee_counts ON employee_counts.user_id = users_to_examine.id