Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 2145337C683 for ; Wed, 1 Oct 2008 07:34:43 -0300 (ADT) Received: from postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 55219-01 for ; Wed, 1 Oct 2008 07:34:35 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from ti-out-0910.google.com (ti-out-0910.google.com [209.85.142.189]) by postgresql.org (Postfix) with ESMTP id AE58737C17D for ; Wed, 1 Oct 2008 07:34:33 -0300 (ADT) Received: by ti-out-0910.google.com with SMTP id y8so219460tia.5 for ; Wed, 01 Oct 2008 03:34:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:mime-version:content-type; bh=OmyUyciP1zraZXvPl24GGtoQRqTTztAql26HzWfFWK8=; b=Akr++s458Pbl1Xs2TzGSlcglO7EpNNsFbw3WMTcRUmJTsMLHxj8i1fbcIsx+6S46T6 Nteg+PBojC8DURvRHZNWJ19bRV47qyb5TWskgn524qTYtJ4a7HEmAFvTBXG+1bAFHcF5 JTPQU5Pm7D0z9/wLH8STgltN3Zwu4MbOxh1UY= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:mime-version:content-type; b=mIo6b/AvCRKQdUAwNCC+gcDUyqE0OQAIYYWhs3Eps7a10D2NdWPpwThrunl8BOE1dg DgzVN2NPcVKhFOHekJ4BHwGLGBBafigUyMKUlwj3Lmms7+W4YkxDqbNXKpUS9cS/RR4a 2lwpa+gxU+tGNXm0DLIX2HFtz8Jnqt5t1oDpA= Received: by 10.110.20.15 with SMTP id 15mr11575225tit.48.1222857269044; Wed, 01 Oct 2008 03:34:29 -0700 (PDT) Received: by 10.110.11.15 with HTTP; Wed, 1 Oct 2008 03:34:28 -0700 (PDT) Message-ID: <7e4ba9550810010334q1ed0e93cn9e84f230a1028a2f@mail.gmail.com> Date: Wed, 1 Oct 2008 16:04:28 +0530 From: "Gauri Kanekar" To: pgsql-performance@postgresql.org Subject: Confusing Query Performance MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_11221_19520916.1222857269036" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=0 required=5 tests=HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 200810/1 X-Sequence-Number: 31582 ------=_Part_11221_19520916.1222857269036 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi, We have a table called "table1" which contains around 638725448 records. We created a subset of this table and named it as "new_table1" which has around 120107519 records. "new_table1" is 18% of the the whole "table1". If we fire the below queries we are not finding any drastic performance gain. Query 1 : SELECT SUM(table1.idlv), SUM(table1.cdlv) FROM table1, table2 CROSS JOIN table3 WHERE table1.dk = table2.k AND table2.dt BETWEEN '2008.08.01' AND '2008.08.20' AND table1.nk = table3.k AND table3.id = 999 ; Time taken : 9967.051 ms 9980.021 ms QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Aggregate (cost=647373.04..647373.05 rows=1 width=16) (actual time=9918.010..9918.010 rows=1 loops=1) -> Nested Loop (cost=186.26..647160.32 rows=42543 width=16) (actual time=655.832..6622.011 rows=5120582 loops=1) -> Nested Loop (cost=0.00..17.42 rows=30 width=8) (actual time=0.024..0.164 rows=31 loops=1) -> Index Scan using ridx on table3 (cost=0.00..8.27 rows=1 width=4) (actual time=0.012..0.014 rows=1 loops=1) Index Cond: (id = 999) -> Index Scan using rdtidx on table2 (cost=0.00..8.85 rows=30 width=4) (actual time=0.008..0.110 rows=31 loops=1) Index Cond: ((table2.dt >= '2008-08-01 00:00:00'::timestamp without time zone) AND (table2.dt <= '2008-08-20 00:00:00'::timestamp without time zone)) -> Bitmap Heap Scan on table1 (cost=186.26..21489.55 rows=5459 width=24) (actual time=57.053..170.657 rows=165180 loops=31) Recheck Cond: ((table1.nk = table3.k) AND (table1.dk = table2.k)) -> Bitmap Index Scan on rndtidx (cost=0.00..184.89 rows=5459 width=0) (actual time=47.855..47.855 rows=165180 loops=31) Index Cond: ((table1.nk = table3.k) AND (table1.dk = table2.k)) Total runtime: 9918.118 ms (12 rows) Time: 9967.051 ms Query 2 : SELECT SUM(new_table1.idlv) , SUM(new_table1.cdlv) FROM new_table1, table2 CROSS JOIN table3 WHERE new_table1.dk = table2.k AND table2.dt BETWEEN '2008.08.01' AND '2008.08.20' AND new_table1.nk = table3.k AND table3.id = 999 ; Time taken : 8225.308 ms 8500.728 ms QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Aggregate (cost=414372.59..414372.60 rows=1 width=16) (actual time=8224.300..8224.300 rows=1 loops=1) -> Nested Loop (cost=0.00..414246.81 rows=25155 width=16) (actual time=19.578..4922.680 rows=5120582 loops=1) -> Nested Loop (cost=0.00..17.42 rows=30 width=8) (actual time=0.034..0.125 rows=31 loops=1) -> Index Scan using ridx on table3 (cost=0.00..8.27 rows=1 width=4) (actual time=0.020..0.022 rows=1 loops=1) Index Cond: (id = 999) -> Index Scan using rdtidx on table2 (cost=0.00..8.85 rows=30 width=4) (actual time=0.010..0.064 rows=31 loops=1) Index Cond: ((table2.dt >= '2008-08-01 00:00:00'::timestamp without time zone) AND (table2.dt <= '2008-08-20 00:00:00'::timestamp without time zone)) -> Index Scan using rndtidx on new_table1 (cost=0.00..13685.26 rows=8159 width=24) (actual time=0.648..117.415 rows=165180 loops=31) Index Cond: ((new_table1.nk = table3.k) AND (new_table1.dk = table2.k)) Total runtime: 8224.386 ms (10 rows) Time: 8225.308 ms We have set join_collapse_limit = 8, from_collapse_limit = 1. -- Regards Gauri ------=_Part_11221_19520916.1222857269036 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline
Hi,

