Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YXpLh-00086m-TP for pgsql-performance@arkaria.postgresql.org; Tue, 17 Mar 2015 11:07:50 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YXpLg-0000hk-Hf for pgsql-performance@arkaria.postgresql.org; Tue, 17 Mar 2015 11:07:48 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YXpLf-0000hY-4J for pgsql-performance@postgresql.org; Tue, 17 Mar 2015 11:07:47 +0000 Received: from mail-ob0-x233.google.com ([2607:f8b0:4003:c01::233]) by makus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1YXpLb-0004nm-6c for pgsql-performance@postgresql.org; Tue, 17 Mar 2015 11:07:44 +0000 Received: by obcxo2 with SMTP id xo2so4343290obc.0 for ; Tue, 17 Mar 2015 04:07:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zetainteractive.com; s=google; h=reply-to:from:references:in-reply-to:mime-version:thread-index:date :message-id:subject:to:cc:content-type; bh=M/mYCrrtgN1x7tpyGCvHuePsyMI3iKcNN2sA5sRMiNk=; b=HAR/wkEfnC6wd7wl0HDRzEDT9xv1Ffo/Jwrja3F6L7JPiB/YEH1ObYNuK/MRpIwJLN mb0rKbJA1cAl7NQivrLJ1GHq9q0Q9H9XfghuM6oAg4E4OEx3e5BZFyrotXQB2FIPf3sx UiG22CBlp5AJtv4NSBBGP7gN2b64lDKZ5a4mw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:reply-to:from:references:in-reply-to :mime-version:thread-index:date:message-id:subject:to:cc :content-type; bh=M/mYCrrtgN1x7tpyGCvHuePsyMI3iKcNN2sA5sRMiNk=; b=RiYbobT1CmNyUsj3ZyvC6ccAijaXuNWes6HCmRohpKc2LmVxRH3CLruy52bUAyCDba Hf0WDegawH9ZxM2GFyHudYD6D9GmRawcO8dkaj145Ki5sj+oWnEF5SBI48oV7bV+nhyu Cvq22Ari/XSfXOgX57ZJ8LqK2IgZswjfxdubSsh+UeaVLmbdMqbR3L3hJepexkOVpJjY BNAGzTPwUTKy6ljc6hSPr0EcRTUgyKBmsg8vxcw58f4NO5Iqp2EDI6cNp84sLHDXvsq6 txkqCVDlqBaDKrRVItyKyTOr/U7yqtDbwxBMwbdEgqNkrdwzSw2HD07hfTG6HQY2X7FA SeHg== X-Gm-Message-State: ALoCoQmOyldaBmsLm1nF5Bn5FdLzIaRXBvRWFOSjmg4k4k2DGyUnEImO7fesjqrnOGJdVtJNb4NT X-Received: by 10.202.183.213 with SMTP id h204mr7922871oif.27.1426590462411; Tue, 17 Mar 2015 04:07:42 -0700 (PDT) Reply-To: From: Vivekanand Joshi References: <84a6fe4be04c3ef0a714851977860ac8@mail.gmail.com> <160b8780d89add7f23c4eb9c3debb99d@mail.gmail.com> <55034A48.4040409@2ndquadrant.com> <849e75d9463938372dd4c516f5c5d89c@mail.gmail.com> <55037CEA.5050906@2ndquadrant.com> <55073272.20209@BlueTreble.com> <55074424.9050506@2ndquadrant.com> <55076FFB.9090105@BlueTreble.com> c5231342781287ff8cbc9ac1f20ff036@mail.gmail.com In-Reply-To: c5231342781287ff8cbc9ac1f20ff036@mail.gmail.com MIME-Version: 1.0 X-Mailer: Microsoft Outlook 15.0 thread-index: AQGXPdRf3VxuG24kJ8WwflsQcHnAgQJFdAGLAd7f+TAB3+I8LgHxOMQ4AZdEQFACZq94KAEoF2rjAunz58UBLhBnRQIZ3EmNAZo17EoBofTzdgDeIilznNdP4vCAADxKsA== Date: Tue, 17 Mar 2015 16:37:41 +0530 Message-ID: <61a7a27428e588411e8ac1817043205c@mail.gmail.com> Subject: Re: Performance issues To: vjoshi@zetainteractive.com, Jim Nasby , Tomas Vondra , Scott Marlowe , Varadharajan Mukundan Cc: pgsql-performance@postgresql.org Content-Type: text/plain; charset=UTF-8 X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org EXPLAIN ANALYZE didn't give result even after three hours. -----Original Message----- From: Vivekanand Joshi [mailto:vjoshi@zetainteractive.com] Sent: Tuesday, March 17, 2015 1:11 PM To: 'Jim Nasby'; 'Tomas Vondra'; 'Scott Marlowe'; 'Varadharajan Mukundan' Cc: 'pgsql-performance@postgresql.org' Subject: RE: [PERFORM] Performance issues Hi Guys, Next level of query is following: If this works, I guess 90% of the problem will be solved. SELECT COUNT(DISTINCT TARGET_ID) FROM S_V_F_PROMOTION_HISTORY_EMAIL PH INNER JOIN S_V_D_CAMPAIGN_HIERARCHY CH ON PH.TOUCHPOINT_EXECUTION_ID = CH.TOUCHPOINT_EXECUTION_ID WHERE 1=1 AND SEND_DT >= '2014-03-13' AND SEND_DT <= '2015-03-14' In this query, I am joining two views which were made earlier with CTEs. I have replaced the CTE's with subqueries. The view were giving me output in around 5-10 minutes and now I am getting the same result in around 3-4 seconds. But when I executed the query written above, I am again stuck. I am attaching the query plan as well the link. http://explain.depesz.com/s/REeu I can see most of the time is spending inside a nested loop and total costs comes out be cost=338203.81..338203.82. How to take care of this? I need to run this query in a report so I cannot create a table like select * from views and then join the table. If I do that I am getting the answer of whole big query in some 6-7 seconds. But that is not feasible. A report (Jasper can have only one single (big/small query). Let me know if you need any other information. Thanks a ton! Vivek -----Original Message----- From: Jim Nasby [mailto:Jim.Nasby@BlueTreble.com] Sent: Tuesday, March 17, 2015 5:36 AM To: Tomas Vondra; vjoshi@zetainteractive.com; Scott Marlowe; Varadharajan Mukundan Cc: pgsql-performance@postgresql.org Subject: Re: [PERFORM] Performance issues On 3/16/15 3:59 PM, Tomas Vondra wrote: > On 16.3.2015 20:43, Jim Nasby wrote: >> On 3/13/15 7:12 PM, Tomas Vondra wrote: >>> (4) I suspect many of the relations referenced in the views are not >>> actually needed in the query, i.e. the join is performed but >>> then it's just discarded because those columns are not used. >>> Try to simplify the views as much has possible - remove all the >>> tables that are not really necessary to run the query. If two >>> queries need different tables, maybe defining two views is >>> a better approach. >> >> A better alternative with multi-purpose views is to use an outer join >> instead of an inner join. With an outer join if you ultimately don't >> refer to any of the columns in a particular table Postgres will >> remove the table from the query completely. > > Really? Because a quick test suggests otherwise: > > db=# create table test_a (id int); > CREATE TABLE > db=# create table test_b (id int); > CREATE TABLE > db=# explain select test_a.* from test_a left join test_b using (id); > QUERY PLAN > ---------------------------------------------------------------------- > Merge Left Join (cost=359.57..860.00 rows=32512 width=4) > Merge Cond: (test_a.id = test_b.id) > -> Sort (cost=179.78..186.16 rows=2550 width=4) > Sort Key: test_a.id > -> Seq Scan on test_a (cost=0.00..35.50 rows=2550 width=4) > -> Sort (cost=179.78..186.16 rows=2550 width=4) > Sort Key: test_b.id > -> Seq Scan on test_b (cost=0.00..35.50 rows=2550 width=4) > (8 rows) > > Also, how would that work with duplicate rows in the referenced table? Right, I neglected to mention that the omitted table must also be unique on the join key: decibel@decina.attlocal=# create table a(a_id serial primary key); CREATE TABLE decibel@decina.attlocal=# create table b(a_id int); CREATE TABLE decibel@decina.attlocal=# explain analyze select a.* from a left join b using(a_id); QUERY PLAN -------------------------------------------------------------------------- --------------------------------- Hash Right Join (cost=67.38..137.94 rows=2550 width=4) (actual time=0.035..0.035 rows=0 loops=1) Hash Cond: (b.a_id = a.a_id) -> Seq Scan on b (cost=0.00..35.50 rows=2550 width=4) (never executed) -> Hash (cost=35.50..35.50 rows=2550 width=4) (actual time=0.002..0.002 rows=0 loops=1) Buckets: 4096 Batches: 1 Memory Usage: 32kB -> Seq Scan on a (cost=0.00..35.50 rows=2550 width=4) (actual time=0.001..0.001 rows=0 loops=1) Planning time: 0.380 ms Execution time: 0.086 ms (8 rows) decibel@decina.attlocal=# alter table b add primary key(a_id); ALTER TABLE decibel@decina.attlocal=# explain analyze select a.* from a left join b using(a_id); QUERY PLAN -------------------------------------------------------------------------- --------------------- Seq Scan on a (cost=0.00..35.50 rows=2550 width=4) (actual time=0.001..0.001 rows=0 loops=1) Planning time: 0.247 ms Execution time: 0.029 ms (3 rows) decibel@decina.attlocal=# alter table a drop constraint a_pkey; ALTER TABLE decibel@decina.attlocal=# explain analyze select a.* from a left join b using(a_id); QUERY PLAN -------------------------------------------------------------------------- --------------------- Seq Scan on a (cost=0.00..35.50 rows=2550 width=4) (actual time=0.001..0.001 rows=0 loops=1) Planning time: 0.098 ms Execution time: 0.011 ms (3 rows) -- Jim Nasby, Data Architect, Blue Treble Consulting Data in Trouble? Get it in Treble! http://BlueTreble.com -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance