Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f8vdj-0008Gx-R7 for pgsql-performance@arkaria.postgresql.org; Wed, 18 Apr 2018 22:33:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f8vdi-0003w4-EJ for pgsql-performance@arkaria.postgresql.org; Wed, 18 Apr 2018 22:33:22 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f8vdi-0003vv-3m for pgsql-performance@lists.postgresql.org; Wed, 18 Apr 2018 22:33:22 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f8vdf-00087v-PG for pgsql-performance@lists.postgresql.org; Wed, 18 Apr 2018 22:33:20 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w3IMXICP024998; Wed, 18 Apr 2018 18:33:18 -0400 From: Tom Lane To: Vitaliy Garnashevich cc: "pgsql-performance@lists.postgresql.org" Subject: Re: SeqScan vs. IndexScan In-reply-to: <439201cf-0a22-8b48-34bd-f311108acd0f@gmail.com> References: <439201cf-0a22-8b48-34bd-f311108acd0f@gmail.com> Comments: In-reply-to Vitaliy Garnashevich message dated "Thu, 19 Apr 2018 01:14:48 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <24996.1524090798.1@sss.pgh.pa.us> Date: Wed, 18 Apr 2018 18:33:18 -0400 Message-ID: <24997.1524090798@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Vitaliy Garnashevich writes: > I'm running the same query with "set enable_seqscan = on;" and "set > enable_seqscan = off;": > ... > Why optimizer is choosing SeqScan (on cmn_user) in the first query, > instead of an IndexScan, despite of SeqScan being more costly? Because it cares about the total plan cost, not the cost of any one sub-node. In this case, the total costs at the join level are fuzzily the same, but the indexscan-based join has worse estimated startup cost, so it prefers the first choice. The real problem here is the discrepancy between estimate and reality for the number of rows out of the sys_user scan; because of that, you're going to get garbage choices at the join level no matter what :-(. You should look into what's causing that misestimate and whether you can reduce the error, perhaps by providing better stats or reformulating the filter conditions in a way the optimizer understands better. regards, tom lane