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 1qABXn-0003Vv-9H for pgsql-hackers@arkaria.postgresql.org; Fri, 16 Jun 2023 15:39:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1qABXm-0003Lj-28 for pgsql-hackers@arkaria.postgresql.org; Fri, 16 Jun 2023 15:39:22 +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 1qABXl-0003Er-Oh for pgsql-hackers@lists.postgresql.org; Fri, 16 Jun 2023 15:39:21 +0000 Received: from mail-ej1-x62c.google.com ([2a00:1450:4864:20::62c]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qABXh-002YFy-C5 for pgsql-hackers@postgresql.org; Fri, 16 Jun 2023 15:39:18 +0000 Received: by mail-ej1-x62c.google.com with SMTP id a640c23a62f3a-9827bd8e0afso124489366b.1 for ; Fri, 16 Jun 2023 08:39:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1686929955; x=1689521955; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id:from :to:cc:subject:date:message-id:reply-to; bh=/oIC715WEIAd3L96pMRnl66gVyzvLv7ih8DNMJUzMOY=; b=bsvEJ3E5sRWxxhjNivR6LPmOxQV17OiEYmOzl9bDbyf0dpmE2GtngJv2I0cUYRXHm4 JnU4YJ53dQPPG5ud/ByCL8jFgVL9THFJCAyGX+GUwuPQEGQxKLwlIepY0xdEQALKI69q Lp4Smt68Jf7ZpuvrEABTkDKtHW4aSqP9s2NuDn74TJDlINaoOhk8JaKLx0+K3WKNuqie 7O6EuLuefxD7U42LMEZMd7/eAk7ZhnFvl460kMtwfD3u6ygqwSQtYUHfr0rbYsprEBSw On8tMagkbcBxDN8fW2tLvY3NDBD/dU8jGk71mMi9pk/ssrgMryYdwgpPRekqzQZlpKH8 eTAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686929955; x=1689521955; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=/oIC715WEIAd3L96pMRnl66gVyzvLv7ih8DNMJUzMOY=; b=EM6QlFQ1NAg/GPMtBzpWLHDnhHS80ThI0D6tqolvY12ULvClr3I5Be/VKqunRmqjQY 5VxXWop1N4HP3hSlxMcA09Q+iu0A4Fk0++qjjgYIi6pJ5xmRnR50syb53HKxaF/Dge5R 81zLCpNj9LeBdi+pM2zdpdyc/Ag9JLzFOaB5wwI3fokSNFUhGyOU3ICPL0TrDKl86bzw CFoFc15q0CvCcHAzCUm+SRX3XKt9MzZxTUMK6+6FKUpPbaubOpRi4udyMEGZR7RRBHwc CQo58Q7B3EVJSbRNIh8pqWateD3urDoinzNYDhOFjoXa5X6i6snp/So0oxjNeXnAmviA s7/Q== X-Gm-Message-State: AC+VfDxOqZtnt9s+NGtKY+Ul6HsbzOx/jyznFlVd1VwxSb7E+fi4JNFN vRxBI7j/EE3do97oSHm3pKTj+w== X-Google-Smtp-Source: ACHHUZ5gYkltxqSiHqP7a1YA8MCi/hJYA7gQn6+d0PMvmltIlM92AtQ50goe5Kcc7mJ9+Ro5gfyXbQ== X-Received: by 2002:a17:907:a06:b0:974:1e0e:9bd4 with SMTP id bb6-20020a1709070a0600b009741e0e9bd4mr2225501ejc.16.1686929955641; Fri, 16 Jun 2023 08:39:15 -0700 (PDT) Received: from [10.137.0.17] (static-84-42-175-93.bb.vodafone.cz. [84.42.175.93]) by smtp.gmail.com with ESMTPSA id i20-20020a170906a29400b00977fb2fb44asm11008292ejz.28.2023.06.16.08.39.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 16 Jun 2023 08:39:14 -0700 (PDT) Message-ID: Date: Fri, 16 Jun 2023 17:39:14 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.1 Subject: Re: Incorrect estimation of HashJoin rows resulted from inaccurate small table statistics To: Quan Zongliang , pgsql-hackers References: Content-Language: en-US From: Tomas Vondra In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 6/16/23 11:25, Quan Zongliang wrote: > > We have a small table with only 23 rows and 21 values. > > The resulting MCV and histogram is as follows > stanumbers1 | {0.08695652,0.08695652} > stavalues1  | {v1,v2} > stavalues2  | > {v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21} > > An incorrect number of rows was estimated when HashJoin was done with > another large table (about 2 million rows). > > Hash Join  (cost=1.52..92414.61 rows=2035023 width=0) (actual > time=1.943..1528.983 rows=3902 loops=1) > That's interesting. I wonder how come the estimate gets this bad simply by skipping values entries with a single row in the sample, which means we know the per-value selectivity pretty well. I guess the explanation has to be something strange happening when estimating the join condition selectivity, where we combine MCVs from both sides of the join (which has to be happening here, otherwise it would not matter what gets to the MCV). It'd be interesting to know what's in the other MCV, and what are the other statistics for the attributes (ndistinct etc.). Or even better, a reproducer SQL script that builds two tables and then joins them. > The reason is that the MCV of the small table excludes values with rows > of 1. Put them in the MCV in the statistics to get the correct result. > > Using the conservative samplerows <= attstattarget doesn't completely > solve this problem. It can solve this case. > > After modification we get statistics without histogram: > stanumbers1 | {0.08695652,0.08695652,0.04347826,0.04347826, ... } > stavalues1  | {v,v2, ... } > > And we have the right estimates: > Hash Join  (cost=1.52..72100.69 rows=3631 width=0) (actual > time=1.447..1268.385 rows=3902 loops=1) > I'm not against building a "complete" MCV, but I guess the case where (samplerows <= num_mcv) is pretty rare. Why shouldn't we make the MCV complete whenever we decide (ndistinct <= num_mcv)? That would need to happen later, because we don't have the ndistinct estimate yet at this point - we'd have to do the loop a bit later (or likely twice). FWIW the patch breaks the calculation of nmultiple (and thus likely the ndistinct estimate). regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company