Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eK5zT-0002Wz-0l for pgsql-general@arkaria.postgresql.org; Wed, 29 Nov 2017 17:17:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eK5zR-0002Uh-J7 for pgsql-general@arkaria.postgresql.org; Wed, 29 Nov 2017 17:17:41 +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.84_2) (envelope-from ) id 1eK5zR-0002UX-8l for pgsql-general@lists.postgresql.org; Wed, 29 Nov 2017 17:17:41 +0000 Received: from mx2.mailbox.org ([80.241.60.215]) by makus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eK5zO-00006b-4N for pgsql-general@postgresql.org; Wed, 29 Nov 2017 17:17:39 +0000 Received: from smtp1.mailbox.org (smtp1.mailbox.org [80.241.60.240]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx2.mailbox.org (Postfix) with ESMTPS id 61EB84C494; Wed, 29 Nov 2017 18:17:34 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by spamfilter03.heinlein-hosting.de (spamfilter03.heinlein-hosting.de [80.241.56.117]) (amavisd-new, port 10030) with ESMTP id 8kGGS_NSrORl; Wed, 29 Nov 2017 18:17:21 +0100 (CET) Message-ID: <1511975838.2309.12.camel@cybertec.at> Subject: Re: seq vs index scan in join query From: Laurenz Albe To: Emanuel Alvarez , pgsql-general@postgresql.org Date: Wed, 29 Nov 2017 18:17:18 +0100 In-Reply-To: References: Organization: Cybertec Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Emanuel Alvarez wrote: > the problematic query looks like this: > > SELECT keywords.strategy_id, results.position, results.created_at FROM results > JOIN keywords ON results.keyword_id = keywords.id > WHERE results.account_id = 1 > AND results.created_at >= '2017-10-25 00:00:00.000000' > AND results.created_at <= '2017-11-10 23:59:59.999999'; > > > as you can see in the query plan [1] a sequential scan is preferred. > as we understand it, this happens because the number of rows returned > from results is too large. if we reduce this number by either > selecting a smaller created_at range, or another account_id with fewer > keywords, the planner falls back to an index scan, confirming that the > number of rows returned from results has a direct influence in this > choice. > > on the other hand, if we disable sequential scans (SET enable_seqscan > = 0), we see than not only the query runs faster but the cost seems to > be lower, as seen in the query plan [2]. The optimizer is right here. Even though your second execution without sequential scans ran faster, it is worse. That is because the execution with the sequential scan touched 26492 + 80492 = 106984 blocks, while the second execution touched 311301 + 48510 = 359811 blocks, more than three times as many. The second execution was just lucky because most of these blocks were already cached, and it had to read only half as many blocks from disk. If you repeat the execution a couple of times, you should see that the execution using the sequential scans becomes faster. You can boost performance even more by increasing work_mem so that the hash can be created in memory. Yours, Laurenz Albe