Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ffPdi-00075I-Us for pgsql-hackers@arkaria.postgresql.org; Tue, 17 Jul 2018 13:03:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ffPdh-0006MB-8K for pgsql-hackers@arkaria.postgresql.org; Tue, 17 Jul 2018 13:03:37 +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.89) (envelope-from ) id 1ffPdh-0006M1-1E for pgsql-hackers@lists.postgresql.org; Tue, 17 Jul 2018 13:03:37 +0000 Received: from mail-wm0-x243.google.com ([2a00:1450:400c:c09::243]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ffPde-0007Xd-EG for pgsql-hackers@postgresql.org; Tue, 17 Jul 2018 13:03:36 +0000 Received: by mail-wm0-x243.google.com with SMTP id 69-v6so1431420wmf.3 for ; Tue, 17 Jul 2018 06:03:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=lO6LuI9UtyHaLjRUnohKWwg5+JYv0QRH4m6etPOrSCA=; b=zNbKunWMX6doqS75azFTFT8uBMToYLqI+YUQBxg2AOlTHByC+oQyssSw1S8AodEnLv z1r16pPh6n3vgD6koh3VXRfFErKdfboidA6s2WLH+OxNK7KGwG67rCIoOrvJlcR2rLLG fLp8NcxtsLaPjwVE5WnaQDq5LGYFXnUAPGvNoQfpbEMVGNhCL32TbJbz0kuU/47jtlYT +a1vo/NDgEn8rXNHroAkP2nnYT5yNOWYYcaVK7L6PBkq03L5Loaqp1tm7t++sUMIpPwH q+UYerIibycN5I1gdjda0VM78vX8iA0wyWsX0Dt87+DA3C8amwEvlJyWtcDRaVAac+h7 CXxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=lO6LuI9UtyHaLjRUnohKWwg5+JYv0QRH4m6etPOrSCA=; b=Q7491tpqO8Vxrqq7UsWitZbHSIfF03FKy8x+E1/FK9bDZIvwBU67a4YoZEwJh1vQnK AaHy2OP+NVB1hyypfpOAzdlXMKDzq8balaqozFvo1wfXvy/3lqOnTi/r7h/AhLeNCLkz 5cXVm/IesJwKCCsMfdU+KbzvijuQ2tdkHOvRf+EM3O7u+oZIB373CpkIZBS6+FpjH9Bt jWZJN+7O93yqidU0loNx+4LnU87iU/zqT1j/wtvdNt5MdjK/7SPU/f4aaFASg4ta+j9Y TmHW8+nzXD+CXZZ2OyNFUG6JS9mv4lk7hSSb59aSC4xQUK+Z6Ip7CU4orFWywPMj+4s6 LHEw== X-Gm-Message-State: AOUpUlFTfpjvq7kFO2rPyzdC8Cj/MYelGI2uPjj26TlMJE5zbJkbMk8h B9o3snjGBg0RI3q8Hn2dygQI/fcVzg0ah+sKXqlrd1LboWtfiNi9u19UWmbmkuHqS5XLcs75wNo YKcBMU1PHkyHU7t+evo5HKXpNped8S5t4ci79nWg0qERGOvqSgq0iv+DktZ613fiBmOcK/bxNls rphK9ri7evJf8= X-Google-Smtp-Source: AAOMgpfPlA+Mlmt/rBzeTAEaeHDti/C103as+K+TdfH22VteekPlEeYoVKvt5+y/nc82hzO8Dik+SA== X-Received: by 2002:a1c:93d3:: with SMTP id v202-v6mr1024581wmd.57.1531832612752; Tue, 17 Jul 2018 06:03:32 -0700 (PDT) Received: from [10.137.2.21] (static-84-42-175-93.net.upcbroadband.cz. [84.42.175.93]) by smtp.gmail.com with ESMTPSA id u14-v6sm2063813wrs.57.2018.07.17.06.03.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 17 Jul 2018 06:03:31 -0700 (PDT) Subject: Re: [HACKERS] PATCH: multivariate histograms and MCV lists To: Dean Rasheed Cc: Bruce Momjian , Alvaro Herrera , Andres Freund , Thomas Munro , Mark Dilger , Adrien Nayrat , Pg Hackers References: <49348328-1a02-7e45-e50f-e1d4d0243c5c@2ndquadrant.com> <7d12b5d1-ac77-67e3-c67e-7fcbd3753e9c@2ndquadrant.com> <20180407141201.GA10394@momjian.us> <4416a567-4452-c5fd-7117-5cc7827b5fad@2ndquadrant.com> <80bc98b2-1c43-1fe8-b316-d447bb68cff2@2ndquadrant.com> From: Tomas Vondra Message-ID: Date: Tue, 17 Jul 2018 15:03:28 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 07/17/2018 11:09 AM, Dean Rasheed wrote: > On 16 July 2018 at 21:55, Tomas Vondra wrote: >> ... >> >> So, how would the proposed algorithm work? Let's start with "a=1": >> >> sel(a=1) = 0.1508 >> >> I don't see much point in applying the two "b" clauses independently (or >> how would it be done, as it's effectively a single clause): >> >> sel(b=1 or b=2) = 0.0673 >> >> And we get >> >> total_sel = sel(a=1) * sel(b=1 or b=2) = 0.0101 >> >> From the multivariate MCV we get >> >> mcv_sel = 0.0399 >> >> And finally >> >> total_sel = Max(total_sel, mcv_sel) = 0.0399 >> >> Which is great, but I don't see how that actually helped anything? We >> still only have the estimate for the ~7% covered by the MCV list, and >> not the remaining non-MCV part. >> > > Right. If these are the only stats available, and there are just 2 > top-level clauses like this, it either returns the MCV estimate, or > the old univariate estimate (whichever is larger). It avoids > over-estimating, but will almost certainly under-estimate when the MCV > matches are incomplete. > Yeah :-( In my experience under-estimates tend to have much worse consequences (say a nested loop chosen by under-estimate vs. hash join chosen by over-estimate). This certainly influenced some of the choices I've made in this patch (extrapolation to non-MCV part is an example of that), but I agree it's not particularly scientific approach and I'd very much want something better. > >> I could do the same thing for the second query, but the problem there is >> actually exactly the same - extrapolation from MCV to non-MCV part >> roughly doubles the estimate. >> >> So unless I'm applying your algorithm incorrectly, this does not seem >> like a very promising direction :-( >> > > You could be right. Actually it's the order dependence with more than > 2 top-level clauses that bothers me most about this algorithm. It's > also not entirely obvious how to include histogram stats in this > scheme. > I think for inequalities that's fairly simple - histograms work pretty well for that, and I have a hunch that replacing the 0.5 estimate for partially-matching buckets with conver_to_scalar-like logic and the geometric mean (as you proposed) will work well enough. For equalities it's going to be hard. The only thing I can think of at the moment is checking if there are any matching buckets at all, and using that to decide whether to extrapolate the MCV selectivity to the non-MCV part or not (or perhaps to what part of the non-MCV part). > A different approach that I have been thinking about is, in > mcv_update_match_bitmap(), attempt to work out the probability of all > the clauses matching and it not being an MCV value. For example, given > a clause like a=1 whose univariate estimate we know (0.1508 in the > above example), knowing that the MCV values account for 0.0222+0.0177 > of that, the remainder must be from non-MCV values. So we could say > that the probability that a=1 and it not being and MCV is > 0.1508-0.0222-0.0177 = 0.1109. So then the question is could we > combine these non-MCV probabilities to give an estimate of how many > non-MCV values we need to worry about? I've not fully thought that > through, but it might be useful. Could we use it to derive some upper boundaries on the non-MCV part? > The problem is, it's still likely to > over-estimate when the MCV matches fully cover all possibilities, and > I still don't see a way to reliably detect that case. > I guess we can use a histogram to limit the over-estimates, as explained above. It may not be 100% reliable as it depends on the sample and how exactly the buckets are formed, but it might help. But perhaps these over-estimates are a particularly serious issue? When you already get matches in a MCV, the number of matching rows is going to be pretty significant. If you over-estimate 2x, so what? IMHO that's still pretty accurate estimate. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services