From: | Alban Hertroys <haramrae(at)gmail(dot)com> |
---|---|
To: | Andreas Joseph Krogh <andreas(at)visena(dot)com> |
Cc: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: Optimize query for listing un-read messages |
Date: | 2014-05-03 21:21:21 |
Message-ID: | 51091B6E-1EA2-4D11-A868-82FB6D570F07@gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On 03 May 2014, at 12:45, Andreas Joseph Krogh <andreas(at)visena(dot)com> wrote:
> Do you really need to query message_property twice? I would think this would give the same results:
>
> SELECT
> m.id AS message_id,
> prop.person_id,
> coalesce(prop.is_read, FALSE) AS is_read,
> m.subject
> FROM message m
> LEFT OUTER JOIN message_property prop ON prop.message_id = m.id AND prop.person_id = 1 AND prop.is_read = FALSE
> ;
Ah yes, of course that would match a bit too much. This however does give the same results:
SELECT
m.id AS message_id,
prop.person_id,
coalesce(prop.is_read, FALSE) AS is_read,
m.subject
FROM message m
LEFT OUTER JOIN message_property prop ON prop.message_id = m.id AND prop.person_id = 1
WHERE prop.is_read IS NULL OR prop.is_read = FALSE
;
That shaves off half the time of the query here, namely one indexscan.
The remaining time appears to be spent finding the rows in “message" that do not have a corresponding “message_property" for the given (message_id, person_id) tuple. It’s basically trying to find no needle in a haystack, you won’t know that there is no needle until you’ve searched the entire haystack.
It does seem to help a bit to create separate indexes on message_property.message_id and message_property.person_id; that reduces the sizes of the indexes that the database needs to match and merge other in order to find the missing message_id’s.
Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll find there is no forest.
From | Date | Subject | |
---|---|---|---|
Next Message | Andreas Joseph Krogh | 2014-05-03 21:29:21 | Re: Optimize query for listing un-read messages |
Previous Message | Andreas Heiduk | 2014-05-03 19:32:53 | Re: Manipulating jsonb |