Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 43734633190 for ; Fri, 2 Jul 2010 13:40:17 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 68903-08 for ; Fri, 2 Jul 2010 16:40:07 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.illumen.com (unknown [64.207.29.137]) by mail.postgresql.org (Postfix) with ESMTP id 6FAA7633180 for ; Fri, 2 Jul 2010 13:40:06 -0300 (ADT) X-MimeOLE: Produced By Microsoft Exchange V6.5 Content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----_=_NextPart_001_01CB1A05.38B370B0" Subject: Re: Question about partitioned query behavior Date: Fri, 2 Jul 2010 10:40:03 -0600 Message-ID: In-Reply-To: X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: [PERFORM] Question about partitioned query behavior Thread-Index: AcsZ+1c9r17b2woSTKmpzTYc+zUXEgACWd9w References: From: "Benjamin Krajmalnik" To: "Ranga Gopalan" , X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.594 tagged_above=-10 required=5 tests=BAYES_50=0.8, HTML_MESSAGE=0.001, RDNS_NONE=0.793 X-Spam-Level: * X-Archive-Number: 201007/30 X-Sequence-Number: 39419 This is a multi-part message in MIME format. ------_=_NextPart_001_01CB1A05.38B370B0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable In postgresql.conf, what are your settings for constraint_exclusion? There are 3 settings - on, off, or partition. Mine are set as follows: =20 constraint_exclusion =3D on # on, off, or partition =20 Under 8.4.4 I had it set to partition, but the behavior was not what I expected so I set it back to "on" and only the applicable partitions get processed. =20 From: pgsql-performance-owner@postgresql.org [mailto:pgsql-performance-owner@postgresql.org] On Behalf Of Ranga Gopalan Sent: Friday, July 02, 2010 9:29 AM To: pgsql-performance@postgresql.org Subject: [PERFORM] Question about partitioned query behavior =20 Hi, My question is regarding ORDER BY / LIMIT query behavior when using partitioning. I have a large table (about 100 columns, several million rows) partitioned by a column called day (which is the date stored as yyyymmdd - say 20100502 for May 2nd 2010 etc.). Say the main table is called FACT_TABLE and each child table is called FACT_TABLE_yyyymmdd (e.g. FACT_TABLE_20100502, FACT_TABLE_20100503 etc.) and has an appropriate CHECK constraint created on it to CHECK (day =3D yyyymmdd). Postgres Version: PostgreSQL 8.4.2 on x86_64-unknown-linux-gnu, compiled by GCC gcc (GCC) 3.4.6 20060404 (Red Hat 3.4.6-10), 64-bit The query pattern I am looking at is (I have tried to simplify the column names for readability): SELECT F1 from FACT_TABLE=20 where day >=3D 20100502 and day <=3D 20100507 # selecting for a week ORDER BY F2 desc LIMIT 100 This is what is happening: When I query from the specific day's (child) table, I get what I expect - a descending Index scan and good performance. # explain select F1 from FACT_TABLE_20100502 where day =3D 20100502 = order by F2 desc limit 100; =20 QUERY PLAN =20 ------------------------------------------------------------------------ ------------------------------------------------------------------------ -- Limit (cost=3D0.00..4.81 rows=3D100 width=3D41) -> Index Scan Backward using F2_20100502 on FACT_TABLE_20100502 (cost=3D0.00..90355.89 rows=3D1876985 width=3D41 ) Filter: (day =3D 20100502) BUT: When I do the same query against the parent table it is much slower - two things seem to happen - one is that the descending scan of the index is not done and secondly there seems to be a separate sort/limit at the end - i.e. all data from all partitions is retrieved and then sorted and limited - This seems to be much less efficient than doing a descending scan on each partition and limiting the results and then combining and reapplying the limit at the end. explain select F1 from FACT_TABLE where day =3D 20100502 order by F2 = desc limit 100; =20 QUERY PLAN =20 ------------------------------------------------------------------------ ------------------------------------------------------------------------ --- Limit (cost=3D20000084948.01..20000084948.01 rows=3D100 width=3D41) -> Sort (cost=3D20000084948.01..20000084994.93 rows=3D1876986 = width=3D41) Sort Key: public.FACT_TABLE.F2 -> Result (cost=3D10000000000.00..20000084230.64 = rows=3D1876986 width=3D41) -> Append (cost=3D10000000000.00..20000084230.64 rows=3D1876986 width=3D41) -> Seq Scan on FACT_TABLE (cost=3D10000000000.00..10000000010.02 rows=3D1 width=3D186) Filter: (day =3D 20100502) -> Seq Scan on FACT_TABLE_20100502 FACT_TABLE (cost=3D10000000000.00..10000084220.62 rows=3D1876985 width=3D4 1) Filter: (day =3D 20100502) (9 rows) Could anyone please explain why this is happening and what I can do to get the query to perform well even when querying from the parent table? Thanks, Ranga ________________________________ Hotmail is redefining busy with tools for the New Busy. Get more from your inbox. See how. =20 ------_=_NextPart_001_01CB1A05.38B370B0 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

