Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f1CPc-00062j-Hm for pgsql-hackers@arkaria.postgresql.org; Wed, 28 Mar 2018 14:50:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f1CPa-0005jC-Nx for pgsql-hackers@arkaria.postgresql.org; Wed, 28 Mar 2018 14:50:50 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f1CPa-0005j0-9s for pgsql-hackers@lists.postgresql.org; Wed, 28 Mar 2018 14:50:50 +0000 Received: from mail-wm0-x243.google.com ([2a00:1450:400c:c09::243]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1f1CPW-0007fW-Rn for pgsql-hackers@postgresql.org; Wed, 28 Mar 2018 14:50:48 +0000 Received: by mail-wm0-x243.google.com with SMTP id b127so4642471wmf.5 for ; Wed, 28 Mar 2018 07:50:46 -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=q2X8UUIze9i78z42LhqVJlQ6YOMgycUTCTMcqjQavFk=; b=cWL8X/b1KZhYPlNl3XufqHXg4RyV/V+/ViiRqmqn6R7y83wchm63t4S2e4LyZJDv9+ brO+krO8Os/UXnVtlfhx4a9MgAM8EdhQ0toMI01EWG1kc1dRWopXlcpYN50GMNkXDe/G O+Fytl9UjRCESrL9m6xqIAy0C8td6jNUP1NubFjrb86GcOiTKjMVG6/WwbWUXsSe8H1S qly3yGTKHcwceb/ipzYAzZtuvx/dZ4vq0VxrsUKsfOl+lTFCgwi2kscfbZ/zPv/P9V66 flpFqG+uhGAZ6Y9XoZ9sgouX1MlPodr4rayKwXZ1z2hfVKhe+E4maF7QxCMsbmQAHQ/L ABBA== 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=q2X8UUIze9i78z42LhqVJlQ6YOMgycUTCTMcqjQavFk=; b=Zrwbm64N2wj39TDfMJBZZT25bGlQhB4LgE+q9LEECRpyRoa1kCvYGlZkCX1wHg+uGA D9Yn47Un7KQJW6LT5rNkqBZpPRf6QDOQIu9eaEWCvJDzyGPMSyJyoWHkxG93n25CTPMJ mhlnmgJIla4/n5nX+sII5beFp3EN5ApWIxvUIg46mX2T9xiwNl0D1RJ42MggIAHZZiSM cg5KBHWu6tKCBe+3s7OS6UejRCfUvvY1tgp2noXoptlCJ9yguFw9BwzW1rqioJ56lFnl uuinLRiweOgi4InyTyfUyb7vZXfEEvVHhzlLRnMTLvR0E4sEvyFSljb59gAsW5mQMYGZ eT0Q== X-Gm-Message-State: AElRT7EXPnJGiKbkcwY/MartAeUMrrngPKqg/2e3fC4Y2ziUDJC5pg7j NXizH/WJO3f5GQFS1OkXqKD2WCVb826SnhoCOVYL9/HmSa9lAwH5fbUEUp+JBeSoEVisaD9z93X XQUsurtXWHC9QlmLVQHK2PRY/Xg/jBlCbgnSGxKFlj521K8D1N1/nJRdh0b+FwVpmb56h4kbsn3 G1S6O5RxO8ad4= X-Google-Smtp-Source: AIpwx49QaaL2o/rqOkiylnIIUCjUnEJ4/Ala4d1wsDOWvpBYPx/mW9BOyTxh+8PikMjaMcVPWJDO2Q== X-Received: by 10.28.150.210 with SMTP id y201mr3141996wmd.112.1522248644658; Wed, 28 Mar 2018 07:50:44 -0700 (PDT) Received: from [10.137.2.19] (ip-78-102-97-226.net.upcbroadband.cz. [78.102.97.226]) by smtp.gmail.com with ESMTPSA id m35sm7591056wrm.59.2018.03.28.07.50.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 28 Mar 2018 07:50:43 -0700 (PDT) Subject: Re: [HACKERS] PATCH: multivariate histograms and MCV lists To: Dean Rasheed Cc: Alvaro Herrera , Andres Freund , Thomas Munro , Mark Dilger , Adrien Nayrat , Pg Hackers References: <20180310171906.gd6vyz6hnou2n3h7@alvherre.pgsql> <850c3b6b-2641-496c-4efc-500c720f7260@2ndquadrant.com> <66ba9c45-3b07-e5b6-fd11-15e75f6ee8e1@2ndquadrant.com> <49348328-1a02-7e45-e50f-e1d4d0243c5c@2ndquadrant.com> <1aaa338a-fa70-1b92-988e-498eb996ce57@2ndquadrant.com> <681c6a78-2aef-769e-bd0f-290ac650e71d@2ndquadrant.com> From: Tomas Vondra Message-ID: Date: Wed, 28 Mar 2018 16:50:41 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 03/28/2018 04:12 PM, Dean Rasheed wrote: > On 28 March 2018 at 01:34, Tomas Vondra wrote: >> Attached is a patch fixing this. In the end I've decided to keep both >> branches - one handling boolean Vars and one for NOT clauses. I think >> you're right we can only see (NOT var) cases, but I'm not sure about that. >> >> For example, what if an operator does not have a negator? Then we can't >> transform NOT (a AND b) => (NOT a OR NOT b), I guess. So I kept this for >> now, and we can remove this later. >> > > OK, but it's going to have to work harder to set "fullmatch" > correctly. If you have a boolean Var clause, which is identical to > "bool_var = true", it ought to add to "eqmatches" if true is found in > the MCV list. Likewise a boolean Var under a NOT clause is identical > to "bool_var = false", so it ought to add to "eqmatches" if false is > found in the MCV list. Both those cases would be easy to handle, if > general NOT support wasn't required, and you just special-cased "NOT > bool_var". > > If you're going to handle the general case of an arbitrary clause > under a NOT, then the recursive call to mcv_update_match_bitmap() > would seem to need to know that it's under a NOT (a new "is_not" > parameter?), to invert the logic around adding to "eqmatches". That > applies to other general OpExpr's too -- for example, "NOT (box_var = > ?)" won't be rewritten because there is no box_ne operator, but when > mcv_update_match_bitmap() is called recursively with the "box_var = > ?", it shouldn't add to "eqmatches", despite this being an EQSEL > operator. > > As mentioned before, I think this whole thing only works if > mcv_update_match_bitmap() returns the "eqmatches" list, so that if it > is called recursively, it can be merged with the caller's list. What > isn't immediately obvious to me is what happens to a NOT clause under > another NOT clause, possibly with an AND or OR in-between. Would the > "is_not" parameter just flip back to false again? > After thinking about this a bit more, I'm not sure if updating the info based on recursive calls makes sense. The fullmatch flag was supposed to answer a simple question - can there be just a single matching item? If there are equality conditions on all columns, there can be just a single matching item - if we have found it in the MCV (i.e. s1 > 0.0), then we don't need to inspect the non-MCV part. But handling this in recursive manner breaks this entirely, because with something like (a=1) AND (b=1 OR b=2) you suddenly can have multiple matching items. Which makes the fullmatch flag somewhat useless. So I think we should be looking at top-level equality clauses only, just like number_of_groups() does. > There's also an interesting question around the NullTest clause. Since > NULLs are being recorded in the MCV list, shouldn't "IS NULL" be > treated as semantically like an equality clause, and cause that > attribute to be added to "eqmatches" if NULL is found in the MCV list? > > >> I've also realized that the "fullmatch" flag is somewhat confused, >> because some places interpreted it as "there is equality on each >> attribute" but in fact it also required an actual MCV match. > > Yes, I was having similar thoughts. I think "eqmatches" / "fullmatch" > probably just wants to track whether there was an exact comparison on > all the attributes, not whether or not the value was in the MCV list, > because the latter is already available in the "matches" bitmap. > Knowing that complete, exact comparisons happened, and it wasn't in > the MCV list, makes the "(1 - mcv_totalsel)) / otherdistinct" estimate > reasonable. > I think we can remove the fullmatch flag from mcv_update_bitmap entirely. All we need to know is the presence of equality clauses and whether there was a match in MCV (which we know from s1 > 0.0). > However, I don't think that tracking "eqmatches" or "fullmatch" is > sufficient for the general case. For example, for other operators like > "!=", "<", "<=", all (or maybe half) the "1 - mcv_totalsel" ought to > count towards the selectivity, plus possibly part of the MCV list > (e.g., for "<=", using the sum of the matching MCV frequencies plus > half the sum of the non-MCV frequencies might be reasonable -- c.f. > scalarineqsel()). For an OR clause, you might want to count the number > of non-MCV matches, because logically each one adds another "(1 - > mcv_totalsel)) / otherdistinct" to the total selectivity. It's not > immediately obvious how that can be made to fit into the current code > structure. Perhaps it could be made to work by tracking the overall > selectivity as it goes along. Or perhaps it could track the > count/proportion of non-MCV matches. > Yes, ignoring the non-equality clauses in 0002 is wrong - that's pretty much why it's WIP and not merged into 0001. thanks for the feedback -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services