Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jWhhH-0002G9-CW for pgsql-general@arkaria.postgresql.org; Thu, 07 May 2020 14:40:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jWhhG-000068-69 for pgsql-general@arkaria.postgresql.org; Thu, 07 May 2020 14:40:22 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jWhhF-00005o-Pd for pgsql-general@lists.postgresql.org; Thu, 07 May 2020 14:40:21 +0000 Received: from wout5-smtp.messagingengine.com ([64.147.123.21]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jWhhA-00025Z-Gv for pgsql-general@postgresql.org; Thu, 07 May 2020 14:40:19 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id ACFD47B8; Thu, 7 May 2020 10:40:13 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Thu, 07 May 2020 10:40:13 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=Q dDCSDJjXJOeXqltdle2UFozzFrbx4viwMq5eQxRgQM=; b=sz0NAA7LCBrVTf7AM Jd/PBd0VzgIORxV/dQ//UHb+zz3usRhHWuryyNWbnleIyv/lboTGD62MfoeQQ03D y8wkpKzay+ZpOAWJixjEEzM5+3I846fTz4P8ZzvonoC9TaDHSKCNngqqNqrzKZY9 rWXPsa69oAceb+q/qStCpatfcRUqbC3uIECc9rhS0nRCbPjhVMykoWeWF13kZ3SQ 34x0lTQGqKUyYadVUvKow6JKd/bHd8gr56XpiAjhTFQFp+PxDiMdwsb8v7wV4EsU eLsf197QVcla05vNtevX62oCpxRAN6iiT4H3XToG6d5xXoGv3k1/5DjHDX4gReg+ 6edFg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=QdDCSDJjXJOeXqltdle2UFozzFrbx4viwMq5eQxRg QM=; b=JNxxJ+0W7pFZqJrKWJ95n59N4vc4Hwqxuu7jkCPQ8fD/aa3trmBMhwwyj VaWBlmCaf1qIpkyjGa4eaf7tG86ju6+JKrtLtFpbaHjtU0uytdVjnReY88enWlk/ tXXfI6fLsYe7AgDKsuNrl88FteESuTCDGNigPSe2lw5SDH7OOSnyA9jLTH7EGVW7 7Iegr3aksGCqYKNn4fOj7PMbjnMTBP16Z2bPKJYs7sxVO3Fa2scX3oloAhSG6n/2 +KlU9qX6mUNE/3eW0PYkAUQu6TOzYYxBKbNBdD6s1lsy11yy09p/BvAJLn4xzkdn MFOvYqn6WUbTwFruVOzyBtUaut+PA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduhedrkedtgdejkecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhfhkffffgggjggtgfesthekre dttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhl rghvvghrsegrkhhlrghvvghrrdgtohhmqeenucggtffrrghtthgvrhhnpeevveefveefue egffekkefhgeeiueffgfetueejvdfghfeujeejhfekjeelgedtgeenucfkphepjeehrddu jedvrdehrddvfedunecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilh hfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Received: from [192.168.1.3] (75-172-5-231.tukw.qwest.net [75.172.5.231]) by mail.messagingengine.com (Postfix) with ESMTPA id 2E67E3280065; Thu, 7 May 2020 10:40:12 -0400 (EDT) Subject: Re: Explain plan changes - IN CLAUSE ( Passing direct values Vs INNER Query ) To: Amarendra Konda , pgsql-performance@lists.postgresql.org, pgsql-general@postgresql.org References: From: Adrian Klaver Message-ID: <92fe033c-0221-30e3-1143-de8b0c8d923b@aklaver.com> Date: Thu, 7 May 2020 07:40:11 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 5/7/20 4:19 AM, Amarendra Konda wrote: > Hi, > > PostgreSQL version : PostgreSQL 9.6.2 on x86_64-pc-linux-gnu, compiled > by gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9), 64-bit > > We have noticed huge difference interms of execution plan ( response > time) , When we pass the direct values  Vs  inner query to IN clause. > > High level details of the use case are as follows > > * As part of the SQL there are 2 tables named Process_instance > (master) and Process_activity ( child) > * Wanted to fetch TOP 50 rows from  Process_activity table for the > given values of the Process_instance. > * When we used Inner Join / Inner query ( query1)  between parent > table and child table , LIMIT is not really taking in to account. > Instead it is fetching more rows and columns that required, and > finally limiting the result It is doing what you told it to do which is SELECT all process_instance_i's for user_id='317079413683604' and app_id = '427380312000560' and then filtering further. I am going to guess that if you run the inner query alone you will find it returns ~23496 rows. You might have better results if you an actual join between process_activity and process_instance. Something like below(obviously not tested): SELECT pa.process_activity_id FROM process_activity pa JOIN process_instance pi ON pa.process_instance_id = pi.process_instance_id WHERE pa.app_id = '427380312000560' AND pa.created > '1970-01-01 00:00:00' AND pi.user_id = '317079413683604' ORDER BY pa.process_instance_id, pa.created LIMIT 50; The second query is not equivalent as you are not filtering on user_id and you are filtering on only three process_instance_id's. > * > > > *Query1* > > web_1=> EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS) SELECT > pa.process_activity_id  FROM process_activity pa WHERE pa.app_id = > '427380312000560' AND pa.created > '1970-01-01 00:00:00' AND > pa.process_instance_id in *_(SELECT pi.process_instance_id FROM > process_instance pi WHERE pi.user_id = '317079413683604' AND pi.app_id = > '427380312000560')_* ORDER BY pa.process_instance_id,pa.created limit 50; > > >                                                                 QUERY PLAN > > ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ >  Limit  (cost=1071.47..1071.55 rows=31 width=24) (actual > time=85.958..85.991 rows=50 loops=1) >    Output: pa.process_activity_id, pa.process_instance_id, pa.created >    Buffers: shared hit=43065 >    ->  Sort  (cost=1071.47..1071.55 rows=31 width=24) (actual > time=85.956..85.971 rows=50 loops=1) >          Output: pa.process_activity_id, pa.process_instance_id, pa.created >          Sort Key: pa.process_instance_id, pa.created >          Sort Method: top-N heapsort  Memory: 28kB >          Buffers: shared hit=43065 >          ->  Nested Loop  (cost=1.14..1070.70 rows=31 width=24) (actual > time=0.031..72.183 rows=46992 loops=1) >                Output: pa.process_activity_id, pa.process_instance_id, > pa.created >                Buffers: shared hit=43065 >                ->  Index Scan using fki_conv_konotor_user_user_id on > public.process_instance pi  (cost=0.43..2.66 rows=1 width=8) (actual > time=0.010..0.013 rows=2 loops=1) >                      Output: pi.process_instance_id >                      Index Cond: (pi.user_id = '317079413683604'::bigint) >                      Filter: (pi.app_id = '427380312000560'::bigint) >                      Buffers: shared hit=5 >                ->  Index Scan using > process_activity_process_instance_id_app_id_created_idx on > public.process_activity pa  (cost=0.70..1053.80 rows=1425 width=24) > (actual time=0.015..20.702 rows=*23496* loops=2) > * Output: pa.process_activity_id, pa.process_activity_type, > pa.voice_url, pa.process_activity_user_id, pa.app_id, > pa.process_instance_id, pa.alias, pa.read_by_user, pa.source, > pa.label_category_id, pa.label_id, pa.csat_response_id, > pa.process_activity_fragments, pa.created, pa.updated, pa.rule_id, pa.market > ing_reply_id, pa.delivered_at, pa.reply_fragments, pa.status_fragment, > pa.internal_meta, pa.interaction_id, pa.do_not_translate, > pa.should_translate, pa.in_reply_to* >                      Index Cond: ((pa.process_instance_id = > pi.process_instance_id) AND (pa.app_id = '427380312000560'::bigint) AND > (pa.created > '1970-01-01 00:00:00'::timestamp without time zone)) >                      Buffers: shared hit=43060 >  Planning time: 0.499 ms >  Execution time: 86.040 ms > (22 rows) > > *_Query 2_* > > web_1=>  EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS) SELECT > pa.process_activity_id AS m_process_activity_id FROM process_activity m > WHERE pa.app_id = '427380312000560' AND pa.created > '1970-01-01 > 00:00:00' AND pa.process_instance_id in > (*240117466018927,325820556706970,433008275197305*) ORDER BY > pa.process_instance_id,pa.created limit 50; > >                                    QUERY PLAN > --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- >  Limit  (cost=0.70..37.66 rows=50 width=24) (actual time=0.023..0.094 > rows=50 loops=1) >    Output: process_activity_id, process_instance_id, created >    Buffers: shared hit=50 >    ->  Index Scan using > process_activity_process_instance_id_app_id_created_idx on > public.process_activity pa  (cost=0.70..3124.97 rows=4226 width=24) > (actual time=0.022..0.079 *rows=50* loops=1) >          Output: process_activity_id, process_instance_id, created >          Index Cond: ((pa.process_instance_id = ANY > ('{140117466018927,225820556706970,233008275197305}'::bigint[])) AND > (pa.app_id = '427380312000560'::bigint) AND (pa.created > '1970-01-01 > 00:00:00'::timestamp without time zone)) >          Buffers: shared hit=50 >  Planning time: 0.167 ms >  Execution time: 0.137 ms > (9 rows) > > > Can someone explain > > * Why It is fetching more columns and more rows, incase of inner query ? > * Is there any option to really limit values with INNER JOIN, INNER > query ? If yes, can you please share information on this ? > > Thanks in advance for your time and suggestions. > > Regards, Amar -- Adrian Klaver adrian.klaver@aklaver.com