Re: analyzing intermediate query

From: Scott Carey <scott(at)richrelevance(dot)com>
To: Andrus <kobruleht2(at)hot(dot)ee>, "pgsql-performance(at)postgresql(dot)org" <pgsql-performance(at)postgresql(dot)org>, PFC <lists(at)peufeu(dot)com>
Subject: Re: analyzing intermediate query
Date: 2008-12-02 17:50:03
Message-ID: BDFBB77C9E07BE4A984DAAE981D19F961ACA17D9EF@EXVMBX018-1.exch018.msoutlookonline.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Often times, switching an inner subselect that requires a distinct to a group by on that column yields better results. In this case, the IN should be equivalent, so it probably will not help. This would look like:

SELECT dok.*
FROM dok
JOIN (SELECT dokumnr FROM temptbl GROUP BY dokumnr ) x USING(dokumnr);

Whether that hepls depends on how big dokumnr is and where the query bottleneck is. Note there are subtle differences between DISTINCT and GROUP BY with respect to nulls.

________________________________________
From: pgsql-performance-owner(at)postgresql(dot)org [pgsql-performance-owner(at)postgresql(dot)org] On Behalf Of Andrus [kobruleht2(at)hot(dot)ee]
Sent: Tuesday, December 02, 2008 7:50 AM
To: pgsql-performance(at)postgresql(dot)org; PFC
Subject: Re: [PERFORM] analyzing intermediate query

> Oh, I just thought about something, I don't remember in which version it
> was added, but :
>
> EXPLAIN ANALYZE SELECT sum(column1) FROM (VALUES ...a million
> ntegers... ) AS v
>
> Postgres is perfectly happy with that ; it's either a bit slow (about 1
> second) or very fast depending on how you view things...

I tried in 8.1.4

select * from (values (0)) xx

but got

ERROR: syntax error at or near ")"
SQL state: 42601
Character: 26

Even if this works this may be not solution: I need to apply distinct to
temporary table. Temporary table may contain duplicate values and without
DISTINCT join produces invalid result.
Temporary table itself is created from data from server tables, it is not
generated from list.

I can use

SELECT dok.*
FROM dok
WHERE dokumnr IN (SELECT dokumnr FROM temptbl)

but this seems never use bitmap index scan in 8.1.4

Sadly, creating second temporary table from first temporary table specially
for this query seems to be only solution.

When materialized row count will be added so that statistics is exact and
select count(*) from tbl runs fast ?

Andrus.

--
Sent via pgsql-performance mailing list (pgsql-performance(at)postgresql(dot)org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Andrus 2008-12-02 18:58:43 Re: analyzing intermediate query
Previous Message DANIEL CRISTIAN CRUZ 2008-12-02 16:36:51 Re: Not so simple query and a half million loop