We have a table called "table1" which contains around 638725448 records.
We created a subset of this table and named it as "new_table1" which has around 120107519 records.

"new_table1" is 18% of the the whole "table1".

If we fire the below queries we are not finding any drastic performance gain.


Query 1 :
SELECT SUM(table1.idlv), SUM(table1.cdlv)
       FROM table1, table2 CROSS JOIN table3
       WHERE table1.dk = table2.k 
             AND table2.dt BETWEEN '2008.08.01' AND '2008.08.20'
             AND table1.nk = table3.k
             AND table3.id = 999 ;

Time taken :
9967.051 ms
9980.021 ms


                                                                                       QUERY PLAN                           
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=647373.04..647373.05 rows=1 width=16) (actual time=9918.010..9918.010 rows=1 loops=1)
   ->  Nested Loop  (cost=186.26..647160.32 rows=42543 width=16) (actual time=655.832..6622.011 rows=5120582 loops=1)
         ->  Nested Loop  (cost=0.00..17.42 rows=30 width=8) (actual time=0.024..0.164 rows=31 loops=1)
               ->  Index Scan using ridx on table3  (cost=0.00..8.27 rows=1 width=4) (actual time=0.012..0.014 rows=1 loops=1)
                     Index Cond: (id = 999)
               ->  Index Scan using rdtidx on table2  (cost=0.00..8.85 rows=30 width=4) (actual time=0.008..0.110 rows=31 loops=1)
                     Index Cond: ((table2.dt >= '2008-08-01 00:00:00'::timestamp without time zone) AND (table2.dt <= '2008-08-20 00:00:00'::timestamp without time zone))
         ->  Bitmap Heap Scan on table1  (cost=186.26..21489.55 rows=5459 width=24) (actual time=57.053..170.657 rows=165180 loops=31)
               Recheck Cond: ((table1.nk = table3.k) AND (table1.dk = table2.k))
               ->  Bitmap Index Scan on rndtidx  (cost=0.00..184.89 rows=5459 width=0) (actual time=47.855..47.855 rows=165180 loops=31)
                     Index Cond: ((table1.nk = table3.k) AND (table1.dk = table2.k))
 Total runtime: 9918.118 ms
(12 rows)

Time: 9967.051 ms



Query 2 :

SELECT SUM(new_table1.idlv) , SUM(new_table1.cdlv)
       FROM new_table1, table2 CROSS JOIN table3
       WHERE new_table1.dk = table2.k 
             AND table2.dt BETWEEN '2008.08.01' AND '2008.08.20'
             AND new_table1.nk = table3.k
             AND table3.id = 999 ;

Time taken :
8225.308 ms
8500.728 ms


                                                                                       QUERY PLAN                           
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=414372.59..414372.60 rows=1 width=16) (actual time=8224.300..8224.300 rows=1 loops=1)
   ->  Nested Loop  (cost=0.00..414246.81 rows=25155 width=16) (actual time=19.578..4922.680 rows=5120582 loops=1)
         ->  Nested Loop  (cost=0.00..17.42 rows=30 width=8) (actual time=0.034..0.125 rows=31 loops=1)
               ->  Index Scan using ridx on table3  (cost=0.00..8.27 rows=1 width=4) (actual time=0.020..0.022 rows=1 loops=1)
                     Index Cond: (id = 999)
               ->  Index Scan using rdtidx on table2  (cost=0.00..8.85 rows=30 width=4) (actual time=0.010..0.064 rows=31 loops=1)
                     Index Cond: ((table2.dt >= '2008-08-01 00:00:00'::timestamp without time zone) AND (table2.dt <= '2008-08-20 00:00:00'::timestamp without time zone))
         ->  Index Scan using rndtidx on new_table1  (cost=0.00..13685.26 rows=8159 width=24) (actual time=0.648..117.415 rows=165180 loops=31)
               Index Cond: ((new_table1.nk = table3.k) AND (new_table1.dk = table2.k))
 Total runtime: 8224.386 ms
(10 rows)

Time: 8225.308 ms

We have set join_collapse_limit = 8, from_collapse_limit = 1.

--
Regards
Gauri
------=_Part_11221_19520916.1222857269036--