fix: optimize sp-performance-query.helper.ts#320
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes the SQL query generation for storage provider performance materialized views by replacing an inefficient multi-table LEFT JOIN approach with staged Common Table Expressions (CTEs). The optimization targets migrations 1761500000000 and 1761500000001, which were causing startup instability due to expensive view creation (roughly ~100s per call).
Changes:
- Refactored query from cartesian product with
COUNT(DISTINCT)to staged aggregation using CTEs - Preserved all existing output columns and semantic behavior
- Eliminated the performance bottleneck caused by joining retrievals twice (r and r_ipfs)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Collaborator
Author
-- 2) Compare OLD baseline vs NEW snapshots captured in the last 10 minutes
WITH old_stats AS (
SELECT
count(*) AS samples,
avg(mean_exec_time) AS mean_ms,
avg(temp_blks_written) AS temp_blks_written_avg,
avg(shared_blks_read) AS shared_blks_read_avg
FROM public.perf_baseline_sp_perf
WHERE query ILIKE '%CREATE MATERIALIZED VIEW sp_performance_all_time AS%'
AND query ILIKE '%COUNT(DISTINCT d.id)%' -- old query signature
),
new_stats_10m AS (
SELECT
count(*) AS samples,
avg(mean_exec_time) AS mean_ms,
avg(temp_blks_written) AS temp_blks_written_avg,
avg(shared_blks_read) AS shared_blks_read_avg
FROM public.perf_baseline_sp_perf
WHERE captured_at >= now() - interval '10 minutes'
AND query ILIKE '%CREATE MATERIALIZED VIEW sp_performance_all_time AS%'
AND query ILIKE '%WITH deals_filtered AS%' -- new query signature
)
SELECT
round(o.mean_ms::numeric, 2) AS old_mean_ms,
round(n.mean_ms::numeric, 2) AS new_mean_ms_last_10m,
round((o.mean_ms / NULLIF(n.mean_ms, 0))::numeric, 2) AS speedup_x,
round((100 * (1 - (n.mean_ms / NULLIF(o.mean_ms, 0))))::numeric, 2) AS exec_time_reduction_pct,
round(o.temp_blks_written_avg::numeric, 2) AS old_temp_blks_written_avg,
round(n.temp_blks_written_avg::numeric, 2) AS new_temp_blks_written_avg_last_10m,
round(o.shared_blks_read_avg::numeric, 2) AS old_shared_blks_read_avg,
round(n.shared_blks_read_avg::numeric, 2) AS new_shared_blks_read_avg_last_10m,
o.samples AS old_samples,
n.samples AS new_samples_last_10m
FROM old_stats o
CROSS JOIN new_stats_10m n;
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR rewrites the SQL generated by
generateSpPerformanceQuery()inapps/backend/src/database/helpers/sp-performance-query.helper.tsto reduce migration-time cost when rebuilding SP performance materialized views.This targets startup instability where pending migrations (
1761500000000,1761500000001) repeatedly execute expensive view creation.Problem
pg_stat_statementsshowsCREATE MATERIALIZED VIEW sp_performance_all_time AS ...as a major hotspot (roughly ~100s per call, high temp block writes), which contributes to restart loops during migration retries.What Changed
COUNT(DISTINCT ...)+ multi-join query shape with staged aggregation:deals_filtered->deal_metricsretrievals_filtered->retrieval_metricsstorage_providersgenerateSpPerformanceQuery), so migration call sites remain unchanged.Expected Impact
Risk
Validation
pnpm typecheck && pnpm lint && pnpm check && pnpm format && pnpm build && pnpm testpnpm -C apps/backend test -- migrations.e2e-spec.tsPost-Deploy Verification
1761500000000and1761500000001are applied.pg_stat_statementsfor reduced runtime/temp I/O onCREATE MATERIALIZED VIEW sp_performance_all_time AS ....