Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 4F0B51337CBA for ; Wed, 27 Apr 2011 21:59:47 -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 32799-01-9 for ; Thu, 28 Apr 2011 00:59:25 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail-pz0-f46.google.com (mail-pz0-f46.google.com [209.85.210.46]) by mail.postgresql.org (Postfix) with ESMTP id 66D5413381F2 for ; Wed, 27 Apr 2011 21:40:27 -0300 (ADT) Received: by pzk9 with SMTP id 9so1353940pzk.19 for ; Wed, 27 Apr 2011 17:40:27 -0700 (PDT) MIME-Version: 1.0 Received: by 10.68.59.164 with SMTP id a4mr1543425pbr.304.1303949897351; Wed, 27 Apr 2011 17:18:17 -0700 (PDT) Received: by 10.68.48.68 with HTTP; Wed, 27 Apr 2011 17:18:17 -0700 (PDT) X-Originating-IP: [66.215.95.51] In-Reply-To: <4DB88690.5050701@selectacast.net> References: <4DB88690.5050701@selectacast.net> Date: Wed, 27 Apr 2011 17:18:17 -0700 Message-ID: Subject: Re: index usage on queries on inherited tables From: Samuel Gendler To: Joseph Shraibman Cc: Robert Haas , pgsql-performance@postgresql.org Content-Type: multipart/alternative; boundary=bcaec539639c6fc29d04a1ef7e88 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.899 tagged_above=-10 required=5 tests=BAYES_00=-1.9, HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 201104/385 X-Sequence-Number: 43447 --bcaec539639c6fc29d04a1ef7e88 Content-Type: text/plain; charset=ISO-8859-1 On Wed, Apr 27, 2011 at 2:11 PM, Joseph Shraibman wrote: > On 04/27/2011 04:32 PM, Robert Haas wrote: > > In the first case, PostgreSQL evidently thinks that using the indexes > > will be slower than just ignoring them. You could find out whether > > it's right by trying it with enable_seqscan=off. > > My point is that this is just a problem with inherited tables. It > should be obvious to postgres that few rows are being returned, but in > the inherited tables case it doesn't use indexes. This was just an > example. In a 52 gig table I have a "select id from table limit 1 order > by id desc" returns instantly, but as soon as you declare a child table > it tries to seq scan all the tables. > > If I'm understanding correctly, this kind of obviates the utility of partitioning if you structure a warehouse in a traditional manner. Assuming a fact table partitioned by time, but with foreign keys to a time dimension, it is now not possible to gain any advantage from the partitioning if selecting on columns in the time dimension. "select * from fact_table f join time_dimension t on f.time_id = t.time_id where t.quarter=3 and t.year = 2010" will scan all partitions of the fact table despite the fact that all of the rows would come from 3 partitions, assuming a partitioning schema that uses one partition for each month. I use a time id that is calculable from the from the timestamp so it doesn't need to be looked up, and partitioning on time_id directly is easy enough to handle, but if I'm understanding the problem, it sounds like nothing short of computing the appropriate time ids before issuing the query and then including a 'where f.time_id between x and y' clause to the query will result in the partitions being correctly excluded. Is that what people are doing to solve this problem? The alternative is to leave a timestamp column in the fact table (something I tend to do since it makes typing ad-hoc queries in psql much easier) and partition on that column and then always include a where clause for that column that is at least as large as the requested row range. Both result in fairly ugly queries, though I can certainly see how I might structure my code to always build queries which adhere to this. I'm just in the process of designing a star schema for a project and was intending to use exactly the structure I described at the top of the email. Is there a postgres best-practices for solving this problem? There's no way I can get away without partitioning. I'm looking at a worst case table of 100,000 rows being written every 5 minutes, 24x7 - 29 million rows per day, a billion rows per month - with most queries running over a single month or comparing same months from differing years and quarters - so a month based partitioning. Normal case is closer to 10K rows per 5 minutes. Suggestions? --sam --bcaec539639c6fc29d04a1ef7e88 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

On Wed, Apr 27, 2011 at 2:11 PM, Joseph = Shraibman <jks@= selectacast.net> wrote:
On 04/27/2011 04:32 PM, Robert Haas wrote:
> In the first case, PostgreSQL evidently thinks that using the indexes<= br> > will be slower than just ignoring them. =A0You could find out whether<= br> > it's right by trying it with enable_seqscan=3Doff.

My point is that this is just a problem with inherited tables. =A0It<= br> should be obvious to postgres that few rows are being returned, but in
the inherited tables case it doesn't use indexes. =A0This was just an example. =A0In a 52 gig table I have a "select id from table limit 1 o= rder
by id desc" returns instantly, but as soon as you declare a child tabl= e
it tries to seq scan all the tables.


If I'm understanding correctly, this kind of obviates the utili= ty of partitioning if you structure a warehouse in a traditional manner. = =A0Assuming a fact table partitioned by time, but with foreign keys to a ti= me dimension, it is now not possible to gain any advantage from the partiti= oning if selecting on columns in the time dimension.

"select * from fact_table f join time_dimension t = on f.time_id =3D t.time_id where t.quarter=3D3 and t.year =3D 2010" wi= ll scan all partitions of the fact table despite the fact that all of the r= ows would come from 3 partitions, assuming a partitioning schema that uses = one partition for each month. =A0

I use a time id that is calculable from the from the ti= mestamp so it doesn't need to be looked up, and partitioning on time_id= directly is easy enough to handle, but if I'm understanding the proble= m, it sounds like nothing short of computing the appropriate time ids befor= e issuing the query and then including a 'where f.time_id between x and= y' clause to the query will result in the partitions being correctly e= xcluded. =A0Is that what people are doing to solve this problem? =A0The alt= ernative is to leave a timestamp column in the fact table (something I tend= to do since it makes typing ad-hoc queries in psql much easier) and partit= ion on that column and then always include a where clause for that column t= hat is at least as large as the requested row range. =A0Both result in fair= ly ugly queries, though I can certainly see how I might structure my code t= o always build queries which adhere to this.

I'm just in the process of designing a star schema = for a project and was intending to use exactly the structure I described at= the top of the email. Is there a postgres best-practices for solving this = problem? There's no way I can get away without partitioning. =A0I'm= looking at a worst case table of 100,000 rows being written every 5 minute= s, 24x7 - 29 million rows per day, a billion rows per month - with most que= ries running over a single month or comparing same months from differing ye= ars and quarters - so a month based partitioning. =A0Normal case is closer = to 10K rows per 5 minutes.

Suggestions?

--sam
<= br>

--bcaec539639c6fc29d04a1ef7e88--