Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 8B0AA6324A7 for ; Mon, 1 Feb 2010 00:25:55 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 61607-07 for ; Mon, 1 Feb 2010 04:25:44 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-yw0-f176.google.com (mail-yw0-f176.google.com [209.85.211.176]) by mail.postgresql.org (Postfix) with ESMTP id CB23062DBF0 for ; Mon, 1 Feb 2010 00:25:44 -0400 (AST) Received: by ywh6 with SMTP id 6so3116402ywh.4 for ; Sun, 31 Jan 2010 20:25:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:reply-to:received :in-reply-to:references:from:date:x-google-sender-auth:message-id :subject:to:cc:content-type:content-transfer-encoding; bh=O3ojvALWoOc4URJt8cIOs4LGyWDgH7QT00eQMEDHS9U=; b=CIMXXyrC2qt3lCqdY5iNTDrSZj74A9bEJyjk2FNYFQ91sbU+NYPN2UaBeRw2micaaq EdiaWFo3xJ2rJFmwC/2SpNeiW+7fgSrDM4aoA+ncV+KZeCjoV3dogovDg3i5tvfLrQ3U oQhEEnDJISX8JG101xbCaxsnV3traVqwRZrkw= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:reply-to:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type :content-transfer-encoding; b=KDoEpq2faL7srQ1b9vfKbSubIc0BgUJ769Q+FrZPST/XuLsu3rjCtnA3qWmvJR39Za XwyeE+XIwq0a+Fw1dem5h8cHGNTS256GZrvIYzx690XIlbjpXeCTleCuOqOWIWBQVtbW wPGjZELJy/XifS1YgOitSwbxMrhvK70Hy89fc= MIME-Version: 1.0 Reply-To: dclements89@gmail.com Received: by 10.100.18.21 with SMTP id 21mr914114anr.173.1264998343929; Sun, 31 Jan 2010 20:25:43 -0800 (PST) In-Reply-To: <28233.1264995140@sss.pgh.pa.us> References: <1f30b80c1001290049ub864091r8298fca3daa0eb69@mail.gmail.com> <1f30b80c1001311650q264dfda0uf9894cac3b9e0c3e@mail.gmail.com> <28233.1264995140@sss.pgh.pa.us> From: Dave Clements Date: Mon, 1 Feb 2010 15:25:23 +1100 X-Google-Sender-Auth: e2f5fd8d7f682b26 Message-ID: <1f30b80c1001312025m118d5798lcb599aeb517beddc@mail.gmail.com> Subject: Re: Fwd: Help required on query performance To: Tom Lane Cc: pgsql-sql Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201002/1 X-Sequence-Number: 33600 After the analyze I am getting the time 3.20 ms but there is not HashJoin there. Still all of them are NestLoops. But that is fine. Now the only problem is the sequence scan on sq_sch_idx table. I have a query like this: explain analyze select count(*) from sq_sch_idx where value =3D '%download%= '; This query does a sequence scan on the table. Is there a way I can create an index for this? thanks On Mon, Feb 1, 2010 at 2:32 PM, Tom Lane wrote: > Dave Clements writes: >> Hello, I have this query in my system which takes around 2.5 seconds >> to run. I have diagnosed that the problem is actually a hashjoin on >> perm and s_ast_role tables. Is there a way I can avoid that join? > > BTW, just for the record, that diagnosis was completely off. =A0The > upper level of your explain results is > > =A0HashAggregate =A0(cost=3D38145.19..38145.20 rows=3D1 width=3D149) (act= ual time=3D2635.965..2636.086 rows=3D243 loops=3D1) > =A0 -> =A0Nested Loop =A0(cost=3D15.00..38145.18 rows=3D1 width=3D149) (a= ctual time=3D4.417..2635.086 rows=3D598 loops=3D1) > =A0 =A0 =A0 =A0 -> =A0Nested Loop =A0(cost=3D4.13..37993.95 rows=3D8 widt= h=3D153) (actual time=3D0.781..310.579 rows=3D975 loops=3D1) > =A0 =A0 =A0 =A0 =A0 =A0 ... > =A0 =A0 =A0 =A0 -> =A0Bitmap Heap Scan on sq_ast_lnk_tree t =A0(cost=3D10= .87..18.88 rows=3D2 width=3D4) (actual time=3D2.382..2.382 rows=3D1 loops= =3D975) > =A0 =A0 =A0 =A0 =A0 =A0 ... > > from which we can see that the main problem is doing the sq_ast_lnk_tree > scan over again 975 times, once per row coming out of the other side of > the join. =A0That accounted for 975*2.382 =3D 2322.450 msec, or the vast > majority of the runtime. =A0The planner wouldn't have picked this plan > except that it thought that only 8 rows would come out of the other side > of the join; repeating the scan 8 times seemed better than the > alternatives. =A0After you improved the statistics, it most likely > switched *to* a hash join (or possibly a merge join) for this step, > rather than switching away from one. > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0regards, tom lane >