X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id C5EB7D1CF73 for ; Wed, 2 Jun 2004 05:49:01 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 29349-08 for ; Wed, 2 Jun 2004 08:48:53 +0000 (GMT) Received: from trade-india.com (unknown [61.16.154.82]) by svr1.postgresql.org (Postfix) with SMTP id 65D73D1CF05 for ; Wed, 2 Jun 2004 05:48:45 -0300 (ADT) Received: (qmail 23768 invoked from network); 2 Jun 2004 08:35:28 -0000 Received: from valium.trade-india-local.com (HELO trade-india.com) (192.168.0.72) by system66.trade-india-local.com with SMTP; 2 Jun 2004 08:35:28 -0000 Message-ID: <40BD94BC.5030903@trade-india.com> Date: Wed, 02 Jun 2004 14:20:04 +0530 From: Rajesh Kumar Mallah Organization: Infocom Network Limited User-Agent: Mozilla Thunderbird 0.5 (X11/20040208) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Tom Lane Cc: pgsql-sql@postgresql.org Subject: Re: Query becoming slower on adding a primary key References: <33303.192.168.0.100.1086117670.squirrel@system67.trade-india-local.com> <16609.1086125809@sss.pgh.pa.us> In-Reply-To: <16609.1086125809@sss.pgh.pa.us> Content-Type: multipart/alternative; boundary="------------030404000401060109030308" X-Virus-Scanned: by amavisd-new at hub.org X-Archive-Number: 200406/9 X-Sequence-Number: 17954 This is a multi-part message in MIME format. --------------030404000401060109030308 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Tom Lane wrote: >mallah@trade-india.com writes: > > >>tradein_clients=# explain analyze select email_id ,email ,contact from >>t_a a join email_source f using(email_id) join email_subscriptions h >>using(email_id) where 1=1 and f.source_id =1 and h.sub_id = 3 ; >> >> > > > >>Runs for Ever. >> >> > >So what does plain explain say about it? > > Oops sorry that was a valuable info i left. (sorry for delay too) tradein_clients=# explain select email_id ,email ,contact from t_a a join email_source f using(email_id) join email_subscriptions h using(email_id) where 1=1 and f.source_id =1 and h.sub_id = 3 ; +-----------------------------------------------------------------------------------------+ | QUERY PLAN | +-----------------------------------------------------------------------------------------+ | Hash Join (cost=133741.48..224746.39 rows=328814 width=40) | | Hash Cond: ("outer".email_id = "inner".email_id) | | -> Seq Scan on email_subscriptions h (cost=0.00..70329.54 rows=749735 width=4) | | Filter: (sub_id = 3) | | -> Hash (cost=130230.99..130230.99 rows=324994 width=44) | | -> Hash Join (cost=26878.00..130230.99 rows=324994 width=44) | | Hash Cond: ("outer".email_id = "inner".email_id) | | -> Seq Scan on email_source f (cost=0.00..26159.21 rows=324994 width=4) | | Filter: (source_id = 1) | | -> Hash (cost=18626.80..18626.80 rows=800080 width=40) | | -> Seq Scan on t_a a (cost=0.00..18626.80 rows=800080 width=40) | +-----------------------------------------------------------------------------------------+ (11 rows) Time: 452.417 ms tradein_clients=# ALTER TABLE t_a add primary key(email_id); NOTICE: ALTER TABLE / ADD PRIMARY KEY will create implicit index "t_a_pkey" for table "t_a" ALTER TABLE Time: 7923.230 ms tradein_clients=# explain select email_id ,email ,contact from t_a a join email_source f using(email_id) join email_subscriptions h using(email_id) where 1=1 and f.source_id =1 and h.sub_id = 3 ; +-------------------------------------------------------------------------------------------------------------------+ | QUERY PLAN | +-------------------------------------------------------------------------------------------------------------------+ | Hash Join (cost=106819.76..197824.68 rows=328814 width=40) | | Hash Cond: ("outer".email_id = "inner".email_id) | | -> Seq Scan on email_subscriptions h (cost=0.00..70329.54 rows=749735 width=4) | | Filter: (sub_id = 3) | | -> Hash (cost=103309.28..103309.28 rows=324994 width=44) | | -> Merge Join (cost=0.00..103309.28 rows=324994 width=44) | | Merge Cond: ("outer".email_id = "inner".email_id) | | -> Index Scan using t_a_pkey on t_a a (cost=0.00..44689.59 rows=800080 width=40) | | -> Index Scan using email_source_pkey on email_source f (cost=0.00..52602.59 rows=324994 width=4) | | Filter: (source_id = 1) | +-------------------------------------------------------------------------------------------------------------------+ (10 rows) Time: 2436.551 ms tradein_clients=# Regds Mallah. > regards, tom lane > > > --------------030404000401060109030308 Content-Type: text/html; charset=us-ascii Content-Transfer-Encoding: 7bit
Tom Lane wrote:
mallah@trade-india.com writes:
  
