Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1abXFx-000514-FP for pgsql-hackers@arkaria.postgresql.org; Thu, 03 Mar 2016 17:41:45 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1abXFw-0004M8-Na for pgsql-hackers@arkaria.postgresql.org; Thu, 03 Mar 2016 17:41:44 +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.84) (envelope-from ) id 1abXEa-0002pP-Te for pgsql-hackers@postgresql.org; Thu, 03 Mar 2016 17:40:21 +0000 Received: from mail-pf0-x232.google.com ([2607:f8b0:400e:c00::232]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1abXET-0000si-Vu for pgsql-hackers@postgresql.org; Thu, 03 Mar 2016 17:40:19 +0000 Received: by mail-pf0-x232.google.com with SMTP id 124so18615634pfg.0 for ; Thu, 03 Mar 2016 09:40:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=98EcRd5Bn9s3lrVW1TyYgY/2VWG/bejqBCtPcIZdALg=; b=evffPovQCNfQ+7SCcwTfDDgjS1eyE7tw2e0wQVoDTLI2Odna83XWj8fC04jwa92UXG rkMUrNyXTXIzGIctYxcYA1QacyzPNzevg3p8vdVFdUOF04eeSnmN4YHM5QrzSVz+6iqT rbFP+IAJSo7ccWDk+bULl7QvTYHNguA7tKIJTAKpFv09saG713umm5J43vFRjgjhvoI1 E11smjrEGFbGISJtGadxgcZEAuzbHXw2IX0szgU/tXEsm3QMg48mJdFgcWMomhNsmWwU F1egcBzhk0YLl8/l4OfCSB+M0EutFwo5ofeCKR/t7y9k58Mi0Q5O9U2RCkK30pvMEtcK ZkwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=98EcRd5Bn9s3lrVW1TyYgY/2VWG/bejqBCtPcIZdALg=; b=WmPz+ZnYefUPGXriKtWNmcncfbm26zcWdjvmW7+FAb4L6KJcx5YQLXj6O/LLy4GNGI I4Rg99kOY8Y9flBACbKQfGJjdvdmm0vgP/HnbBSmLaV+95BItpN4dz8UNHnhuWwd3Q+u 8V1ES1zQq2t/iRoe9Nsk/tEjRwAfqOyiigwcdTDko9bSj27yIk1sJq7qrNb8tXX3JxlG vecyILUlruArIoA+03Cd4JlqFFlY0fpI0lygbsQGhCxTRagbnyDBuOl5My4rQcbp5EmF hJ+bYYmwhwrh/1H5wK6jaSQkLAnjgYlCu/rlypxVmg0trR5J2gbyxI8tNAMLna899jhq cVxw== X-Gm-Message-State: AD7BkJJXJ9Cnauf0zqh9G6K9pxOKiJ8HKs4WT3EEkR186ACvcIQZO8AcmbTdboZQ/4YDaQ== X-Received: by 10.98.32.18 with SMTP id g18mr5309604pfg.116.1457026812273; Thu, 03 Mar 2016 09:40:12 -0800 (PST) Received: from [192.168.2.121] (204-195-49-243.wavecable.com. [204.195.49.243]) by smtp.gmail.com with ESMTPSA id 85sm61969384pfl.18.2016.03.03.09.40.10 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 03 Mar 2016 09:40:11 -0800 (PST) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 9.2 \(3112\)) Subject: Re: improving GROUP BY estimation From: Mark Dilger In-Reply-To: <56D867C3.3040309@2ndquadrant.com> Date: Thu, 3 Mar 2016 09:40:04 -0800 Cc: Alexander Korotkov , PostgreSQL-development Content-Transfer-Encoding: quoted-printable Message-Id: <904EB795-BCDD-4D68-8D44-274B40C4B2E9@gmail.com> References: <56CD0381.5060502@2ndquadrant.com> <03A3B58D-6A2D-44FE-9B03-E21948CFCBAA@gmail.com> <56CFA373.3060105@2ndquadrant.com> <7D1A65E8-7ED8-4268-9C24-8EA07E95D2BC@gmail.com> <56CFCE96.5080306@2ndquadrant.com> <56D867C3.3040309@2ndquadrant.com> To: Tomas Vondra X-Mailer: Apple Mail (2.3112) X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org > On Mar 3, 2016, at 8:35 AM, Tomas Vondra w= rote: >=20 > Hi, >=20 > On 03/03/2016 12:53 PM, Alexander Korotkov wrote: >> Hi, Tomas! >>=20 >> I've assigned to review this patch. >>=20 >> I've checked version estimate-num-groups-v2.txt by Mark Dilger. >> It applies to head cleanly, passes corrected regression tests. >>=20 >> About correlated/uncorrelated cases. I think now optimizer mostly assumes >> all distributions to be independent. I think we should follow this >> assumption in this case also until we have fundamentally better option (= like >> your multivariate statistics). >>=20 >> @@ -3438,9 +3438,9 @@ estimate_num_groups(PlannerInfo *root, List *group= Exprs, >> double input_rows, >> reldistinct =3D clamp; >>=20 >> /* >> -* Multiply by restriction selectivity. >> +* Estimate number of distinct values expected in given number of rows. >> */ >> -reldistinct *=3D rel->rows / rel->tuples; >> +reldistinct *=3D (1 - powl((reldistinct - 1) / reldistinct, rel->rows)); >>=20 >> /* >> * Update estimate of total distinct groups. >>=20 >> I think we need way more explanation in comments here (possibly with >> external links). For now, it looks like formula which appears from nowhe= re. >=20 > I thought the details (particularly the link to stackexchange, discussing= the formula) would be enough, but let me elaborate. >=20 > The current formula >=20 > reldistinct *=3D rel->rows / rel->tuples; >=20 > simply multiplies the ndistinct estimate with selectivity. So when you se= lect 1% of the table, the estimate says we'll see 1% of ndistinct values. B= ut clearly that's naive, because for example when you have 10k distinct val= ues and you select 10M rows, you should expect nearly all of them in the sa= mple. The current formula assumes total dependence between the columns. You can create tables where this is true, and the formula gives precisely the right estimate. I'm not claiming this is common, or that it should be the default assumption, but it is one end of the spectrum of possible correct estimates. > And that's what the formula does - it gives you the expected number of di= stinct values, when selecting 'k' values from 'd' distinct values with repl= acements: >=20 > count(k, d) =3D d * (1 - ((d-1)/d) ^ k) >=20 > It's assuming the distinct values are equally probable (uniform distribut= ion) and that the probabilities do not change (that's the "with replacement= "). The new formula assumes total independence between the columns. You can likewise create tables where this is true, and you did so upthread, and for those tables it gives precisely the right estimate. This is the other end of the spectrum of possible correct estimates. In the absence of multivariate statistics, either because you haven't committed that work yet, or because the multivariate data the planner needs hasn't been collected, choosing an estimate exactly in the middle of the spectrum (whatever that would mean mathematically) would minimize the maximum possible error in the estimate. I have the sense that my point of view is in the minority, because the expectation is the problems caused by independence assumptions will only be fixed when multivariate statistics are implemented and available, and so we should just keep the independence assumptions everywhere until that time. I tend to disagree with that, on the grounds that even when that work is finished, we'll never have complete coverage of every possible set of columns and what their degree of dependence is. Perhaps I am badly mistaken. Can somebody more familiar with the issues than I am please disabuse me of my wrongheadedness? Mark Dilger --=20 Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers