Re: Optimize query for listing un-read messages

From: Brice André <brice(at)famille-andre(dot)be>
To: Andreas Joseph Krogh <andreas(at)visena(dot)com>
Cc: pgsql-sql <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Optimize query for listing un-read messages
Date: 2014-05-04 17:43:11
Message-ID: CAOBG12ksaHi990-X3XOKzU6E3ne9kFP9L89n2Dg2+8A9yjxy4g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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.

Regards,
Brice

2014-05-04 18:53 GMT+02:00 Andreas Joseph Krogh <andreas(at)visena(dot)com>:

> På søndag 04. mai 2014 kl. 18:49:43, skrev Brice André <
> brice(at)famille-andre(dot)be>:
>
> Yes, I was a bit too fast. but replace it with
>
> WHERE NOT prop.is_read = TRUE
>
> and it should be OK.
>
>
> No, that also will be treated as an INNER JOIN, because it kills tuples
> where prop is null. I need entries where prop IS NULL (hence the LEFT OUTER
> JOIN) because messages without an entry in message_property must be treated
> as unread, the same as messages with an entry in message_property where
> is_read=FALSE.
>
> --
> *Andreas Jospeh Krogh*
> CTO / Partner - Visena AS
> Mobile: +47 909 56 963
> andreas(at)visena(dot)com
> www.visena.com
> <https://www.visena.com>
>
>

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Andreas Joseph Krogh 2014-05-04 18:03:23 Re: Optimize query for listing un-read messages
Previous Message Andreas Joseph Krogh 2014-05-04 16:53:57 Re: Optimize query for listing un-read messages