agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
From: Andreas Joseph Krogh <andreas@visena.com>
To: Brice André <brice@famille-andre.be>
Cc: pgsql-sql <pgsql-sql@postgresql.org>
Subject: Re: Optimize query for listing un-read messages
Date: Sun, 4 May 2014 18:40:15 +0200 (CEST)
Message-ID: <OfficeNetEmail.b6.9cee154efc4db41a.145c81a866d@prod2> (raw)
In-Reply-To: <CAOBG12n+iWtKh2HBggfoErWF8h3GRVetE3gev2CFZtOoGnDoAg@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-sql>

På søndag 04. mai 2014 kl. 14:06:35, skrev Brice André <brice@famille-andre.be 
<mailto:brice@famille-andre.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@visena.com <mailto:andreas@visena.com> 
www.visena.com <https://www.visena.com;  <https://www.visena.com;  

view thread (7+ messages)  latest in thread

Message-ID: <OfficeNetEmail.b6.9cee154efc4db41a.145c81a866d@prod2>
Permalink:  ../OfficeNetEmail.b6.9cee154efc4db41a.145c81a866d@prod2/
Also on:    postgresql.org/message-id/OfficeNetEmail.b6.9cee154efc4db41a.145c81a866d@prod2

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: andreas@visena.com, brice@famille-andre.be
  Subject: Re: Optimize query for listing un-read messages
  In-Reply-To: <OfficeNetEmail.b6.9cee154efc4db41a.145c81a866d@prod2>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox