Re: Aggregate weirdness - Mailing list pgsql-performance

From Tom Lane
Subject Re: Aggregate weirdness
Date
Msg-id 16439.1225805972@sss.pgh.pa.us
Whole thread Raw
In response to Aggregate weirdness  (Martin Kjeldsen <martin@martinkjeldsen.dk>)
List pgsql-performance
Martin Kjeldsen <martin@martinkjeldsen.dk> writes:
> CREATE OR REPLACE VIEW v_test_with_number AS
>         SELECT
>                 t.*,
>                 (SELECT SUM(number) FROM test_use WHERE test_id = t.id) as numbers
>         FROM test t;

This is a bad way to do it --- the sub-select isn't readily optimizable.
Try something like

 SELECT t.id, t.name, sum(test_use.number) AS numbers
   FROM test_use
   JOIN test t ON test_use.test_id = t.id
  GROUP BY t.id, t.name;


            regards, tom lane

pgsql-performance by date:

Previous
From: "sathiya psql"
Date:
Subject: epqa; postgres performance optimizer support tool; opensource.
Next
From: "praveen"
Date:
Subject: Installation Error of postgresql-8.1.5 with perl.