Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 4C496B5DBC4 for ; Sun, 25 Sep 2011 22:24:15 -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 93304-06 for ; Mon, 26 Sep 2011 01:24:09 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 073C4B5DBD6 for ; Sun, 25 Sep 2011 22:24:08 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p8Q1O5au012135; Sun, 25 Sep 2011 21:24:05 -0400 (EDT) To: Stephen Frost cc: Michael Viscuso , Greg Smith , pgsql-performance@postgresql.org Subject: Re: Query optimization using order by and limit In-reply-to: <20110926003428.GS12765@tamriel.snowman.net> References: <4E7A93F9.1050301@2ndQuadrant.com> <6768.1316657361@sss.pgh.pa.us> <4E7AA399.6000209@getcarbonblack.com> <20110922145319.GJ12765@tamriel.snowman.net> <8338.1316989350@sss.pgh.pa.us> <20110926003428.GS12765@tamriel.snowman.net> Comments: In-reply-to Stephen Frost message dated "Sun, 25 Sep 2011 20:34:28 -0400" Date: Sun, 25 Sep 2011 21:24:05 -0400 Message-ID: <12134.1317000245@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.404 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-0.504 X-Spam-Level: X-Archive-Number: 201109/360 X-Sequence-Number: 45136 Stephen Frost writes: > * Tom Lane (tgl@sss.pgh.pa.us) wrote: >> Yeah, it's spending quite a lot of time finding the first matching row >> in each child table. I'm curious why that is though; are the child >> tables not set up with nonoverlapping firstloadtime ranges? > The issue here is that the query is saying "Give me the first 150 > records with this host_id in this week-long range". Oh, I see. So the query range overlaps multiple child tables, even after constraint exclusion eliminates a lot of them. > My point above is that the CHECK constraints ensure an ordering which > could be leveraged to use the latest table first and then stop if enough > tuples are returned (or immediately go to the next table), without ever > considering the other tables. Yeah. My opinion is that trying to reverse-engineer that from the CHECK constraints would cost a lot more than it's worth. What we need, and will hopefully have sooner or later, is an abstract concept of "partitioned table" in which this kind of relationship is known a-priori instead of having to be laboriously re-deduced every time we plan a query. >> If you're stuck with this table arrangement, one thing that would help >> is a two-column index on (host_guid, firstloadtime) on each child table. > Agreed, I mentioned this to the OP previously and it's on his list of > things to try. AFAICS the fact that this example would be fast if we were only paying attention to the newest table is mere luck. If it can take a long time to find the first matching host_guid record in several of the child tables, why might it not take just as long to find said record in the other one? I think you really need the two-column indexes, if keeping this query's runtime to a minimum is critical. regards, tom lane