RE: Slow query fixed by replacing equality with a nested query

From: <val(dot)janeiko(at)gmail(dot)com>
To: "'Michael Lewis'" <mlewis(at)entrata(dot)com>
Cc: <pgsql-performance(at)postgresql(dot)org>
Subject: RE: Slow query fixed by replacing equality with a nested query
Date: 2022-01-21 11:37:54
Message-ID: 003801d80ebb$54ac97a0$fe05c6e0$@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

My mistake. I have updated the query in the gist: cte1 should have been referenced in cte2.

The query plans are correct. It was just the query in the gist that was incorrect (I was just verifying cte1 was the culprit – without it the query is fast too).

This SQL query is a result of translating a FHIR query into SQL. These queries are generated on the fly from user input. The chains will not always be linear. But I guess I could write an optimizer that rewrites linear parts as JOINS. If that would result in better query plans.

I have done a few simple experiments in the past comparing CTEs like this to JOINS, but the resultant query plans were the same. CTEs seemed easier to follow when troubleshooting issues, so I left them as such. Do JOINs become better than CTEs at a certain point?

I will attempt to rewrite the query with JOINs on Monday to see if it makes a difference. It might be tricky, the relationship from resource table to search parameter tables is often a 1 to many.

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Valentin Janeiko 2022-01-24 13:22:41 Re: Slow query fixed by replacing equality with a nested query
Previous Message Michael Lewis 2022-01-21 03:33:17 Re: Slow query fixed by replacing equality with a nested query