shlogg · Early preview
Augusts Bautra @augustsbautra

Optimizing UNION Queries: From 30s To Under 1s Report Generation Time

Debugging performance issues in reports led to a breakthrough: replacing WHERE field IN with WHERE EXISTS (SELECT 1 ...) improved query time from 30s to under 1s!

Today I was debugging performance issues in one of our reports, and I could not make heads nor tails of it for a long time.
I was pretty sure the problem was with one of the more complex scopes where UNION was being used, but having spent a couple hours trying several approaches, I ended up pasting the scope in GPT and asking for tips.
This is what I stared out with:

scope :special_records, ->(project_id:) do
  project_config_entries = CompanyProjects::TeklaReports::TotalsConfigEntry.where(project_id: project_id)
  specials_from_report_file = unscoped.where(special: true).select(:id)
  specia...