I have the following query:
SELECT DISTINCT
e.id,
folder,
subject,
in_reply_to,
message_id,
"references",
e.updated_at,
(
select count(*)
from emails
where
(
select "references"[1]
from emails
where message_id = e.message_id
) = ANY ("references")
or message_id =
(
select "references"[1]
from emails
where message_id = e.message_id
)
)
FROM "emails" e
INNER JOIN "email_participants"
ON ("email_participants"."email_id" = e."id")
WHERE (("user_id" = 220)
AND ("folder" = 'INBOX'))
ORDER BY e."updated_at" DESC
LIMIT 10 OFFSET 0;
Here is the explain analyze output of the above query.
The query peformed fine until I added the count subquery below:
(
select count(*)
from emails
where
(
select "references"[1]
from emails
where message_id = e.message_id
) = ANY ("references")
or message_id =
(
select "references"[1]
from emails
where message_id = e.message_id
)
)
In fact I have tried simpler subqueries and it seems to be the aggregate function itself that is taking the time.
Is then an alternative way that I could append the count subquery onto each result? Should I update the results after the initial query has run for example?
Here is a pastebin that will create the table and also run the badly performing query at the end to display what the output should be.