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 20:03:23 +0200 (CEST)
Message-ID: <OfficeNetEmail.f.6326dbf6bf0bb59a.145c8659a94@prod2> (raw)
In-Reply-To: <CAOBG12ksaHi990-X3XOKzU6E3ne9kFP9L89n2Dg2+8A9yjxy4g@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-sql>

På søndag 04. mai 2014 kl. 19:43:11, skrev Brice André <brice@famille-andre.be 
<mailto:brice@famille-andre.be>>: Forget my last answer : it was a stupid 
one... I tried to answer quickly, but with tiredness, it does not give good 
results.
   For me, your problem of performance comes from the "WHERE NOT EXISTS 
(query)" because your query is executed on each result of the outer join.
   I tried to figure out how you can avoid this with your current database 
design, but I did not found any solution. Maybe someone on the forum will have 
an idea.
   If not, what I can propose your is to arrange yourself so that, for each 
couple (message, user) of your database, you have a corresponding entry in 
message_property, so that the first solution I proposed you (with an inner 
join) will work. And with multi-column indexes, it should be fast.
   To do so, you can use trigger mechanism on both the insertion of the 
message to create all message_property entries of that message, and on user 
insertion to create all message_properties of the user, so that you do not need 
to change anything outside your SQL design.
   The disadvantages of this solution are that the insertion of a new message 
or of a new message will be slower, and that your database size will be 
greater, but it should solve the problem of fast determining all read or unread 
messages of a dedicated user.   Yes, the reason it cannot be fast is because PG 
is unable to index the difference between two sets, so my schema, although a 
correct one, isn't index friendly so a caching-mechanism must be used for fast, 
indexed access. The solution is to redesign and have an entry in 
message_property for each combination of user/message.   -- 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)

Message-ID: <OfficeNetEmail.f.6326dbf6bf0bb59a.145c8659a94@prod2>
Permalink:  ../OfficeNetEmail.f.6326dbf6bf0bb59a.145c8659a94@prod2/
Also on:    postgresql.org/message-id/OfficeNetEmail.f.6326dbf6bf0bb59a.145c8659a94@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.f.6326dbf6bf0bb59a.145c8659a94@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