Re: Many joins: monthly summaries S-L--O--W

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Michael Glaesmann <grzm(at)myrealbox(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Many joins: monthly summaries S-L--O--W
Date: 2003-10-21 17:27:34
Message-ID: 4557.1066757254@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice pgsql-sql

Michael Glaesmann <grzm(at)myrealbox(dot)com> writes:
> [ a very messy query ]

It does seem like that is a big query with a small query struggling to
get out. I don't have any immediate suggestions, but I wonder whether
you wouldn't profit by reading something about how to do crosstabs and
statistics in SQL. Joe Celko's book "SQL For Smarties" is an invaluable
resource for hard problems in SQL (be sure to get the 2nd edition).
Also, if you decide that a crosstab would help, take a look at
the crosstab functions in contrib/tablefunc. (Celko's book only covers
SQL-standard solutions, not methods that depend on nonstandard features,
so he's at a disadvantage when covering crosstab methods.)

Also, I do have a suggestion for this:

> current_inventory_view, showing the most recent inventory qty and date,
> is defined as
> SELECT inv.date, inv.product_code, inv.qty FROM inventory inv, (SELECT
> max(inventory.date) AS date, inventory.product_code FROM inventory
> GROUP BY inventory.product_code) curr_inv WHERE ((inv.date =
> curr_inv.date) AND (inv.product_code = curr_inv.product_code));

If you don't mind using a Postgres-specific feature, you should be able
to make this view faster by using DISTINCT ON. Look at the "weather
reports" example in the SELECT reference page.

regards, tom lane

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Michael Glaesmann 2003-10-21 19:16:16 Almost relational PostgreSQL (was: one-to-one)
Previous Message Tom Lane 2003-10-21 17:10:36 Re: Custom function problems

Browse pgsql-sql by date

  From Date Subject
Next Message Adam Witney 2003-10-21 18:42:36 Re: how to create a multi columns return function ?
Previous Message Peter Eisentraut 2003-10-21 16:48:21 [postgres] Re: Deutsche PostgreSQL-Mailingliste unter postgresql.org