Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1c20f9-0007cb-0B for pgsql-performance@arkaria.postgresql.org; Wed, 02 Nov 2016 18:53:27 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1c20f8-0003Oy-A9 for pgsql-performance@arkaria.postgresql.org; Wed, 02 Nov 2016 18:53:26 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1c20f7-0003Os-TC for pgsql-performance@postgresql.org; Wed, 02 Nov 2016 18:53:26 +0000 Received: from mail-yw0-x231.google.com ([2607:f8b0:4002:c05::231]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1c20f4-0005HE-35 for pgsql-performance@postgresql.org; Wed, 02 Nov 2016 18:53:24 +0000 Received: by mail-yw0-x231.google.com with SMTP id h14so19408557ywa.2 for ; Wed, 02 Nov 2016 11:53:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:subject:message-id:mime-version:content-disposition :user-agent; bh=CWjtaacc0VImn8N3Cx9x/doQBeRmUABzqGjsGQTFlLQ=; b=vYg3jDrctff2Vmo0I9vVyAAeME+83NLCw0tzj77+13Rlmxuy8C6jKjEM8GMgaLSf/J YmWqTnxR+6vy9zw+sKHWwj8GDpXOY4KOhqQ9xyeau07+qiPYHs//3QkX7mKq/DgDXpt5 ylI4YFjHX2WwMLBIvhvIDJOtjZnH/zG5tyGLDR0ka3ar1inU2c+aTguRm5EnOAQ2fWjE aFNsN0DXJCqDTk4JeUIp+ibxy6sHij5LF2LvYc2I7YYsXkKxdJiQsvYBQXp2cLWWn9g9 y5eK4tulbMR/HQ57b7tv/ova8qb5Vr6oBOobu78KtKOZFolKr8sIvaGkU1T/YOquw43d zSkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition:user-agent; bh=CWjtaacc0VImn8N3Cx9x/doQBeRmUABzqGjsGQTFlLQ=; b=bux5L2e+XLR+PmvQwpH1hrq8oLudZzTfhCfd8ivXKowniFafK7/pa1q06WaYL7812M k5YPAtELZLLIZQpXgRjQlcBbXdJ5f5JdJFMWk+lnnTuRVSVS7nipVOwGQTxZZsDeyyxh 048hGSN4sCOtWEPAa66z6rRcNkiMJiw/aJQipp2YVTe51ew/QSSFTOJZ/TtCLa/Gj+PD tLVuwmN1GuD2zTTA2ycrPGQLCBrTQEu3djs1Yq/Rsn1RP+dszNkUunKgc0KdSMhmZ1K3 eEzffsoopTZ7x+rvUJvIzdxAPA3WwjYQwsj4ZXbAuO9Dj020DbgvW3Tr926cXbvnhQaA 26gw== X-Gm-Message-State: ABUngvcwjCPQ/Dr814W5QgP7QJqWHDdl00mJcZtg2dp/D9bW7F4wqumoSN8Tq0/PgqvsOQ== X-Received: by 10.36.91.13 with SMTP id g13mr3835794itb.86.1478112799775; Wed, 02 Nov 2016 11:53:19 -0700 (PDT) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id b134sm1324647ioe.15.2016.11.02.11.53.19 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 02 Nov 2016 11:53:19 -0700 (PDT) Received: by pryzbyj (Postfix, from userid 1000) id B122480142E; Wed, 2 Nov 2016 13:53:18 -0500 (CDT) Date: Wed, 2 Nov 2016 13:53:18 -0500 From: Justin Pryzby To: pgsql-performance@postgresql.org Subject: no MCV list of tiny table with unique columns Message-ID: <20161102185318.GA3987@telsasoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org We have a report query which joins (multiple times, actually) against this trivial, tiny table: ts=# \d bsm_to_switch Table "public.bsm_to_switch" Column | Type | Modifiers --------+------+----------- bsm | text | not null switch | text | not null ts=# SELECT length(bsm), length(switch) FROM bsm_to_switch; length | length --------+-------- 10 | 6 10 | 6 (2 rows) The column values are distinct. I believe the join is being (badly) underestimated, leading to a crappy plan involving multiple nested loop joins, which takes 2.5 hours instead of a handful of seconds; I believe that might be resolved by populating its MCV list.. ..however, on reading commands/analyze.c, the issue is these columns have no duplicates, and also postgres decides that "since the number of distinct rows is greater than 10% of the total number of rows", that ndistinct should be -1 (meaning it scales with the table size). That's fine, except that it then effectively precludes populating the MCV list. | if (nmultiple == 0) | { | /* | * If we found no repeated non-null values, assume it's a unique | * column; but be sure to discount for any nulls we found. | */ | stats->stadistinct = -1.0 * (1.0 - stats->stanullfrac); | } | else if (track_cnt < track_max && toowide_cnt == 0 && | nmultiple == track_cnt) | { | /* | * Our track list includes every value in the sample, and every | * value appeared more than once. Assume the column has just | * these values. (This case is meant to address columns with | * small, fixed sets of possible values, such as boolean or enum | * columns. If there are any values that appear just once in the | * sample, including too-wide values, we should assume that that's | * not what we're dealing with.) | */ | stats->stadistinct = track_cnt; | } ts=# SELECT attname, inherited, null_frac, avg_width, n_distinct, most_common_vals FROM pg_stats WHERE tablename='bsm_to_switch'; attname | inherited | null_frac | avg_width | n_distinct | most_common_vals ---------+-----------+-----------+-----------+------------+------------------ bsm | f | 0 | 11 | -1 | switch | f | 0 | 7 | -1 | (2 rows) Any ideas? I tried setting n_distinct=2, but that seems to not have any effect within ANALYZE itself. ts=# SELECT attname, inherited, null_frac, avg_width, n_distinct, most_common_vals FROM pg_stats WHERE tablename='bsm_to_switch'; attname | inherited | null_frac | avg_width | n_distinct | most_common_vals ---------+-----------+-----------+-----------+------------+------------------ bsm | f | 0 | 11 | 2 | switch | f | 0 | 7 | 2 | (2 rows) Thanks in advance. Justin -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance