Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 5975B64219E for ; Thu, 10 Jun 2010 16:56:59 -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 58278-04 for ; Thu, 10 Jun 2010 19:56:51 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sp-exchfea.sp.corp.collab.net (sp-exchfea.sp.corp.collab.net [204.16.106.198]) by mail.postgresql.org (Postfix) with ESMTP id 8F565636D9B for ; Thu, 10 Jun 2010 16:56:51 -0300 (ADT) Received: from [204.11.125.143] ([204.11.125.143]) by sp-exchfea.sp.corp.collab.net with Microsoft SMTPSVC(6.0.3790.3959); Thu, 10 Jun 2010 12:56:49 -0700 Message-ID: <4C114381.3030900@collab.net> Date: Thu, 10 Jun 2010 12:56:49 -0700 From: Anne Rosset User-Agent: Thunderbird 2.0.0.24 (Windows/20100228) MIME-Version: 1.0 To: Craig James 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> <4C11413B.8080107@emolecules.com> In-Reply-To: <4C11413B.8080107@emolecules.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 10 Jun 2010 19:56:49.0847 (UTC) FILETIME=[10871470:01CB08D7] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.011 tagged_above=-10 required=5 tests=BAYES_20=-0.001, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201006/183 X-Sequence-Number: 39039 Craig James wrote: > 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 > Hi Craig, I tried again after adding your suggested index but I didn't see any improvements: (seems that the index is not used) psrdb=# explain analyze 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; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------- Result (cost=0.17..0.18 rows=1 width=0) (actual time=11.942..11.943 rows=1 loops=1) InitPlan -> Limit (cost=0.00..0.17 rows=1 width=8) (actual time=11.931..11.932 rows=1 loops=1) -> Index Scan Backward using item_rank_rank on item_rank (cost=0.00..2933.84 rows=17558 width=8) (actual time=11.926..11.926 rows=1 loops=1) Filter: ((rank IS NOT NULL) AND (pf_id IS NULL) AND ((project_id)::text = 'proj2783'::text)) Total runtime: 11.988 ms (6 rows) Time: 13.654 ms Thanks, Anne