Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id D9C2A632768 for ; Thu, 15 Jul 2010 04:57:55 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 57532-03 for ; Thu, 15 Jul 2010 07:57:48 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-ey0-f174.google.com (mail-ey0-f174.google.com [209.85.215.174]) by mail.postgresql.org (Postfix) with ESMTP id B3B77632510 for ; Thu, 15 Jul 2010 04:57:47 -0300 (ADT) Received: by eya25 with SMTP id 25so98333eya.19 for ; Thu, 15 Jul 2010 00:57:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from :user-agent:mime-version:to:cc:subject:references:in-reply-to :content-type:content-transfer-encoding; bh=FzQE5FUzTFwpB+bF+kO+K53WhZED0nmLZkKMjRW5yrw=; b=QWHhrD+yE4vOjOOALDCtQ/hxR2LS46PFOSP9INR8bD2rBbp3bSCGgcOBiDd+zMSe2u Am9RR7k/haWWdtBAQx14yA3b8EWh9xybYuqo0u2OS5LYW+4xy11mQrohFcl2f3Z/EaFB OmMUxYQKY4Uz8PPz087Q4LUB2WoCR2IwhXOHs= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; b=emnqmRS2px7SmMXWF66XwaaC5WhSxJU0G7PE6XwsuE6bEKTjOZ05TzZrBNqdu1t3k8 RPTWiADTX03gXHpBx1swYoeQainExd+pey0ldwnveelV8GQsaezcKDxkVfOQAxYxpi/F q6k007aBu371OjUNVpiL0txHZR5bpFd8pC2Jg= Received: by 10.213.31.148 with SMTP id y20mr2421342ebc.9.1279180665741; Thu, 15 Jul 2010 00:57:45 -0700 (PDT) Received: from [0.0.0.0] (5357DA75.cable.casema.nl [83.87.218.117]) by mx.google.com with ESMTPS id v59sm5861012eeh.22.2010.07.15.00.57.43 (version=TLSv1/SSLv3 cipher=RC4-MD5); Thu, 15 Jul 2010 00:57:44 -0700 (PDT) Message-ID: <4C3EBFBA.1040408@gmail.com> Date: Thu, 15 Jul 2010 09:58:50 +0200 From: Yeb Havinga User-Agent: Thunderbird 2.0.0.24 (Windows/20100228) MIME-Version: 1.0 To: Zotov CC: pgsql-performance@postgresql.org Subject: Re: Query optimization problem References: <4C3EA6CB.4070502@oe-it.ru> In-Reply-To: <4C3EA6CB.4070502@oe-it.ru> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201007/233 X-Sequence-Number: 39622 Hello Zotov, Somehow the equivalence d2.basedon=d1.id is not used in the slow query, probably because the equivalence constant value would be used inside a not-base expression (the OR). You can see that the equivalence values *are* used by changing the or to an and and compare both queries. The only thing you can do to guarantee the planner has all information to in cases like this it explicitly name the equivalence inside OR expressions, e.g. SELECT d1.ID, d2.ID FROM DocPrimary d1 JOIN DocPrimary d2 ON d2.BasedOn=d1.ID WHERE (d1.ID=234409763 and d2.basedon=234409763) or (d2.ID=234409763) ; regards, Yeb Havinga PS: the analyze time of the slow query showed 0.007ms? Zotov wrote: > I have a query: > > SELECT d1.ID, d2.ID > FROM DocPrimary d1 > JOIN DocPrimary d2 ON d2.BasedOn=d1.ID > WHERE (d1.ID=234409763) or (d2.ID=234409763) > > i think what QO(Query Optimizer) can make it faster (now it seq scan > and on > million records works 7 sec) > > SELECT d1.ID, d2.ID > FROM DocPrimary d1 > JOIN DocPrimary d2 ON d2.BasedOn=d1.ID > WHERE (d2.BasedOn=234409763) or (d2.ID=234409763) > > > ---------------------- > Slow Query > ---------------------- > test=# EXPLAIN (ANALYZE on, VERBOSE on, COSTS on, BUFFERS off )SELECT > d1.ID, > d2.ID > test-# FROM DocPrimary d1 > test-# JOIN DocPrimary d2 ON d2.BasedOn=d1.ID > test-# WHERE (d1.ID=234409763) or (d2.ID=234409763); > QUERY PLAN > ------------------------------------------------------------------------------------------------------------------------ > > Hash Join (cost=58.15..132.35 rows=2 width=8) (actual > time=0.007..0.007 > rows=0 loops=1) > Output: d1.id, d2.id > Hash Cond: (d2.basedon = d1.id) > Join Filter: ((d1.id = 234409763) OR (d2.id = 234409763)) > -> Seq Scan on public.docprimary d2 (cost=0.00..31.40 rows=2140 > width=8) (actual time=0.002..0.002 rows=0 loops=1) > Output: d2.id, d2.basedon > -> Hash (cost=31.40..31.40 rows=2140 width=4) (never executed) > Output: d1.id > -> Seq Scan on public.docprimary d1 (cost=0.00..31.40 > rows=2140 > width=4) (never executed) > Output: d1.id > > ------------------ > Fast Query > ------------------ > test=# EXPLAIN (ANALYZE on, VERBOSE on, COSTS on, BUFFERS off )SELECT > d1.ID, > d2.ID > test-# FROM DocPrimary d1 > test-# JOIN DocPrimary d2 ON d2.BasedOn=d1.ID > test-# WHERE (d2.BasedOn=234409763) or (d2.ID=234409763); > QUERY PLAN > --------------------------------------------------------------------------------------------------------------------------------- > > Nested Loop (cost=8.60..58.67 rows=12 width=8) (actual > time=0.026..0.026 > rows=0 loops=1) > Output: d1.id, d2.id > -> Bitmap Heap Scan on public.docprimary d2 (cost=8.60..19.31 > rows=12 > width=8) (actual time=0.023..0.023 rows=0 loops=1) > Output: d2.id, d2.basedon > Recheck Cond: ((d2.basedon = 234409763) OR (d2.id = > 234409763)) > -> BitmapOr (cost=8.60..8.60 rows=12 width=0) (actual > time=0.018..0.018 rows=0 loops=1) > -> Bitmap Index Scan on basedon_idx (cost=0.00..4.33 > rows=11 width=0) (actual time=0.008..0.008 rows=0 loops=1) > Index Cond: (d2.basedon = 234409763) > -> Bitmap Index Scan on id_pk (cost=0.00..4.26 rows=1 > width=0) (actual time=0.003..0.003 rows=0 loops=1) > Index Cond: (d2.id = 234409763) > -> Index Scan using id_pk on public.docprimary d1 > (cost=0.00..3.27 > rows=1 width=4) (never executed) > Output: d1.id, d1.basedon > Index Cond: (d1.id = d2.basedon) > > > -------------------------------------------- > PGver: PostgreSQL 9.0b x86 > OS: Win7 x64 > > --------------------- > Create table query: > --------------------- > > CREATE TABLE docprimary > ( > id integer NOT NULL, > basedon integer, > CONSTRAINT id_pk PRIMARY KEY (id) > ); > CREATE INDEX basedon_idx > ON docprimary > USING btree > (basedon); > >