Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id C6DD72E0047 for ; Tue, 19 Feb 2008 10:09:59 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 14631-04 for ; Tue, 19 Feb 2008 10:09:46 -0400 (AST) Received: from cat.co.za (mailer.cat.co.za [196.34.125.51]) by postgresql.org (Postfix) with SMTP id D5C202E0038 for ; Tue, 19 Feb 2008 10:09:44 -0400 (AST) Received: (qmail 22404 invoked by uid 1002); 19 Feb 2008 14:17:29 -0000 Received: from stuartb@cat.co.za by mail.cat.co.za by uid 807 with qmail-scanner-1.21 (clamscan: 0.70. Clear:RC:1(196.34.125.52):. Processed in 3.558251 secs); 19 Feb 2008 14:17:29 -0000 Received: from unknown (HELO ?192.168.34.201?) (196.34.125.52) by 0 with SMTP; 19 Feb 2008 14:17:25 -0000 Message-ID: <47BAE223.3000100@cat.co.za> Date: Tue, 19 Feb 2008 16:05:23 +0200 From: Stuart Brooks User-Agent: Thunderbird 2.0.0.9 (Windows/20071031) MIME-Version: 1.0 To: Richard Huxton CC: pgsql-sql@postgresql.org Subject: Re: Am I wasting my time with partitions? References: <47BAB556.7060506@cat.co.za> <47BADE1C.10903@archonet.com> In-Reply-To: <47BADE1C.10903@archonet.com> 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=0.269 tagged_above=0 required=5 tests=AWL=-0.141, SUBJECT_FUZZY_TION=0.41 X-Spam-Level: X-Archive-Number: 200802/152 X-Sequence-Number: 30243 > It seems to me that postgresql doesn't use indexes when being asked > for an ordered result sets from a partitioned table. I have an > application where this is critical, but I was hoping to use partitions > because of the ease of rotating out old rows. > >> metadb=> explain select * from l order by amount,lineitem_key limit 10; >> QUERY PLAN >> -> Seq Scan on l (cost=0.00..16.90 rows=690 >> width=88) >> -> Seq Scan on l1 l (cost=0.00..4951.00 >> rows=250000 width=49) >> -> Seq Scan on l2 l (cost=0.00..5653.66 >> rows=285466 width=49) > >> NB. Just addressing one of the inherited tables works fine. >> >> metadb=> explain select * from l1 order by amount,lineitem_key limit 10; > > Well, you don't have an index it can use to find the smallest > (amount,lineitem) across all of lX. If PG was smart enough to figure > out that it only needed to check l1, then you do. Unfortunately it isn't. > You're right, it can't determine which of the partitions will have the smallest value, but what it could do is pull the smallest value from each and compare. In the absence of the LIMIT there wouldn't be much which could be done, but the the LIMIT means it only actually needs to pull 10 rows from each partition. An alternative way of doing this would be: (SELECT * FROM L1 ORDER BY amount,lineitem_key LIMIT 10) UNION (SELECT * FROM L2 ORDER BY amount,lineitem_key LIMIT 10) ORDER BY amount,lineitem_key LIMIT 10; Unfortunately this means one can't just address the parent table, but it does essentially what I'd hoped postgres would do for me :) It would be quite a long query if there were 100 partitions! > If you add the constraint you use to partition by, does that help you? > I tried to strip the example down to its bare essentials but in this case I would be partitioning by lineitem_key and would obviously index and add a CONSTRAINT on that as well. I don't think it would help though, the query needs to merge from all tables. Thanks for the response, Stuart