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 1kHVAP-0002B7-Qa for pgsql-performance@arkaria.postgresql.org; Sun, 13 Sep 2020 16:47:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kHVAO-0000IT-LD for pgsql-performance@arkaria.postgresql.org; Sun, 13 Sep 2020 16:47:52 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kHVAO-0000IM-Dv for pgsql-performance@lists.postgresql.org; Sun, 13 Sep 2020 16:47:52 +0000 Received: from mail-lj1-x241.google.com ([2a00:1450:4864:20::241]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kHVAL-0003qo-QS for pgsql-performance@lists.postgresql.org; Sun, 13 Sep 2020 16:47:51 +0000 Received: by mail-lj1-x241.google.com with SMTP id a22so16514393ljp.13 for ; Sun, 13 Sep 2020 09:47:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=vNZncnGxnn3c4xHmCpl1gRz+SarAQjE65X6hS5iqij0=; b=buMkWd23A7dB8TFxdQOh1gmcQKgIqka261fZn3VroG196+fZUmCAgkE7npBHZu2RdD O4xhoTgWdt44nKo4z9siQgIqTU1Cu+FhNpt7p1xNdoyyWf88BF38ZJn1SuOg1EJvJcST XSG8mrFiYQEE5MFj7FtgDT5u/T0Mr0O3ip4NOqExIwHB73eZhYOwpLVQZumVCtC9qrHk gCz9OMGF/RB4QMYrdSxvAzLlOGhuQocD0ebvdeh1lOIdfNGawP7+n8lOuFbhibZMoYjx GSPLDqKpZmmSgih2ablqnnlW4RFCTI3C8aoWlzrmkEZeaVkJQtE1GVOubqtFoI3emZWU Q0dA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=vNZncnGxnn3c4xHmCpl1gRz+SarAQjE65X6hS5iqij0=; b=goOUGesVHMTfaBARhZogG395QUhLBOjs2oRNHqfhKq8fsAE75x07KXsh+fVb0Jowlk Kdl9aH2seekQzzb8jIo/+bpcCmM85bBIfC0hAsAvZOnumZZVh9vaWKRqlxWaJ/dK7Lua EPmyRoTxjuD7sssnDoigOjwDCL1RVeNhUUD9/3HUnRqf9zB2msNskzUdg7eB2z4TJ4m+ ZhZLAa2CPotOaZ3xM+u3wnmsVK+QPPlQWQcqT9M95hSfRQflFqa6sJXtR0tS1hTDEGAB pFOL22QquG2pA5kXGS6YJezbzw2kgJ/X7zL3Qy3T7ohAfaY/vv88hCosb7xxi4E1L9kV lMjA== X-Gm-Message-State: AOAM533yJ8I62P/NHdSfgepJDTnfiKAafgKhp0E/nKdi+JbLMoO1nffT kYb/iY83eQHy0UgvAi9wtDrsAQ== X-Google-Smtp-Source: ABdhPJwB55BusiauLBdMMtZcOtVQI7d/+KSyctFX4q4DrNptnFJEMUymj077OYMRfph82EOoXhKjZQ== X-Received: by 2002:a2e:b5a8:: with SMTP id f8mr3523046ljn.246.1600015667985; Sun, 13 Sep 2020 09:47:47 -0700 (PDT) Received: from localhost (37-247-27-67.customers.ownit.se. [37.247.27.67]) by smtp.gmail.com with ESMTPSA id 4sm2514788lfe.153.2020.09.13.09.47.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Sep 2020 09:47:46 -0700 (PDT) Date: Sun, 13 Sep 2020 18:47:45 +0200 From: Tomas Vondra To: "Gopisetty, Ramesh" Cc: "pgsql-performance@lists.postgresql.org" Subject: Re: Performance Issue (Not using Index when joining two tables). Message-ID: <20200913164745.dfnoeiz54wgmgckn@development> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Sun, Sep 13, 2020 at 02:58:15PM +0000, Gopisetty, Ramesh wrote: >Hi, > >Good Morning! > >Postgres Version : 11.6 (AWS Native Postgres/AWS Aurora tried on both flavours). > >When i'm joining two tables the primary index is not being used. While is use in clause with values then the index is being used. I have reindexed all the tables, run the auto vaccum as well. > > >pgwfc01q=> select count(*) from chr_simple_val; > count >------- > 13158 >(1 row) > >pgwfc01q=> select count(*) from chr_emp_position; > count >------- > 228 >(1 row) > > >The primary key for the table chr_Simple_val contains OID. Still not using the index. > >I'm sharing the explain plan over here.. > >pgwfc01q=> explain analyze select cep.HOME_DEPT_OID,ctc.oid,ctc.category,ctc.code from chr_emp_position cep inner join chr_Simple_Val ctc on ctc.oid=cep.HOME_DEPT_OID; > QUERY P >LAN >-------------------------------------------------------------------------------------------------------------------- >---------------------------------------------------------------------------------------------------------------- > Hash Join (cost=49299.91..51848.83 rows=651 width=42) (actual time=3512.692..3797.583 rows=228 loops=1) > Hash Cond: ((cep.home_dept_oid)::text = (ctc.oid)::text) > -> Seq Scan on chr_emp_position cep (cost=0.00..2437.77 rows=436 width=11) (actual time=44.713..329.435 rows=22 >8 loops=1) > Filter: ((("current_user"())::text <> ANY ('{wfnadmin,skipvpd}'::text[])) AND f_sel_policy_all(vpd_key, 'CH >R_EMP_POSITION'::character varying) AND f_sel_policy_prod_locale((cep.*)::character varying, prod_locale_code)) > Rows Removed by Filter: 3695 > -> Hash (cost=49176.40..49176.40 rows=9881 width=31) (actual time=3467.907..3467.908 rows=13158 loops=1) > Buckets: 16384 Batches: 1 Memory Usage: 1031kB > -> Seq Scan on chr_simple_val ctc (cost=0.00..49176.40 rows=9881 width=31) (actual time=2.191..3460.929 r >ows=13158 loops=1) > Filter: ((("current_user"())::text <> ANY ('{wfnadmin,skipvpd}'::text[])) AND f_sel_policy_ty_static( >vpd_key) AND f_sel_policy_prod_locale((ctc.*)::character varying, prod_locale_code)) > Rows Removed by Filter: 75771 > Planning Time: 0.297 ms > Execution Time: 3797.768 ms >(12 rows) > Most of the time (3460ms) is spent in the sequential scan on chr_simple_val, and the seqscan on chr_emp_position is taking ~330ms). Combined that's 3790ms out of 3797ms, so the join is pretty much irrelevant. Either the seqscans are causing a lot of I/O, or maybe the f_sel_* functions in the filter are expensive. Judging by how few rows are in the tables (not sure how large the tables are), I'd guess it's the latter ... Hard to say without knowing what the functions do etc. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services