Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id CE561641CE6 for ; Thu, 10 Jun 2010 16:47:33 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 38788-02 for ; Thu, 10 Jun 2010 19:47:21 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail-wy0-f174.google.com (mail-wy0-f174.google.com [74.125.82.174]) by mail.postgresql.org (Postfix) with ESMTP id A6C6A641CA7 for ; Thu, 10 Jun 2010 16:47:20 -0300 (ADT) Received: by wyb40 with SMTP id 40so220349wyb.19 for ; Thu, 10 Jun 2010 12:47:19 -0700 (PDT) Received: by 10.216.156.1 with SMTP id l1mr870204wek.27.1276199237273; Thu, 10 Jun 2010 12:47:17 -0700 (PDT) Received: from Craig-Jamess-MacBook-Pro.local (wsip-70-167-124-126.sd.sd.cox.net [70.167.124.126]) by mx.google.com with ESMTPS id w27sm198732weq.22.2010.06.10.12.47.09 (version=SSLv3 cipher=RC4-MD5); Thu, 10 Jun 2010 12:47:11 -0700 (PDT) Message-ID: <4C11413B.8080107@emolecules.com> Date: Thu, 10 Jun 2010 12:47:07 -0700 From: Craig James User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.9) Gecko/20100317 Thunderbird/3.0.4 MIME-Version: 1.0 To: Anne Rosset CC: pgsql-performance@postgresql.org Subject: Re: Need to increase performance of a query References: <4C1125F0.8090101@collab.net> <4C112B19.6080908@krogh.cc> <4C113098.5040105@collab.net> <98414706.20100610212211@erwied.eu> <4C113E2F.6000305@collab.net> In-Reply-To: <4C113E2F.6000305@collab.net> Content-Type: text/plain; charset=ISO-8859-1; 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, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201006/182 X-Sequence-Number: 39038 On 6/10/10 12:34 PM, Anne Rosset wrote: > Jochen Erwied wrote: >> Thursday, June 10, 2010, 8:36:08 PM you wrote: >> >>> psrdb=# (SELECT >>> psrdb(# MAX(item_rank.rank) AS maxRank >>> psrdb(# FROM >>> psrdb(# item_rank item_rank >>> psrdb(# WHERE >>> psrdb(# item_rank.project_id='proj2783' >>> psrdb(# AND item_rank.pf_id IS NULL >>> psrdb(# >>> psrdb(# ) >>> psrdb-# ORDER BY >>> psrdb-# maxRank DESC; >> >> Don't think it does really matter, but why do you sort a resultset >> consisting of only one row? >> > Sorry, I should have removed the ORDER by (the full query has a union). > So without the ORDER by, here are the results: > psrdb=# SELECT > psrdb-# MAX(item_rank.rank) AS maxRank > psrdb-# FROM > psrdb-# item_rank item_rank > psrdb-# WHERE > psrdb-# item_rank.pf_id='plan1408'; > maxrank > ------------- > 20504000000 > (1 row) > > Time: 1.516 ms > psrdb=# SELECT > psrdb-# MAX(item_rank.rank) AS maxRank > psrdb-# FROM > psrdb-# item_rank item_rank > psrdb-# WHERE > psrdb-# item_rank.project_id='proj2783' > psrdb-# AND item_rank.pf_id IS NULL; > maxrank > ------------- > 20200000000 > (1 row) > > Time: 13.177 ms > > Is there anything that can be done for the second one? Postgres normally doesn't index NULL values even if the column is indexed, so it has to do a table scan when your query includes an IS NULL condition. You need to create an index that includes the "IS NULL" condition. create index item_rank_null_idx on item_rank(pf_id) where item_rank.pf_id is null; Craig