pg.ddx.io pgsql-performance@postgresql.org mailing list archive
help / color / mirror / Atom feedAggregate weirdness
2+ messages / 2 participants
[nested] [flat]
* Aggregate weirdness
@ 2008-11-04 10:56 Martin Kjeldsen <martin@martinkjeldsen.dk>
0 siblings, 1 reply; 2+ messages in thread
From: Martin Kjeldsen @ 2008-11-04 10:56 UTC (permalink / raw)
To: pgsql-performance
Hi,
I have a small problem. I have one view which sum's a another tables field and
uses that sum for several things including filtering. Every time it uses the
that summarised field in other queries or views, the planner seems to duplicate
the SUM. Isn't it possible for the planner only to do the SUM once and reuse it?
I've done a small example illustrating my problem. I'm using 8.3.3. Hope
somebody can tell my what I'm doing wrong or why this is happening. The problem
isn't great and I think I can work around it, using a stable function for doing
the SUM, but I'm still wondering.
Best regards
Martin
----------------------
EXAMPLE:
CREATE TABLE test (
id INTEGER,
name TEXT
);
INSERT INTO test (id, name) VALUES(1, 'ddd');
CREATE TABLE test_use (
test_id INTEGER,
number INTEGER
);
INSERT INTO test_use (test_id, number) VALUES(1, 3);
INSERT INTO test_use (test_id, number) VALUES(1, 4);
INSERT INTO test_use (test_id, number) VALUES(1, 1);
INSERT INTO test_use (test_id, number) VALUES(1, 27);
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;
CREATE OR REPLACE VIEW v_test_with_number_filtered AS
SELECT * FROM v_test_with_number WHERE numbers > 0;
EXPLAIN SELECT * FROM v_test_with_number;
QUERY PLAN
------------------------------------------------------------------------
Seq Scan on test t (cost=0.00..45274.00 rows=1230 width=36)
SubPlan
-> Aggregate (cost=36.78..36.79 rows=1 width=4)
-> Seq Scan on test_use (cost=0.00..36.75 rows=11 width=4)
Filter: (test_id = $0)
(5 rows)
EXPLAIN SELECT * FROM v_test_with_number_filtered;
QUERY PLAN
------------------------------------------------------------------------
Seq Scan on test t (cost=0.00..60360.97 rows=410 width=36)
Filter: ((subplan) > 0)
SubPlan
-> Aggregate (cost=36.78..36.79 rows=1 width=4)
-> Seq Scan on test_use (cost=0.00..36.75 rows=11 width=4)
Filter: (test_id = $0)
-> Aggregate (cost=36.78..36.79 rows=1 width=4)
-> Seq Scan on test_use (cost=0.00..36.75 rows=11 width=4)
Filter: (test_id = $0)
(9 rows)
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: Aggregate weirdness
@ 2008-11-04 13:39 Tom Lane <tgl@sss.pgh.pa.us>
parent: Martin Kjeldsen <martin@martinkjeldsen.dk>
0 siblings, 0 replies; 2+ messages in thread
From: Tom Lane @ 2008-11-04 13:39 UTC (permalink / raw)
To: Martin Kjeldsen <martin@martinkjeldsen.dk>; +Cc: 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
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2008-11-04 13:39 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2008-11-04 10:56 Aggregate weirdness Martin Kjeldsen <martin@martinkjeldsen.dk>
2008-11-04 13:39 ` Tom Lane <tgl@sss.pgh.pa.us>
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