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 1fmgaU-0006Hh-5S for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Aug 2018 14:34:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fmgaR-0002CY-Hk for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Aug 2018 14:34:19 +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 1fmgaR-0002CQ-6a for pgsql-hackers@lists.postgresql.org; Mon, 06 Aug 2018 14:34:19 +0000 Received: from mail-wm0-x242.google.com ([2a00:1450:400c:c09::242]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fmgaN-000461-Gs for pgsql-hackers@postgresql.org; Mon, 06 Aug 2018 14:34:18 +0000 Received: by mail-wm0-x242.google.com with SMTP id s12-v6so14096391wmc.0 for ; Mon, 06 Aug 2018 07:34:15 -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=f4IXmkNmiAIRKRJtG9jTVC/dJOFYVMllna2GLuQ7bNI=; b=TMnKzgS3rBcmxEcQSihXWFv4kiFTChLOIVljkBEt0R+HSFeruzDwJsa+tJ9NvCROj+ EO3Mz4sQJ5afags03khGb70A0AN7TNGxabaE13of1/HmNyY1eFD0A4Bwr/6wwi4+7ACp 9Bg/qkq9oQseS07F+hUT8rmOic9PFmxVkHkS4vuw4yDBVyyLJvAd/VsGr4RlYVy8TdNb /pjA99+DaHdUNaGdd3+PHmKR8ujCP7fmlSsgjz7Czh7qEXlr8q3fZXpwcbOvjjMoXmJs 6OOB8v+yuiykJ7d0C89rRz8t1IzQ5ub8jOsR/5obUMxo6rv0etffvzAT+EomINzaS+Hn hO5w== 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=f4IXmkNmiAIRKRJtG9jTVC/dJOFYVMllna2GLuQ7bNI=; b=EUPkS/Q29j14kcfzFvBmdCZCuRhaibeIaLnz1eGPvw3UzriiH3X1XnBpafP8oJrQZa OTpa4uZsjz5YaTwXtDwSJ4mR877RVxvwGKdrzJFAQxE3vKcgY6ffkFMQkTwM231lr24K q/id3qux5eiPBGNWD4dDGGHLzfcsPGPdi06Z3gDSwt3B4m1WCORBayVpSJM7Nal9g4Ei 6/Ez8TFjpl4nmxtT1E4rdUHy/ywVfy4mGYNH8oWov3nrpinabYjLbZqiICocvPbEU5z3 u0lgbigu/H5FTVVg5NH6pyyFo85Zbgt/JWImkpmWJS8tc7LUm+iDNsas6LfCJNDMBJX8 P7xA== X-Gm-Message-State: AOUpUlGGdG6NcX81GSBbasNzskOPZk/dM8x4KVVv1OAK/lD0J0t83bf2 crwkfZFESVQHmd6MXTBFia08HzRxACfo54xsUK3iMG1h+zQXu0zOL6doliW5/4/MJOGOFUi7vgL 8uihrrPNFuUwljYoklKlCfuwcz2WHiDPBOq4YseMmGA0rF5eWviIDQXXb+2HW9ZlDBmnPU1QNWX eSQ3TOB7hW2C8= X-Google-Smtp-Source: AAOMgpey9xJWS58nLsQ29gJNKvSzWYCc5nOIZUNrRyXdgEra77gCcR77YSEu8DXPe7C3Wl4CYE/3nw== X-Received: by 2002:a1c:32c4:: with SMTP id y187-v6mr11465762wmy.31.1533566053644; Mon, 06 Aug 2018 07:34:13 -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 n7-v6sm20552277wrr.35.2018.08.06.07.34.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 Aug 2018 07:34:13 -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: <341ca23f-144d-130d-1b45-ddddf581797e@2ndquadrant.com> Date: Mon, 6 Aug 2018 16:34:10 +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 08/03/2018 04:24 PM, Dean Rasheed wrote: > On 17 July 2018 at 14:03, Tomas Vondra wrote: >> 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). >> > > So I decided to play a little more with this, experimenting with a > much simpler approach -- this is for MCV's only at the moment, see the > attached (very much WIP) patch (no doc or test updates, and lots of > areas for improvement). > > The basic idea when building the MCV stats is to not just record the > frequency of each combination of values, but also what I'm calling the > "base frequency" -- that is the frequency that that combination of > values would have if the columns were independent (i.e., the product > of each value's individual frequency). > > The reasoning then, is that if we find an MCV entry matching the query > clauses, the difference (frequency - base_frequency) can be viewed as > a correction to be applied to the selectivity returned by > clauselist_selectivity_simple(). If all possible values were covered > by matching MCV entries, the sum of the base frequencies of the > matching MCV entries would approximately cancel out with the simple > selectivity, and only the MCV frequencies would be left (ignoring > second order effects arising from the fact that > clauselist_selectivity_simple() doesn't just sum up disjoint > possibilities). For partial matches, it will use what multivariate > stats are available to improve upon the simple selectivity. > > I wondered about just storing the difference (frequency - > base_frequency) in the stats, but it's actually useful to have both > values, because then the total of all the MCV frequencies can be used > to set an upper bound on the non-MCV part. > > The advantage of this approach is that it is very simple, and in > theory ought to be reasonably applicable to arbitrary combinations of > clauses. Also, it naturally falls back to the univariate-based > estimate when there are no matching MCV entries. In fact, even when > there are no matching MCV entries, it can still improve upon the > univariate estimate by capping it to 1-total_mcv_sel. > > I tested it with the same data posted previously and a few simple > queries, and the initial results are quite encouraging. Where the > previous patch sometimes gave noticeable over- or under-estimates, > this patch generally did better: > > > Query Actual rows Est (HEAD) Est (24 Jun patch) Est (new patch) > Q1 50000 12625 48631 49308 > Q2 40000 9375 40739 38710 > Q3 90000 21644 172688 88018 > Q4 140000 52048 267528 138228 > Q5 140000 52978 267528 138228 > Q6 140000 52050 267528 138228 > Q7 829942 777806 149886 822788 > Q8 749942 748302 692686 747922 > Q9 15000 40989 27595 14131 > Q10 15997 49853 27595 23121 > > Q1: a=1 and b=1 > Q2: a=1 and b=2 > Q3: a=1 and (b=1 or b=2) > Q4: (a=1 or a=2) and (b=1 or b=2) > Q5: (a=1 or a=2) and (b<=2) > Q6: (a=1 or a=2 or a=4) and (b=1 or b=2) > Q7: (a=1 or a=2) and not (b=2) > Q8: (a=1 or a=2) and not (b=1 or b=2) > Q9: a=3 and b>0 and b<3 > Q10: a=3 and b>0 and b<1000 > Interesting idea, and the improvements certainly seem encouraging. I wonder what a counter-example would look like - I think the MCV and non-MCV parts would have to behave very differently (one perfectly dependent, the other perfectly independent). But that does seem very likely, and even if it was there's not much we can do about such cases. > > I've not tried anything with histograms. Possibly the histograms could > be used as-is, to replace the non-MCV part (other_sel). Or, a similar > approach could be used, recording the base frequency of each histogram > bucket, and then using that to refine the other_sel estimate. Either > way, I think it would be necessary to exclude equality clauses from > the histograms, otherwise MCVs might end up being double-counted. > I do have an idea about histograms. I didn't have time to hack on it yet, but I think it could work in combination with your MCV algorithm. Essentially there are two related issues with histograms: 1) equality conditions Histograms work nicely with inequalities, not that well for equalities. For equality clauses, we can estimate the selectivity as 1/ndistinct, similarly to what we do in 1-D cases (we can use ndistinct coefficients if we have them, and MCV tracking the common combinations). If there are both equalities and inequalities, we can then use the equality clauses merely as condition (to limit the set of buckets), and evaluate the inequalities for those buckets only. Essentially compute P(equals + inequals) = P(equals) * P(inequals | equals) IMHO that should help with estimating selectivity of equality clauses. 2) estimating bucket selectivity The other question is how to combine selectivities of multiple clauses for a single bucket. I think the linear approximation (convert_scalar or something like that) and computing geometric mean (as you proposed) is a solid plan. I do have this on my TODO list for this week, unless something urgent comes up. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services