Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id ECBE2632374 for ; Fri, 2 Jul 2010 12:28:56 -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 79615-02 for ; Fri, 2 Jul 2010 15:28:48 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from snt0-omc4-s42.snt0.hotmail.com (snt0-omc4-s42.snt0.hotmail.com [65.54.51.93]) by mail.postgresql.org (Postfix) with ESMTP id ED7016322F5 for ; Fri, 2 Jul 2010 12:28:47 -0300 (ADT) Received: from SNT129-W20 ([65.55.90.201]) by snt0-omc4-s42.snt0.hotmail.com with Microsoft SMTPSVC(6.0.3790.4675); Fri, 2 Jul 2010 08:28:45 -0700 Message-ID: Content-Type: multipart/alternative; boundary="_ae71eb8a-7d70-40ea-9eea-37cf5793c26d_" X-Originating-IP: [65.223.237.18] From: Ranga Gopalan To: Subject: Question about partitioned query behavior Date: Fri, 2 Jul 2010 15:28:45 +0000 Importance: Normal MIME-Version: 1.0 X-OriginalArrivalTime: 02 Jul 2010 15:28:45.0823 (UTC) FILETIME=[42CA48F0:01CB19FB] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.791 tagged_above=-10 required=5 tests=BAYES_50=0.8, HTML_MESSAGE=0.001, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201007/28 X-Sequence-Number: 39417 --_ae71eb8a-7d70-40ea-9eea-37cf5793c26d_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi=2C My question is regarding ORDER BY / LIMIT query behavior when using partiti= oning. I have a large table (about 100 columns=2C several million rows) partitione= d by a column called day (which is the date stored as yyyymmdd - say 201005= 02 for May 2nd 2010 etc.). Say the main table is called FACT_TABLE and eac= h child table is called FACT_TABLE_yyyymmdd (e.g. FACT_TABLE_20100502=2C FA= CT_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=2C compiled= by GCC gcc (GCC) 3.4.6 20060404 (Red Hat 3.4.6-10)=2C 64-bit The query pattern I am looking at is (I have tried to simplify the column n= ames 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=2C 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=3B QUERY P= LAN =20 =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 t= hings 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 - Th= is seems to be much less efficient than doing a descending scan on each par= tition 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=3B QUERY P= LAN =20 =20 ---------------------------------------------------------------------------= --------------------------------------------------------------------- --- Limit (cost=3D20000084948.01..20000084948.01 rows=3D100 width=3D41) -> Sort (cost=3D20000084948.01..20000084994.93 rows=3D1876986 width=3D= 41) Sort Key: public.FACT_TABLE.F2 -> Result (cost=3D10000000000.00..20000084230.64 rows=3D1876986 = width=3D41) -> Append (cost=3D10000000000.00..20000084230.64 rows=3D18= 76986 width=3D41) -> Seq Scan on FACT_TABLE (cost=3D10000000000.00..10= 000000010.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=2C Ranga =20 _________________________________________________________________ Hotmail is redefining busy with tools for the New Busy. Get more from your = inbox. http://www.windowslive.com/campaign/thenewbusy?ocid=3DPID28326::T:WLMTAGL:O= N:WL:en-US:WM_HMP:042010_2= --_ae71eb8a-7d70-40ea-9eea-37cf5793c26d_ Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi=2C

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

I have a large table (about 100 columns=2C sever= al million rows) partitioned by a column called day (which is the date stor= ed as yyyymmdd - say 20100502 for May 2nd 2010 etc.). Say the main table&nb= sp=3B is called FACT_TABLE and each child table is called FACT_TABLE_yyyymm= dd (e.g. FACT_TABLE_20100502=2C FACT_TABLE_20100503 etc.) and has an approp= riate CHECK constraint created on it to CHECK (day =3D yyyymmdd).

Po= stgres Version: =3B PostgreSQL 8.4.2 on x86_64-unknown-linux-gnu=2C com= piled by GCC gcc (GCC) 3.4.6 20060404 (Red Hat 3.4.6-10)=2C 64-bit

T= he query pattern I am looking at is (I have tried to simplify the column na= mes for readability):

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


This is what is happening:

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

# explain =3B sele= ct F1 from FACT_TABLE_20100502 where day =3D 20100502 order by F2 desc limi= t 100=3B
 =3B =3B =3B =3B =3B =3B =3B = =3B =3B =3B =3B =3B =3B =3B =3B =3B =3B=  =3B =3B =3B =3B =3B =3B =3B =3B =3B&nb= sp=3B =3B =3B =3B =3B =3B =3B =3B =3B = =3B =3B =3B =3B =3B =3B =3B =3B =3B =3B=  =3B =3B =3B =3B =3B =3B =3B =3B =3B&nb= sp=3B =3B =3B =3B =3B =3B =3B =3B =3B = =3B =3B =3B =3B QUERY PLAN =3B =3B =3B =3B = =3B =3B =3B =3B =3B =3B =3B =3B =3B =3B=  =3B =3B =3B =3B =3B =3B =3B =3B =3B&nb= sp=3B =3B =3B =3B =3B =3B =3B =3B =3B = =3B =3B =3B =3B =3B =3B =3B =3B =3B =3B=  =3B =3B =3B =3B =3B =3B =3B =3B =3B&nb= sp=3B =3B =3B =3B =3B =3B =3B =3B =3B = =3B =3B =3B =3B =3B
 =3B
----------------------= ---------------------------------------------------------------------------= -----------------------------------------------
--
 =3BLimit = =3B (cost=3D0.00..4.81 rows=3D100 width=3D41)
 =3B =3B ->=3B&n= bsp=3B Index Scan Backward using F2_20100502 on FACT_TABLE_20100502 =3B= (cost=3D0.00..90355.89 rows=3D1876985 width=3D41
)
 =3B =3B&= nbsp=3B =3B =3B =3B =3B =3B Filter: (day =3D 20100502)<= br>


BUT:

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

explain =3B select F1 fr= om FACT_TABLE where day =3D 20100502 order by F2 desc limit 100=3B
 = =3B =3B =3B =3B =3B =3B =3B =3B =3B =3B=  =3B =3B =3B =3B =3B =3B =3B =3B =3B&nb= sp=3B =3B =3B =3B =3B =3B =3B =3B =3B = =3B =3B =3B =3B =3B =3B =3B =3B =3B =3B=  =3B =3B =3B =3B =3B =3B =3B =3B =3B&nb= sp=3B =3B =3B =3B =3B =3B =3B =3B =3B = =3B =3B =3B =3B =3B =3B =3B =3B =3B =3B=  =3B QUERY PLAN =3B =3B =3B =3B =3B =3B =3B=  =3B =3B =3B =3B =3B =3B =3B =3B =3B&nb= sp=3B =3B =3B =3B =3B =3B =3B =3B =3B = =3B =3B =3B =3B =3B =3B =3B =3B =3B =3B=  =3B =3B =3B =3B =3B =3B =3B =3B =3B&nb= sp=3B =3B =3B =3B =3B =3B =3B =3B =3B = =3B =3B =3B =3B =3B =3B =3B =3B =3B =3B=  =3B =3B
 =3B =3B
---------------------------------= ---------------------------------------------------------------------------= ------------------------------------
---
 =3BLimit =3B (cost= =3D20000084948.01..20000084948.01 rows=3D100 width=3D41)
 =3B = =3B ->=3B =3B Sort =3B (cost=3D20000084948.01..20000084994.93 row= s=3D1876986 width=3D41)
 =3B =3B =3B =3B =3B =3B=  =3B =3B Sort Key: public.FACT_TABLE.F2
 =3B =3B =3B=  =3B =3B =3B =3B =3B ->=3B =3B Result =3B (co= st=3D10000000000.00..20000084230.64 rows=3D1876986 width=3D41)
 =3B&= nbsp=3B =3B =3B =3B =3B =3B =3B =3B =3B&nbs= p=3B =3B =3B =3B ->=3B =3B Append =3B (cost=3D1000000= 0000.00..20000084230.64 rows=3D1876986 width=3D41)
 =3B =3B = =3B =3B =3B =3B =3B =3B =3B =3B =3B =3B=  =3B =3B =3B =3B =3B =3B =3B =3B ->=3B&nb= sp=3B Seq Scan on FACT_TABLE =3B (cost=3D10000000000.00..10000000010.02= rows=3D1 width=3D186)
 =3B =3B =3B =3B =3B =3B&= nbsp=3B =3B =3B =3B =3B =3B =3B =3B =3B&nbs= p=3B =3B =3B =3B =3B =3B =3B =3B =3B = =3B =3B Filter: (day =3D 20100502)
 =3B =3B =3B =3B&= nbsp=3B =3B =3B =3B =3B =3B =3B =3B =3B&nbs= p=3B =3B =3B =3B =3B =3B =3B ->=3B =3B Seq Sc= an on FACT_TABLE_20100502 FACT_TABLE =3B (cost=3D10000000000.00..100000= 84220.62 rows=3D1876985 width=3D4
1)
 =3B =3B =3B =3B=  =3B =3B =3B =3B =3B =3B =3B =3B =3B&nb= sp=3B =3B =3B =3B =3B =3B =3B =3B =3B = =3B =3B =3B =3B Filter: (day =3D 20100502)
(9 rows)

<= br>Could anyone please explain why this is happening and what I can do to g= et the query to perform well even when querying from the parent table?
<= br>Thanks=2C

Ranga






Hotmai= l is redefining busy with tools for the New Busy. Get more from your inbox.= See how. = --_ae71eb8a-7d70-40ea-9eea-37cf5793c26d_--