Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YXpt6-0000qq-Gj for pgsql-performance@arkaria.postgresql.org; Tue, 17 Mar 2015 11:42:20 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YXpt5-0005Gz-C6 for pgsql-performance@arkaria.postgresql.org; Tue, 17 Mar 2015 11:42:19 +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 1YXpt4-0005Gt-F4 for pgsql-performance@postgresql.org; Tue, 17 Mar 2015 11:42:18 +0000 Received: from mail-wi0-f174.google.com ([209.85.212.174]) by makus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1YXpt1-0005RT-3O for pgsql-performance@postgresql.org; Tue, 17 Mar 2015 11:42:16 +0000 Received: by wixw10 with SMTP id w10so45405685wix.0 for ; Tue, 17 Mar 2015 04:42:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:date:from:organization:user-agent :mime-version:to:cc:subject:references:in-reply-to:content-type :content-transfer-encoding; bh=Kj0vxB2eiYW37jPTbC0qV3+0Yi+Y5SKEzs2ODzZShvI=; b=CPwqxTsY5ybNo5wn5Txo5q9Q9/OUj1WaQra/KYeGEsbn/77YKb0+qltGplJvea6Tb3 nEF3JRAua+qvdmjypfgATFxTuuhKuPl9ef6qEyID5s/+5DCUCE6yicX3F3yCmapXsrGm DA9XjIVyfEiC0iVBFHndTUwAhi0qMIaXTexmQnwurLg5S4vbW+cywsmJkcN3C5C3Ml0I U6BY+aj4v8IRCTln65hvpDSV5SUDiI1jfVAqwDds3eg4myyY/DjCtySKf4TCn3TCtwlw lzjtPnL36RyUBQipUWwQg/0q2JwjSOIUx+DwP7L9R3g3RUoun7fyJeCBLDGd1N867Bjz GTXQ== X-Gm-Message-State: ALoCoQkXSDmr7YcHxNVl8bzs8F3C8KM8cLxuE4qyHk0zUE4AeOqRWacvjbbQg6G5iFmL9eGhPH6Ze2qG/YyQKx122FFDb4S4CxYcL8AKmvOIc7j7b/EIaLd6BpBvTZMzfj+93dLVSY170K4e3Habji2k1brolw1SDwRD2LP71rcXlbX/d5vnvr4z/SvOmr4TTP7YOukA+rU/ X-Received: by 10.180.76.148 with SMTP id k20mr25844826wiw.71.1426592532913; Tue, 17 Mar 2015 04:42:12 -0700 (PDT) Received: from [192.168.1.163] (ip-78-45-139-138.net.upcbroadband.cz. [78.45.139.138]) by mx.google.com with ESMTPSA id wc10sm145297wic.21.2015.03.17.04.42.11 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 17 Mar 2015 04:42:12 -0700 (PDT) Message-ID: <5508130E.8030202@2ndquadrant.com> Date: Tue, 17 Mar 2015 12:42:06 +0100 From: Tomas Vondra Organization: 2ndQuadrant User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: vjoshi@zetainteractive.com, Jim Nasby , Scott Marlowe , Varadharajan Mukundan CC: pgsql-performance@postgresql.org Subject: Re: Performance issues 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> In-Reply-To: Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.6 (--) 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 Hi, On 17.3.2015 08:41, Vivekanand Joshi wrote: > 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. Most of that cost comes from this: Seq Scan on s_f_promotion_history base (cost=0.00..283,333.66 rows=1 width=32) Filter: ((send_dt >= '2014-03-13 00:00:00'::timestamp without time zone) AND (send_dt <= '2015-03-14 00:00:00'::timestamp without time That's a bit weird, I guess. If you analyze this part of the query separately, i.e. EXPLAIN ANALYZE SELECT * FROM s_f_promotion_history WHERE (send_dt >= '2014-03-13 00:00:00') AND (send_dt <= '2015-03-14 00:00:00') what do you get? I suspect it's used in EXISTS, i.e. something like this: ... WHERE EXISTS (SELECT * FROM s_f_promotion_history WHERE ... send_dt conditions ... AND touchpoint_execution_id = s_f_touchpoint_execution_status_history_1.touchpoint_execution_id) and this is transformed into a nested loop join. If there's a misestimate, this may be quite expensive - try to create index on s_f_promotion_history (touchpoint_execution_id, send_date) regards -- Tomas Vondra http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance