[Solved] Refactoring a slow performing query with a subquery in Postgres

How to optimize this SQL query?

In case you have your own slow SQL query, you can optimize it automatically here.

For the query above, the following recommendations will be helpful as part of the SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
  2. Replace In Subquery With Correlated Exists (modified query below): In many cases, an EXISTS subquery with a correlated condition will perform better than a non correlated IN subquery.
  3. Replace Join With Exists To Avoid Redundant Grouping (modified query below): When a joined table isn’t used anywhere other than in the WHERE clause, it's equivalent to an EXISTS subquery, which often performs better. In cases where the DISTINCT or GROUP BY clause contains only columns from the Primary key, they can be removed to further improve performance, as after this transformation, they are redundant.
Optimal indexes for this query:
CREATE INDEX ahoy_events_idx_visit_id ON "ahoy_events" ("visit_id");
CREATE INDEX participations_idx_event_id_is_preview ON "participations" ("event_id","is_preview");
CREATE INDEX visits_idx_event_id_participation ON "visits" ("event_id","participation_id");
The optimized query:
SELECT
        COUNT(*) 
    FROM
        "participations" 
    WHERE
        "participations"."event_id" = $1 
        AND "participations"."is_preview" = $2 
        AND (
            NOT EXISTS (
                SELECT
                    DISTINCT 1 
                FROM
                    "visits" 
                WHERE
                    (
                        (
                            "visits"."event_id" = $3 
                            AND "visits"."participation_id" IN (
                                SELECT
                                    "participations"."id" 
                                FROM
                                    "participations" 
                                WHERE
                                    "participations"."event_id" = $4 
                                    AND "participations"."is_preview" = $5
                            )
                        ) 
                        AND (
                            "participations"."id" = "visits"."participation_id"
                        )
                    ) 
                    AND (
                        EXISTS (
                            SELECT
                                1 
                            FROM
                                "ahoy_events" 
                            WHERE
                                "ahoy_events"."visit_id" = "visits"."id"
                        )
                    )
                )
            )

Related Articles



* original question posted on StackOverflow here.