Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 89B649FB1C9 for ; Fri, 24 Aug 2007 13:20:40 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 41157-06 for ; Fri, 24 Aug 2007 13:20:39 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from exprod6og56.obsmtp.com (exprod6og56.obsmtp.com [64.18.1.208]) by postgresql.org (Postfix) with SMTP id 74B209FB1B8 for ; Fri, 24 Aug 2007 13:20:39 -0300 (ADT) Received: from source ([63.240.6.44]) (using TLSv1) by exprod6ob56.postini.com ([64.18.5.12]) with SMTP; Fri, 24 Aug 2007 09:20:34 PDT Received: from 172.16.1.243 by mail.mi8.com with ESMTP (- Welcome to Mi8 Corporation www.Mi8.com (D4)); Fri, 24 Aug 2007 12:21:04 -0400 X-Server-Uuid: C8FB4D43-1108-484A-A898-3CBCC7906230 Received: from MI8NYCMAIL08.Mi8.com ([172.16.1.164]) by D01SMTP04.Mi8.com with Microsoft SMTPSVC(6.0.3790.1830); Fri, 24 Aug 2007 12:21:04 -0400 Received: from 209.131.62.113 ([209.131.62.113]) by MI8NYCMAIL08.Mi8.com ([172.16.1.190]) via Exchange Front-End Server mi8owa.mi8.com ( [172.16.1.134]) with Microsoft Exchange Server HTTP-DAV ; Fri, 24 Aug 2007 16:21:03 +0000 User-Agent: Microsoft-Entourage/11.3.6.070618 Date: Fri, 24 Aug 2007 09:20:28 -0700 Subject: Re: partitioned table and ORDER BY indexed_field DESC LIMIT 1 From: "Luke Lonergan" To: "Heikki Linnakangas" , "Anton" cc: pgsql-performance@postgresql.org Message-ID: Thread-Topic: [PERFORM] partitioned table and ORDER BY indexed_field DESC LIMIT 1 Thread-Index: AcfmOwLg5KgGzyOtRWi3sUHduZNHdwAL6wYE In-Reply-To: <46CEB522.9030907@enterprisedb.com> MIME-Version: 1.0 X-OriginalArrivalTime: 24 Aug 2007 16:21:04.0483 (UTC) FILETIME=[C4BA8B30:01C7E66A] X-WSS-ID: 6AD1DAE32UO8785842-11-01 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200708/287 X-Sequence-Number: 26417 We just fixed this - I'll post a patch, but I don't have time to verify against HEAD. - Luke On 8/24/07 3:38 AM, "Heikki Linnakangas" wrote: > Anton wrote: >>>> =# explain SELECT * FROM n_traf ORDER BY date_time DESC LIMIT 1; >>>> QUERY PLAN >>> ---------------------------------------------------------------------------- >>> ----------------------------- >>>> Limit (cost=824637.69..824637.69 rows=1 width=32) >>>> -> Sort (cost=824637.69..838746.44 rows=5643499 width=32) >>>> Sort Key: public.n_traf.date_time >>>> -> Result (cost=0.00..100877.99 rows=5643499 width=32) >>>> -> Append (cost= 0.00..100877.99 rows=5643499 width=32) >>>> -> Seq Scan on n_traf (cost=0.00..22.30 >>>> rows=1230 width=32) >>>> -> Seq Scan on n_traf_y2007m01 n_traf >>>> (cost=0.00..22.30 rows=1230 width=32) >> ... >>>> -> Seq Scan on n_traf_y2007m12 n_traf >>>> (cost=0.00..22.30 rows=1230 width=32) >>>> (18 rows) >>>> >>>> Why it no uses indexes at all? >>>> ------------------------------------------- >>> I'm no expert but I'd guess that the the planner doesn't know which >>> partition holds the latest time so it has to read them all. >> >> Agree. But why it not uses indexes when it reading them? > > The planner isn't smart enough to push the "ORDER BY ... LIMIT ..." > below the append node. Therefore it needs to fetch all rows from all the > tables, and the fastest way to do that is a seq scan.