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 1ouVco-0005og-OG for pgsql-hackers@arkaria.postgresql.org; Mon, 14 Nov 2022 09:19:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ouVcn-00014S-76 for pgsql-hackers@arkaria.postgresql.org; Mon, 14 Nov 2022 09:19:29 +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 1ouVcm-00014J-RW for pgsql-hackers@lists.postgresql.org; Mon, 14 Nov 2022 09:19:28 +0000 Received: from mail-ed1-x52a.google.com ([2a00:1450:4864:20::52a]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ouVcg-0001QS-DY for pgsql-hackers@lists.postgresql.org; Mon, 14 Nov 2022 09:19:27 +0000 Received: by mail-ed1-x52a.google.com with SMTP id x102so1098279ede.0 for ; Mon, 14 Nov 2022 01:19:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=eCYycJp6PttR/yiykTZknlf1jBAV11eLsYp8PLMhKRU=; b=TSwvqU9etwNHYTxCoqGy91vv70vLep8oQm7Je8PhdZNSr5t0rzuMOUi1W278wx0ReJ 6n29Xcwfsr+Q1iHwpnCHMcNABor5dDL9SdAhuKYYCVq+Vln3TNywLkLEf9cF2+YrPJ7V 2ZMx07AkuBiKmSzYEbsscKgsHzyncweWE1YpjhE84PZG82yHiYvMbwo3zopwyRwlZJ4m K45MYwLeGgdQXte2gv610T7jxqzPYFjyDwRx3gnBri02HDotix6sXwGe/81iSoLoe4ya 13L4lpnFOmGUKffl6xe6r6U7hg143jpryZQ8JY0NbOFLIwaFqsKBI4poFOqGPJ/6E1Kv W6zA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=eCYycJp6PttR/yiykTZknlf1jBAV11eLsYp8PLMhKRU=; b=B+e2zD5WVGGmlKfPfu+WgUYRQUQFzOyjDgT9d76i9sw9RoTQ41XYhuq+MKfV3wkcm4 ApSEOOKBgX7C3SPwCY1JwWtB0fj4pVkT5os/Az3vp5PI9oVJhVzWDB6otIlff1k5al8z SIwqBxjR6OV4G4VZknSnv25xlmq4rM98Zy6dxii0HrOca/UxyMAT3z8HIT2z17QXRthI qItS+PyNpARCyPXbY0ETg1xPPyQE5bHFquUnitakHHe+oeh52UM33zeVLgDMomC319zx L8Ud4pls6UTJfD/Rg9a/7OxXFQPfFypEw3W+ASq8oxqHZFCe1x1j8jJoAwscLFusGpKa mtWA== X-Gm-Message-State: ANoB5pmacFqecYo1k9ssfg8k4GJ+Gb624sXrnEC2+pZJkMSaYz5H5V+M 7oo4sc1DvPXpNcv9dIPQIlkGcOgO5lE= X-Google-Smtp-Source: AA0mqf62vljrANZ7SYBMSckbK+NumdeoMrisu2wecX0hq2DbK7PPUdONY7O+IPXtt+a/y0h6naL6/w== X-Received: by 2002:a05:6402:1751:b0:462:1a67:75ef with SMTP id v17-20020a056402175100b004621a6775efmr10597885edx.16.1668417560748; Mon, 14 Nov 2022 01:19:20 -0800 (PST) Received: from [172.16.209.129] ([147.161.235.30]) by smtp.gmail.com with ESMTPSA id o9-20020a170906768900b0078df26efb7dsm3900312ejm.107.2022.11.14.01.19.19 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 14 Nov 2022 01:19:20 -0800 (PST) Message-ID: <16acb727-24e8-6453-1711-aae339802966@gmail.com> Date: Mon, 14 Nov 2022 10:19:18 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.2.2 Subject: Re: Optimize join selectivity estimation by not reading MCV stats for unique join attributes Content-Language: en-US To: Tom Lane Cc: PostgreSQL Developers References: <522124.1668179795@sss.pgh.pa.us> From: David Geier In-Reply-To: <522124.1668179795@sss.pgh.pa.us> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi Tom, > There won't *be* any MCV stats for a column that ANALYZE perceives to > be unique, so I'm not quite sure where the claimed savings comes from. We save if one join attribute is unique while the other isn't. In that case stored MCV stats are read for the non-unique attribute but then never used. This is because MCV stats in join selectivity estimation are only used if they're present on both columns > Please provide a concrete example. A super simple case already showing a significant speedup is the following. The more ways to join two tables and the more joins overall, the higher the expected gain. CREATE TABLE bar(col INT UNIQUE); CREATE TABLE foo (col INT); INSERT INTO foo SELECT generate_series(1, 1000000, 0.5); SET default_statistics_target = 10000; ANALYZE foo, bar; \timing on EXPLAIN SELECT * FROM foo, bar WHERE foo.col = bar.col; Running the above query five times gave me average runtimes of: - 0.62 ms without the patch and - 0.48 ms with the patch. -- David Geier (ServiceNow)