Skip to content

perf: add indexes for top slow queries#3641

Merged
rebelchris merged 4 commits intomainfrom
perf/add-query-performance-indexes
Feb 27, 2026
Merged

perf: add indexes for top slow queries#3641
rebelchris merged 4 commits intomainfrom
perf/add-query-performance-indexes

Conversation

@rebelchris
Copy link
Contributor

Summary

  • Adds 5 database indexes targeting the top slow queries identified via pg_stat_statements, prioritized by total execution time
  • All indexes use CREATE INDEX CONCURRENTLY to avoid table locks in production

Indexes

Index Table Type Target Query Impact
IDX_user_marketing_cta_userId_readAt_null user_marketing_cta Partial B-tree (userId) WHERE readAt IS NULL Marketing CTA boot lookup 19.4M ms total (1M calls, 17.9ms mean)
IDX_view_userId_postId_timestamp_desc view B-tree (userId, postId, timestamp DESC) View dedup check before insert 1.54M ms total (36K calls)
IDX_post_relation_relatedPostId_createdAt post_relation B-tree (relatedPostId, createdAt) PostRelation OR clause (existing index only covers postId) 10.8K ms total (839 calls)
IDX_source_gin_name source GIN trigram name Source ILIKE search Aligns with existing user GIN indexes
IDX_source_gin_handle source GIN trigram handle Source ILIKE search Aligns with existing user GIN indexes

Test plan

  • Verify migration applies cleanly on staging
  • Confirm pg_stat_statements shows improved mean/total times for targeted queries after deployment
  • Validate EXPLAIN ANALYZE uses the new indexes for the marketing CTA and view insert paths

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@pulumi
Copy link

pulumi bot commented Feb 26, 2026

🍹 The Update (preview) for dailydotdev/api/prod (at ac7433a) was successful.

✨ Neo Explanation

This is a standard application deployment rolling out a new version across all 7 services, 32 cron jobs, and triggering fresh DB and ClickHouse schema migration jobs in the production Kubernetes cluster.

Root Cause Analysis

A new version of the application has been built and is being deployed to production. All resources are being updated to roll out the new container image across every service and scheduled job in the cluster.

Dependency Chain

The new application version cascades uniformly across the entire stack:

  • 7 Deployments (main API, background workers, WebSocket, private, Temporal, personalized digest, worker jobs) are all being updated to pull the new container image.
  • 32 CronJobs are being updated in-place to reference the new image version.
  • 2 one-time migration Jobs (database and ClickHouse) are being replaced — the old jobs tied to the previous version are deleted, and fresh migration jobs for the new version are created. This is the standard pattern for running schema migrations on each deploy.

Risk Analysis

No stateful resources (databases, storage buckets, persistent volumes) are being modified or deleted. The migration jobs (DB and ClickHouse) involve running schema migrations against live databases, which carries inherent risk if the migrations are destructive or non-backwards-compatible — but this is a standard part of the deployment pipeline. Kubernetes Deployments will perform rolling updates, so there is no expected downtime for the running services.

Resource Changes

    Name                                                       Type                           Operation
~   vpc-native-update-current-streak-cron                      kubernetes:batch/v1:CronJob    update
~   vpc-native-update-views-cron                               kubernetes:batch/v1:CronJob    update
~   vpc-native-user-profile-analytics-clickhouse-cron          kubernetes:batch/v1:CronJob    update
~   vpc-native-calculate-top-readers-cron                      kubernetes:batch/v1:CronJob    update
~   vpc-native-user-posts-analytics-refresh-cron               kubernetes:batch/v1:CronJob    update
+   vpc-native-api-db-migration-f07346c8                       kubernetes:batch/v1:Job        create
~   vpc-native-update-source-tag-view-cron                     kubernetes:batch/v1:CronJob    update
~   vpc-native-clean-zombie-user-companies-cron                kubernetes:batch/v1:CronJob    update
~   vpc-native-clean-zombie-users-cron                         kubernetes:batch/v1:CronJob    update
~   vpc-native-clean-gifted-plus-cron                          kubernetes:batch/v1:CronJob    update
~   vpc-native-post-analytics-clickhouse-cron                  kubernetes:batch/v1:CronJob    update
~   vpc-native-update-tags-str-cron                            kubernetes:batch/v1:CronJob    update
~   vpc-native-expire-super-agent-trial-cron                   kubernetes:batch/v1:CronJob    update
~   vpc-native-update-achievement-rarity-cron                  kubernetes:batch/v1:CronJob    update
~   vpc-native-post-analytics-history-day-clickhouse-cron      kubernetes:batch/v1:CronJob    update
~   vpc-native-hourly-notification-cron                        kubernetes:batch/v1:CronJob    update
~   vpc-native-validate-active-users-cron                      kubernetes:batch/v1:CronJob    update
~   vpc-native-daily-digest-cron                               kubernetes:batch/v1:CronJob    update
~   vpc-native-update-highlighted-views-cron                   kubernetes:batch/v1:CronJob    update
~   vpc-native-deployment                                      kubernetes:apps/v1:Deployment  update
~   vpc-native-update-trending-cron                            kubernetes:batch/v1:CronJob    update
~   vpc-native-user-profile-analytics-history-clickhouse-cron  kubernetes:batch/v1:CronJob    update
~   vpc-native-worker-job-deployment                           kubernetes:apps/v1:Deployment  update
~   vpc-native-personalized-digest-deployment                  kubernetes:apps/v1:Deployment  update
~   vpc-native-bg-deployment                                   kubernetes:apps/v1:Deployment  update
~   vpc-native-personalized-digest-cron                        kubernetes:batch/v1:CronJob    update
~   vpc-native-generate-search-invites-cron                    kubernetes:batch/v1:CronJob    update
~   vpc-native-generic-referral-reminder-cron                  kubernetes:batch/v1:CronJob    update
~   vpc-native-update-source-public-threshold-cron             kubernetes:batch/v1:CronJob    update
~   vpc-native-ws-deployment                                   kubernetes:apps/v1:Deployment  update
+   vpc-native-api-clickhouse-migration-f07346c8               kubernetes:batch/v1:Job        create
~   vpc-native-user-profile-updated-sync-cron                  kubernetes:batch/v1:CronJob    update
~   vpc-native-clean-stale-user-transactions-cron              kubernetes:batch/v1:CronJob    update
~   vpc-native-clean-zombie-opportunities-cron                 kubernetes:batch/v1:CronJob    update
~   vpc-native-update-tag-recommendations-cron                 kubernetes:batch/v1:CronJob    update
~   vpc-native-clean-zombie-images-cron                        kubernetes:batch/v1:CronJob    update
-   vpc-native-api-db-migration-39593f76                       kubernetes:batch/v1:Job        delete
~   vpc-native-temporal-deployment                             kubernetes:apps/v1:Deployment  update
~   vpc-native-private-deployment                              kubernetes:apps/v1:Deployment  update
~   vpc-native-check-analytics-report-cron                     kubernetes:batch/v1:CronJob    update
~   vpc-native-squad-posts-analytics-refresh-cron              kubernetes:batch/v1:CronJob    update
... and 4 other changes

Cannot use CONCURRENTLY inside a transaction (TypeORM migrations run
in a transaction).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@rebelchris rebelchris requested a review from capJavert February 26, 2026 18:47
@rebelchris rebelchris merged commit 6ca2e66 into main Feb 27, 2026
9 checks passed
@rebelchris rebelchris deleted the perf/add-query-performance-indexes branch February 27, 2026 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant