Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 82C236501DF for ; Tue, 2 Dec 2008 11:14:15 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 28571-05-2 for ; Tue, 2 Dec 2008 11:14:09 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from 42.mail-out.ovh.net (42.mail-out.ovh.net [213.251.189.42]) by mail.postgresql.org (Postfix) with SMTP id 07811650203 for ; Tue, 2 Dec 2008 11:14:06 -0400 (AST) Received: (qmail 608 invoked by uid 503); 2 Dec 2008 15:13:39 -0000 Received: from b6.ovh.net (HELO mail412.ha.ovh.net) (213.186.33.56) by 42.mail-out.ovh.net with SMTP; 2 Dec 2008 15:13:39 -0000 Received: from b0.ovh.net (HELO queue-out) (213.186.33.50) by b0.ovh.net with SMTP; 2 Dec 2008 15:13:53 -0000 Received: from par69-8-88-161-102-87.fbx.proxad.net (HELO soyouz) (88.161.102.87) by ns0.ovh.net with SMTP; 2 Dec 2008 15:13:53 -0000 Date: Tue, 02 Dec 2008 16:14:58 +0100 To: Andrus , pgsql-performance@postgresql.org Subject: Re: analyzing intermediate query From: PFC Content-Type: text/plain; format=flowed; delsp=yes; charset=utf-8 MIME-Version: 1.0 References: <95776070DDC841978DF8335DCF59D6BB@andrusnotebook> Content-Transfer-Encoding: 7bit Message-ID: In-Reply-To: <95776070DDC841978DF8335DCF59D6BB@andrusnotebook> User-Agent: Opera Mail/9.62 (Linux) X-Ovh-Tracer-Id: 5814428595290572422 X-Ovh-Remote: 88.161.102.87 (par69-8-88-161-102-87.fbx.proxad.net) X-Ovh-Local: 213.186.33.20 (ns0.ovh.net) X-Spam-Check: DONE|H 0.5/N X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200812/14 X-Sequence-Number: 32050 >>> My list can contain 1 .. 100000 records and table contains 3000000 >>> records and is growing. >> >> Ah. No IN(), then ;) >> Temp table + ANALYZE seems your only option... > > In 8.3 or 8.4 I think that IN() or temp table produce exactly the same > result. > > Andrus. 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 integers... ) 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... Aggregate (cost=15000.00..15000.01 rows=1 width=4) (actual time=1060.253..1060.253 rows=1 loops=1) -> Values Scan on "*VALUES*" (cost=0.00..12500.00 rows=1000000 width=4) (actual time=0.009..634.728 rows=1000000 loops=1) Total runtime: 1091.420 ms The most interesting thing, of course, is that the statistics are exact. You can use VALUES like a table (Join, whatever). Of course it's always slightly annoying to juggle around with result sets and stuff them in comma-separated strings, but it works. Here it knows there's few rows ===> nested loop EXPLAIN SELECT a.* FROM annonces a JOIN (VALUES (0),(1),(2),(3),(4),(5),(6),(7)) AS v ON (a.id=v.column1); QUERY PLAN ---------------------------------------------------------------------------------------- Nested Loop (cost=0.00..66.73 rows=8 width=943) -> Values Scan on "*VALUES*" (cost=0.00..0.10 rows=8 width=4) -> Index Scan using annonces_pkey on annonces a (cost=0.00..8.32 rows=1 width=943) Index Cond: (a.id = "*VALUES*".column1) With a million values it goes hash of course, etc.