Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1aZ69d-0006nY-JC for pgsql-hackers@arkaria.postgresql.org; Fri, 26 Feb 2016 00:21:09 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1aZ69c-0005VA-Mw for pgsql-hackers@arkaria.postgresql.org; Fri, 26 Feb 2016 00:21:08 +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 1aZ68G-0003x1-Lo for pgsql-hackers@postgresql.org; Fri, 26 Feb 2016 00:19:44 +0000 Received: from mail-pa0-x22a.google.com ([2607:f8b0:400e:c03::22a]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1aZ68E-0003WF-9A for pgsql-hackers@postgresql.org; Fri, 26 Feb 2016 00:19:43 +0000 Received: by mail-pa0-x22a.google.com with SMTP id fl4so40688697pad.0 for ; Thu, 25 Feb 2016 16:19:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=Ala0YG2c7Mm8WsSQB5PJRSrSW1uLdzzBQEna854Zqo8=; b=CqmamB+E1jd8MJ13+4pfDXwhr8TgFXQsb1a0BAknERBAaydiIXk8wxZORxuyFDN5pQ dCNBK+puPGCHb8ZPzeCPQM+jbKoVzD9XkAyTyzywdbtIN6WV6bfYtFF1/hD6nji3JAF/ Q4M6WZkGaRptWk7cIM1OSrL0YF1S5wOy8vPG7a6HzYX0TkddRPBOBZpLcWtU/mAuPTSG EayPUHbzM6QYXK8wk5FBi5M5nALcZVVdwdVN+/enITNFf/yEIT5zkfTEA03ydxe82zrR B1lvEfzk7RkkQs/rVffyS+bB6iCVmOrcjOX9hNaXFll3qNdijfZJaVoY34L2WoGvp37K xjgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:content-type:mime-version:subject:from :in-reply-to:date:cc:content-transfer-encoding:message-id:references :to; bh=Ala0YG2c7Mm8WsSQB5PJRSrSW1uLdzzBQEna854Zqo8=; b=KaQxcrINYxNqgydIfpQZ8IY//PWNoh8zgjN/4wGA7acmU9PYxPI8Hm9xCB04TP9y/y k6fAvitHdD+SgyebVZPnh1xaMrPMlazVYJlIVwgNGsO7lcUR8/U1kfAVUV7HUtAa5SPE 1mr8IJY5Ig5pddEVRIpIuKiGhfS1qorgFRVkbwhTiFTSOZr+Qj/W47JBqg4/nJQ4Y6ZT KZ3V3J7kN58yPVU0P7b3bvRKde4xuI5RPDpV92pS1Oqr3aueqwhMrORvIqBxZ25oaSYA gYwmHZVoItwNj2WGMMx9PwC2AoQ+92o5gg2S4MiHGpatXbCxPq9YONcGXn6TXi0ZEV40 a1yQ== X-Gm-Message-State: AG10YOTCzfTjYupcMy5uhysnNO1/FpSc5dtYdWZm0XvKbcZNFQURKe++GxXxvOUT0ARrRw== X-Received: by 10.66.167.237 with SMTP id zr13mr67922815pab.85.1456445980786; Thu, 25 Feb 2016 16:19:40 -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 h66sm14644506pfd.91.2016.02.25.16.19.39 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 25 Feb 2016 16:19:40 -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: <03A3B58D-6A2D-44FE-9B03-E21948CFCBAA@gmail.com> Date: Thu, 25 Feb 2016 16:19:39 -0800 Cc: PostgreSQL-development Content-Transfer-Encoding: quoted-printable Message-Id: References: <56CD0381.5060502@2ndquadrant.com> <03A3B58D-6A2D-44FE-9B03-E21948CFCBAA@gmail.com> To: Tomas Vondra X-Mailer: Apple Mail (2.3112) X-Pg-Spam-Score: -2.0 (--) 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 Feb 25, 2016, at 3:16 PM, Mark Dilger wrote: >=20 >=20 >> On Feb 23, 2016, at 5:12 PM, Tomas Vondra = wrote: >>=20 >> >>=20 >> So much better. Clearly, there are cases where this will over-estimate t= he cardinality - for example when the values are somehow correlated. >>=20 >=20 > I applied your patch, which caused a few regression tests to fail. Attac= hed > is a patch that includes the necessary changes to the expected test resul= ts. >=20 > It is not hard to write test cases where your patched version overestimat= es > the number of rows by a very similar factor as the old code underestimates > them. My very first test, which was not specifically designed to demonst= rate > this, happens to be one such example: >=20 >=20 > CREATE TABLE t (a INT, b int); > INSERT INTO t SELECT sqrt(gs)::int, gs FROM generate_series(1,10000000) g= s; > ANALYZE t; > EXPLAIN SELECT a FROM t WHERE b < 1000 GROUP BY a; > QUERY PLAN > --------------------------------------------------------------- > HashAggregate (cost=3D169250.21..169258.71 rows=3D850 width=3D4) > Group Key: a > -> Seq Scan on t (cost=3D0.00..169247.71 rows=3D1000 width=3D4) > Filter: (b < 1000) > (4 rows) >=20 > SELECT COUNT(*) FROM (SELECT a FROM t WHERE b < 1000 GROUP BY a) AS ss; > count=20 > -------=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20 > 32 > (1 row) >=20 >=20 >=20 > So, it estimates 850 rows where only 32 are returned . Without applying = your patch, > it estimates just 1 row where 32 are returned. That's an overestimate of= roughly 26 times, > rather than an underestimate of 32 times. >=20 > As a patch review, I'd say that your patch does what you claim it does, a= nd it applies > cleanly, and passes the regression tests with my included modifications. = I think there > needs to be some discussion on the list about whether the patch is a good= idea. >=20 > Mark Dilger >=20 >=20 > Assuming the goal is to minimize the degree to which the estimates are inac= curate, I=20 get better results by a kind of averaging of the old values from the curren= t code base and the new values from Tomas's patch. I tried this and at least for the f= ew examples we've been throwing around, I found the results were not as wildly inaccura= te in the worst case examples than in either of the other two approaches: diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfu= ncs.c index 46c95b0..c83135a 100644 --- a/src/backend/utils/adt/selfuncs.c +++ b/src/backend/utils/adt/selfuncs.c @@ -3414,6 +3414,8 @@ estimate_num_groups(PlannerInfo *root, List *groupExp= rs, double input_rows, Assert(rel->reloptkind =3D=3D RELOPT_BASEREL); if (rel->tuples > 0) { + double old_factor; /* The way = it is currently done on master */ + double new_factor; /* The way = Tomas Vondra proposes doing it */ /* * Clamp to size of rel, or size of rel / 10 if mul= tiple Vars. The * fudge factor is because the Vars are probably co= rrelated but we @@ -3440,7 +3442,10 @@ estimate_num_groups(PlannerInfo *root, List *groupEx= prs, double input_rows, /* * Multiply by restriction selectivity. */ - reldistinct *=3D rel->rows / rel->tuples; + old_factor =3D rel->rows / rel->tuples; /= * old way */ + new_factor =3D (1 - powl((reldistinct - 1) / reldis= tinct, rel->rows)); /* Tomas's way */ + + reldistinct *=3D sqrt(old_factor * new_factor); /= * average of old way and new way, sort of */ =20 /* * Update estimate of total distinct groups. --=20 Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers