From: Joe <dev@freedomcircle.net>
To: Aaron Bono <postgresql@aranya.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Repetitive code
Date: Fri, 16 Jun 2006 11:12:22 -0400
Message-ID: <4492CA56.5020505@freedomcircle.net> (raw)
In-Reply-To: <bf05e51c0606160744m5f28243cjf0fd6da11f48404d@mail.gmail.com>
References: <44920AA7.7070709@freedomcircle.net>
<bf05e51c0606151926n18555a65x28a0156f8bd217cc@mail.gmail.com>
<4492B248.7040406@freedomcircle.net>
<bf05e51c0606160744m5f28243cjf0fd6da11f48404d@mail.gmail.com>
Aaron Bono wrote:
> I haven't stared at your query as long as you have so I may have missed
> something but it looks like in all the selects you are combining the
> first column in the select is the column you filter on. So the the
> outer query doesn't have to know wiether it is a new or changed row:
>
> SELECT * FROM (
> SELECT created as my_date, topic_id, 0, 0, 0, 0 FROM topic
> WHERE page_type IN (1, 2)
> UNION
> [snip]
> SELECT e.updated as my_date, e.topic_id, 1, 1, entry_id, rel_type
> FROM topic_entry e, topic t
> WHERE e.topic_id = t.topic_id
> AND date_trunc('day', e.updated) != e.created
> AND page_type IN (1, 2)
> ) my_union
> where my_union.my_date >= $dt
Thanks Aaron. That does look like a great solution, overlooked since
I'm not that familiar with SELECTs in the FROM clause. It may even make
it possible to discard the interim table and do the web page/RSS feed
directly from the view.
> I would almost be tempted to create a view for each small query and name
> them something meaningful and then another view that does the union. It
> would make the queries easier to understand at least (self documented).
That sounds like a good idea too because schema changes would be
somewhat insulated by the layered views.
Best regards,
Joe
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: dev@freedomcircle.net, postgresql@aranya.com
Subject: Re: Repetitive code
In-Reply-To: <4492CA56.5020505@freedomcircle.net>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox