Re: Optimize query for listing un-read messages

From: Andreas Joseph Krogh <andreas(at)visena(dot)com>
To: Brice André <brice(at)famille-andre(dot)be>
Cc: pgsql-sql <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Optimize query for listing un-read messages
Date: 2014-05-04 16:40:15
Message-ID: OfficeNetEmail.b6.9cee154efc4db41a.145c81a866d@prod2
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

På søndag 04. mai 2014 kl. 14:06:35, skrev Brice André <brice(at)famille-andre(dot)be
<mailto:brice(at)famille-andre(dot)be>>: Dear Andreas,
  For me, putting both "LEFT OUTER JOIN" and "NOT EXISTS" is a bad idea.
  As the "LEFT OUTER JOIN" will put fields of non-existing right table to
null, I would simply rewrite it :
SELECT ... FROM message m
    LEFT OUTER JOIN message_property prop ON prop.message_id = m.id
<http://m.id> AND prop.person_id = 1
WHERE prop.is_read = TRUE
  I would also ensure that an efficient index is used for the outer join. I
would probably try at least a multi-column index on (message_id, person_id) for
the property table. I would also maybe give a try to an index on (message_id,
person_id, is_read), just to see if it improves performances.   The problem is
that your suggested query doesn't return the desired results as it effectively
is an INNER JOIN because you have "WHERE prop.is_read=TRUE", defeating the
whole purpose of a LEFT OUTER JOIN.   -- Andreas Jospeh Krogh CTO / Partner -
Visena AS Mobile: +47 909 56 963 andreas(at)visena(dot)com <mailto:andreas(at)visena(dot)com>
www.visena.com <https://www.visena.com> <https://www.visena.com>  

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Brice André 2014-05-04 16:49:43 Re: Optimize query for listing un-read messages
Previous Message Brice André 2014-05-04 12:06:35 Re: Optimize query for listing un-read messages