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:
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");
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"
)
)
)
)