tradein_clients=# explain analyze select  email_id ,email ,contact from
t_a a join email_source f using(email_id) join email_subscriptions h
using(email_id) where 1=1 and f.source_id =1 and h.sub_id = 3  ;
    

  
Runs for Ever.
    

So what does plain explain say about it?
  
Oops sorry that was a valuable info i left. (sorry for delay too)

tradein_clients=# explain  select  email_id ,email ,contact from t_a a join email_source f using(email_id) join email_subscriptions h
using(email_id) where 1=1 and f.source_id =1 and h.sub_id = 3  ;
+-----------------------------------------------------------------------------------------+
|                                       QUERY PLAN                                        |
+-----------------------------------------------------------------------------------------+
| Hash Join  (cost=133741.48..224746.39 rows=328814 width=40)                             |
|   Hash Cond: ("outer".email_id = "inner".email_id)                                      |
|   ->  Seq Scan on email_subscriptions h  (cost=0.00..70329.54 rows=749735 width=4)      |
|         Filter: (sub_id = 3)                                                            |
|   ->  Hash  (cost=130230.99..130230.99 rows=324994 width=44)                            |
|         ->  Hash Join  (cost=26878.00..130230.99 rows=324994 width=44)                  |
|               Hash Cond: ("outer".email_id = "inner".email_id)                          |
|               ->  Seq Scan on email_source f  (cost=0.00..26159.21 rows=324994 width=4) |
|                     Filter: (source_id = 1)                                             |
|               ->  Hash  (cost=18626.80..18626.80 rows=800080 width=40)                  |
|                     ->  Seq Scan on t_a a  (cost=0.00..18626.80 rows=800080 width=40)   |
+-----------------------------------------------------------------------------------------+
(11 rows)

Time: 452.417 ms
tradein_clients=# ALTER TABLE t_a add primary key(email_id);
NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "t_a_pkey" for table "t_a"
ALTER TABLE
Time: 7923.230 ms
tradein_clients=# explain  select  email_id ,email ,contact from t_a a join email_source f using(email_id) join email_subscriptions
h using(email_id) where 1=1 and f.source_id =1 and h.sub_id = 3  ;
+-------------------------------------------------------------------------------------------------------------------+
|                                                    QUERY PLAN                                                     |
+-------------------------------------------------------------------------------------------------------------------+
| Hash Join  (cost=106819.76..197824.68 rows=328814 width=40)                                                       |
|   Hash Cond: ("outer".email_id = "inner".email_id)                                                                |
|   ->  Seq Scan on email_subscriptions h  (cost=0.00..70329.54 rows=749735 width=4)                                |
|         Filter: (sub_id = 3)                                                                                      |
|   ->  Hash  (cost=103309.28..103309.28 rows=324994 width=44)                                                      |
|         ->  Merge Join  (cost=0.00..103309.28 rows=324994 width=44)                                               |
|               Merge Cond: ("outer".email_id = "inner".email_id)                                                   |
|               ->  Index Scan using t_a_pkey on t_a a  (cost=0.00..44689.59 rows=800080 width=40)                  |
|               ->  Index Scan using email_source_pkey on email_source f  (cost=0.00..52602.59 rows=324994 width=4) |
|                     Filter: (source_id = 1)                                                                       |
+-------------------------------------------------------------------------------------------------------------------+
(10 rows)

Time: 2436.551 ms
tradein_clients=#




Regds
Mallah.


			regards, tom lane

  

--------------030404000401060109030308--