There is a lot of rows in table "logs" (the Request A FROM).
Request A is quick and Request B is quick.
Put together, Request A WHERE IN Request B is very long. Is that normal?
Request A is very quick:
--request A
SELECT "rid", max("createdAt") as "createdAt"
FROM "logs"
WHERE "rid" IN (17,71,196,187,111,86,108,81,54,184,245,27,118,100,175,136,130,67,45)
GROUP BY "rid";
Request B is very quick:
--request B
SELECT "dr"."rid"
FROM (
SELECT *
FROM (
SELECT "eid", "tid", count(*) over (partition by "eid", "tid") count, id as "rid"
FROM rs
WHERE "deletedAt" is NULL
) "nr"
WHERE count > 1
) "dr"
INNER JOIN teams ON teams.id = "dr"."tid"
INNER JOIN projects ON projects.id = teams."pid"
ORDER BY "eid", "tid"
The result of B is the same list of numbers than in request A WHERE IN.
Replacing request A WHERE IN by request B, it becomes very slow.
Postgres: 9.5.6