Skip to content
Advertisement

How can I replace this correlated subquery within a function call?

Given the following tables

buckets

points

And the following query

Output

How can I remove the correlated sub-query to improve the performance?

Currently ~280,000 points * ~650 buckets = ~180,000,000 loops = very slow!

Basically I want to remove the correlated sub-query and apply the width_bucket function only once per unique metric_id in buckets, so that the performance is improved and the function is still given the correct time series data.

How can this be done in Postgres 13?

Advertisement

Answer

You can use a cte to aggregate buckets first

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement