Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eHWSm-00007U-0J for pgsql-performance@arkaria.postgresql.org; Wed, 22 Nov 2017 14:57:20 +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 1eHWSl-0004GN-8h for pgsql-performance@arkaria.postgresql.org; Wed, 22 Nov 2017 14:57:19 +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 1eHWSk-0004GD-Sd for pgsql-performance@lists.postgresql.org; Wed, 22 Nov 2017 14:57:19 +0000 Received: from mail-io0-x229.google.com ([2607:f8b0:4001:c06::229]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eHWSh-0000W1-Mt for pgsql-performance@lists.postgresql.org; Wed, 22 Nov 2017 14:57:17 +0000 Received: by mail-io0-x229.google.com with SMTP id w127so23471560iow.11 for ; Wed, 22 Nov 2017 06:57:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=I26RN2K5uAYP4sryDxiWpgywlPtpZLjcbA42MFAMq94=; b=zIqBndZfM6EhPPsFLzNMtnreo4ZC9dj+NH5aYNU7pSq+dIpNm2twfiF/Td/snb9r2H dQ1OV3DvKhhYCQDbW+WILvO0Ybj+trlLlKvnx0pCAqcr6sT6Hbjh2JRu5F1dc2FUNwcD D0e4jUbQfdsEKZSg3mFgxpjZPbHCvKTye4ZtSSWSS0rHqIlLea/xa1alu1pulm48h2J+ ms4PxR29MsSaGqtDvrz6oUB77N5j7HXpJJzzYXQL9Ms2+4beXXATaNmQ+wL6OAlJfJfp /3cL/BN3Km37zO9z5QXRa1yJzJp9snqojPTp9neNY+1YNC5VY+npEvSeYrmaWzCREQNG d0vA== 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:content-transfer-encoding :in-reply-to:user-agent; bh=I26RN2K5uAYP4sryDxiWpgywlPtpZLjcbA42MFAMq94=; b=fXsVIyBaSORodb3Dtd1qn44E/eiHjhtbhoZ8o8JU+o2o78gnhQ9pNKh87VXR4wNYI2 e2+EhYMPyhVhb5quytIxzEkHUaRNf2J/MOUhBd12GcqJxiwIf6jeKva/6uBwnDjlzPRp B+SwOGZve929/+ktQMXcFTV/S6luA+dQPvON5eOAg1IMRNHosepOFIgkeaAYAaySYr5O pvV+sakhgaI3NIpsaq/VEClkiUvWHOlaZRw7sOlMuPaQYaB/ftLLJlDK5jYZL9YbjpQH M0JudJ7CHJyURSwXRY1YHsrNx+SUCIJzpqWZDooMPIovlB93mZvv6xjrfkuJ/uHc2wyf xl1Q== X-Gm-Message-State: AJaThX7mix71azytd3ANPG6Q7ADbv0lE09y/St8tE0w6JYHfsupShYwy e6qH7OtV2soZXbG+AAxe5PdTnCKyzIE= X-Google-Smtp-Source: AGs4zMaGwXDl/t5mhnv2soUYFOAF+0kmpUpkmKpNounxmUElo7oVAjl4y8ZhJBS+a7ZUx1sjJjF/Xw== X-Received: by 10.107.56.4 with SMTP id f4mr13498369ioa.71.1511362634701; Wed, 22 Nov 2017 06:57:14 -0800 (PST) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id z95sm2149501ita.31.2017.11.22.06.57.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 22 Nov 2017 06:57:14 -0800 (PST) Received: by pryzbyj (Postfix, from userid 1000) id 73A43800FE7; Wed, 22 Nov 2017 08:57:13 -0600 (CST) Date: Wed, 22 Nov 2017 08:57:13 -0600 From: Justin Pryzby To: Artur =?utf-8?B?WmFqxIVj?= Cc: pgsql-performance@lists.postgresql.org Subject: Re: Bad estimates (DEFAULT_UNK_SEL) Message-ID: <20171122145713.GU5668@telsasoft.com> References: <03b401d3639e$5d7493a0$185dbae0$@ang.com.pl> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <03b401d3639e$5d7493a0$185dbae0$@ang.com.pl> User-Agent: Mutt/1.5.23 (2014-03-12) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Wed, Nov 22, 2017 at 03:29:54PM +0100, Artur ZajÄ…c wrote: > CREATE TABLE xyz AS SELECT generate_series(1,10000000,1) AS gs; > > db=# explain analyze select * from xyz where gs&1=1; > Seq Scan on xyz (cost=0.00..260815.38 rows=68920 width=4) (actual time=0.044..2959.728 rows=5000000 loops=1) ... > newrr=# explain analyze select * from xyz where gs&1=1 and gs&2=2 and gs&4=4; > Seq Scan on xyz (cost=0.00..398655.62 rows=2 width=4) (actual time=0.052..3329.422 rows=1250000 loops=1) > I noticed that each additional clause reduces the number about 200 times and > define DEFAULT_NUM_DISTINCT is responsible for this behaviur. I think it's actually: src/include/utils/selfuncs.h-/* default selectivity estimate for boolean and null test nodes */ src/include/utils/selfuncs.h-#define DEFAULT_UNK_SEL 0.005 ..which is 1/200. Note, you can do this, which helps a bit by collecting stats for the index expr: postgres=# CREATE INDEX ON xyz((gs&1)); postgres=# ANALYZE xyz; postgres=# explain analyze SELECT * FROM xyz WHERE gs&1=1 AND gs&2=2 AND gs&4=4; Bitmap Heap Scan on xyz (cost=91643.59..259941.99 rows=124 width=4) (actual time=472.376..2294.035 rows=1250000 loops=1) Recheck Cond: ((gs & 1) = 1) Filter: (((gs & 2) = 2) AND ((gs & 4) = 4)) Rows Removed by Filter: 3750000 Heap Blocks: exact=44248 -> Bitmap Index Scan on xyz_expr_idx (cost=0.00..91643.55 rows=4962016 width=0) (actual time=463.477..463.477 rows=5000000 loops=1) Index Cond: ((gs & 1) = 1) Justin