Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id F1CE71337B92 for ; Wed, 23 Mar 2011 10:34:37 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 92484-09 for ; Wed, 23 Mar 2011 13:34:30 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtp.peak6.com (smtp.optionshouse.com [38.98.137.6]) by mail.postgresql.org (Postfix) with ESMTP id 2BAB51337B43 for ; Wed, 23 Mar 2011 10:34:29 -0300 (ADT) X-AuditID: 0a64cb1e-b7c55ae000000bfb-ad-4d89f6e58d2b Message-ID: <4D89F6E5.7010803@peak6.com> Date: Wed, 23 Mar 2011 08:34:29 -0500 From: Shaun Thomas Reply-To: sthomas@peak6.com Organization: Peak6 User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.7) Gecko/20100720 Lightning/1.0b2 Thunderbird/3.1.1 MIME-Version: 1.0 To: Adarsh Sharma CC: "pgsql-performance@postgresql.org" Subject: Re: Re-Reason of Slowness of Query References: <4D89BABF.4080806@orkash.com> In-Reply-To: <4D89BABF.4080806@orkash.com> Content-Type: text/plain; charset="ISO-8859-1"; format=flowed content-transfer-encoding: 7bit X-Brightmail-Tracker: AAAAAQAAAZE= 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: 201103/278 X-Sequence-Number: 42957 On 03/23/2011 04:17 AM, Adarsh Sharma wrote: > explain analyze select distinct(p.crawled_page_id) from page_content > p where NOT EXISTS (select 1 from clause2 c where c.source_id = > p.crawled_page_id); You know... I'm surprised nobody has mentioned this, but DISTINCT is very slow unless you have a fairly recent version of Postgres that replaces it with something faster. Try this: EXPLAIN ANALYZE SELECT p.crawled_page_id FROM page_content p WHERE NOT EXISTS ( SELECT 1 FROM clause2 c WHERE c.source_id = p.crawled_page_id ) GROUP BY p.crawled_page_id; Or if you like the cleaner query without a sub-select: EXPLAIN ANALYZE SELECT p.crawled_page_id FROM page_content p LEFT JOIN clause2 c ON (c.source_id = p.crawled_page_id) WHERE c.source_id IS NULL GROUP BY p.crawled_page_id; -- Shaun Thomas OptionsHouse | 141 W. Jackson Blvd. | Suite 800 | Chicago IL, 60604 312-676-8870 sthomas@peak6.com ______________________________________________ See http://www.peak6.com/email_disclaimer.php for terms and conditions related to this email