In postgresql.conf, what are your settings for constraint_exclusion?

There are 3 settings – on, off, or = partition.

Mine are set as follows:

 

constraint_exclusion =3D = on            # = on, off, or partition

 

Under 8.4.4 I had it set to partition, but the behavior = was not what I expected so I set it back to “on” and only the = applicable partitions get processed.

 

From:= = pgsql-performance-owner@postgresql.org [mailto:pgsql-performance-owner@postgresql.org] On Behalf Of = Ranga Gopalan
Sent: Friday, July 02, 2010 9:29 AM
To: pgsql-performance@postgresql.org
Subject: [PERFORM] Question about partitioned query = behavior

 

Hi,

My question is regarding ORDER BY / LIMIT query behavior when using partitioning.

I have a large table (about 100 columns, several million rows) = partitioned by a column called day (which is the date stored as yyyymmdd - say 20100502 = for May 2nd 2010 etc.). Say the main table  is called FACT_TABLE and each = child table is called FACT_TABLE_yyyymmdd (e.g. FACT_TABLE_20100502, FACT_TABLE_20100503 etc.) and has an appropriate CHECK constraint = created on it to CHECK (day =3D yyyymmdd).

Postgres Version:  PostgreSQL 8.4.2 on x86_64-unknown-linux-gnu, = compiled by GCC gcc (GCC) 3.4.6 20060404 (Red Hat 3.4.6-10), 64-bit

The query pattern I am looking at is (I have tried to simplify the = column names for readability):

SELECT F1 from FACT_TABLE
where day >=3D 20100502 and day <=3D 20100507  # selecting = for a week
ORDER BY F2 desc
LIMIT 100


This is what is happening:

When I query from the specific day's (child) table, I get what I expect = - a descending Index scan and good performance.

# explain  select F1 from FACT_TABLE_20100502 where day =3D = 20100502 order by F2 desc limit 100;
            &= nbsp;           &n= bsp;           &nb= sp;           &nbs= p;            = ;       QUERY PLAN           &nb= sp;           &nbs= p;            = ;            =             &= nbsp;    
 
-------------------------------------------------------------------------= ----------------------------------------------------------------------- --
 Limit  (cost=3D0.00..4.81 rows=3D100 width=3D41)
   ->  Index Scan Backward using F2_20100502 on FACT_TABLE_20100502  (cost=3D0.00..90355.89 rows=3D1876985 = width=3D41
)
         Filter: (day =3D = 20100502)



BUT:

When I do the same query against the parent table it is much slower - = two things seem to happen - one is that the descending scan of the index is = not done and secondly there seems to be a separate sort/limit at the end - = i.e. all data from all partitions is retrieved and then sorted and limited - This = seems to be much less efficient than doing a descending scan on each partition = and limiting the results and then combining and reapplying the limit at the = end.

explain  select F1 from FACT_TABLE where day =3D 20100502 order by = F2 desc limit 100;
            &= nbsp;           &n= bsp;           &nb= sp;           &nbs= p;            = ;       QUERY PLAN           &nb= sp;           &nbs= p;            = ;            =             &= nbsp;    
  
-------------------------------------------------------------------------= ----------------------------------------------------------------------- ---
 Limit  (cost=3D20000084948.01..20000084948.01 rows=3D100 = width=3D41)
   ->  Sort  = (cost=3D20000084948.01..20000084994.93 rows=3D1876986 width=3D41)
         Sort Key: = public.FACT_TABLE.F2
         ->  = Result  (cost=3D10000000000.00..20000084230.64 rows=3D1876986 width=3D41)
            &= nbsp;  ->  Append  (cost=3D10000000000.00..20000084230.64 = rows=3D1876986 width=3D41)
            &= nbsp;        ->  Seq Scan on FACT_TABLE  = (cost=3D10000000000.00..10000000010.02 rows=3D1 width=3D186)
            &= nbsp;           &n= bsp;  Filter: (day =3D 20100502)
            &= nbsp;        ->  Seq Scan on FACT_TABLE_20100502 FACT_TABLE  (cost=3D10000000000.00..10000084220.62 rows=3D1876985 width=3D4
1)
            &= nbsp;           &n= bsp;  Filter: (day =3D 20100502)
(9 rows)


Could anyone please explain why this is happening and what I can do to = get the query to perform well even when querying from the parent table?

Thanks,

Ranga





Hotmail is redefining busy with tools for the New Busy. Get more from your = inbox. See how.

------_=_NextPart_001_01CB1A05.38B370B0--