Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1agiRz-0001M4-SD for pgsql-hackers@arkaria.postgresql.org; Fri, 18 Mar 2016 00:39:36 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1agiRz-0005DO-Eu for pgsql-hackers@arkaria.postgresql.org; Fri, 18 Mar 2016 00:39:35 +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_2) (envelope-from ) id 1agiQX-0003Yc-IG for pgsql-hackers@postgresql.org; Fri, 18 Mar 2016 00:38:05 +0000 Received: from mail-wm0-x22b.google.com ([2a00:1450:400c:c09::22b]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1agiQU-0000po-FU for pgsql-hackers@postgresql.org; Fri, 18 Mar 2016 00:38:04 +0000 Received: by mail-wm0-x22b.google.com with SMTP id p65so48273490wmp.1 for ; Thu, 17 Mar 2016 17:38:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:cc:from:message-id:date:user-agent :mime-version:in-reply-to; bh=JzYn3h0UvyKeThNeYR7ncw1xUjmN4drc8blj6BDp0Zg=; b=Bshg+GS9En6Nt03JMdfyGl84F+Ao4pLc3FXvm/BJeb1neP3im6EtkJ9aR/LF7XPQul qV8MO74TyKwWSdIGmZZeMmtD60/7QZV2XkyFKNBH7AQC6BJZKc2MA03Y7GXzmPl9PyeK 2tYELo6cT0FZEh+BgEEbUA8aZO6p96v66qz4lSr3yQOX3gLdCj4QOkeCIGtzIsYwh+6y eB6zLSuVcw8IsV3JVWL6rYeBeCYfY6rt1VwcFas7zYC1Ny84GX4nr5luzlT58sDkcTRr 84iW5ZVgEVGi86RL6T2V+ADG7JTp2uPxAdBhyHRMRhwrC8mLWXSqOpKbp0L9/OsNHfIZ hhqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:to:references:cc:from:message-id:date :user-agent:mime-version:in-reply-to; bh=JzYn3h0UvyKeThNeYR7ncw1xUjmN4drc8blj6BDp0Zg=; b=TxfdY3k746wZ9YD88cwNwRIUhxetgcljpPv1XCdR4tp/MAvw9bgfeeTZecRthO8Qcz TmUYn4ijtf1X4oLmubQ03oXXXFMhmkKbVpYuE5s9TrJnBt36CSqwosBdJOY6WY6X18RM Os2b5B/CDiG8eb2wPg1/O7JRaKPIgZ8vZyV43ad9RHPlizmdH3fdmiYAVbztxAUFLpaw 28dT5UyLxnyA/kMMeNXhhrgDTu6WnW5Z5UlJH9puH378y9I7AucSbzknwJiXo/pAJhbj GA3S4baJ3W3ovRhsXCxmD5dMpqRI9JnX325bvq2HZAsLkv6mFqUDDQzNb3a0Rnef2qbP OaEQ== X-Gm-Message-State: AD7BkJJITFeX0IefmzOOA+1UX6jJq0kJP+4OTa8YfdiPDKkK7xAyo9fq5avfs4taN3Dzp07YAdMotM9TGmJuv/LjK2Y73dRZNvRcqNbjzWUkt8RE35GuBYhGu9u5npA5/9z406giculGJDRBzZxeCtDht6ii7awc+JdZPyg129r++hQh0oPHLBON8xz+MMNtf4e5AjXmIITz X-Received: by 10.28.143.9 with SMTP id r9mr37618492wmd.37.1458261480911; Thu, 17 Mar 2016 17:38:00 -0700 (PDT) Received: from [10.137.2.17] (ip-78-45-216-26.net.upcbroadband.cz. [78.45.216.26]) by smtp.gmail.com with ESMTPSA id ei9sm9879972wjd.40.2016.03.17.17.37.59 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 17 Mar 2016 17:37:59 -0700 (PDT) Subject: Re: improving GROUP BY estimation To: Dean Rasheed 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> <904EB795-BCDD-4D68-8D44-274B40C4B2E9@gmail.com> <56D88D93.5000509@2ndquadrant.com> <1457097036.24980.28.camel@2ndquadrant.com> <1457906984.27231.16.camel@2ndquadrant.com> Cc: Mark Dilger , Alexander Korotkov , PostgreSQL-development From: Tomas Vondra Message-ID: <715c5c66-5fe1-a9aa-e28d-6c4114ba0fd6@2ndquadrant.com> Date: Fri, 18 Mar 2016 01:37:56 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:46.0) Gecko/20100101 Thunderbird/46.0a2 MIME-Version: 1.0 In-Reply-To: <1457906984.27231.16.camel@2ndquadrant.com> Content-Type: multipart/mixed; boundary="------------6FE268C615142A79024ABE54" X-Pg-Spam-Score: -2.6 (--) 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 This is a multi-part message in MIME format. --------------6FE268C615142A79024ABE54 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit On 03/13/2016 11:09 PM, Tomas Vondra wrote: > Hi, > > On Sun, 2016-03-13 at 15:24 +0000, Dean Rasheed wrote: >> On 4 March 2016 at 13:10, Tomas Vondra >> wrote: >>> ... >>> >> >> I think that a better formula to use would be >> >> reldistinct *= (1 - powl(1 - rel-rows / rel->tuples, rel->tuples / >> reldistinct) Attached is a v3 of the patch using this formula instead of the original one. Interestingly, that apparently reduces the number of regression tests that get broken to a single one. I'm not sure whether we need to provide a link to the PDF the formula comes from - perhaps we should? I've also repeated the tests for the two tables (dependent and independent columns), comparing the actual number of groups and different estimates, and the results look like this (v3 is the formula used in this patch): 1) independent | 10 | 50 | 100 | 500 | 1000 | 5000 --------------------------------------------------------- actual | 919 | 3829 | 6244 | 9944 | 10001 | 10001 current | 10 | 50 | 102 | 516 | 1018 | 4996 new (v1) | 973 | 4001 | 6382 | 9897 | 9951 | 9951 new (v3) | 1117 | 3852 | 6229 | 9943 | 10004 | 10004 2) dependent | 10 | 50 | 100 | 500 | 1000 | 5000 -------------------------------------------------------- actual | 10 | 50 | 100 | 500 | 1000 | 5000 current | 10 | 53 | 105 | 508 | 1016 | 5014 new (v1) | 880 | 4105 | 6472 | 9955 | 10018 | 10018 new (v3) | 807 | 3680 | 6050 | 9916 | 9983 | 9983 I only collected numbers for the new estimator, the other numbers are just a copy from the previous message. So there might be minor differences due to slightly different ndistinct estimates etc. Anyway, the numbers are obviously quite close to the formula from v1 of the patch, plus the formula gives better estimates when scanning nearly all rows. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --------------6FE268C615142A79024ABE54 Content-Type: binary/octet-stream; name="estimate-num-groups-v3.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="estimate-num-groups-v3.patch" diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c index d396ef1..3630469 100644 --- a/src/backend/utils/adt/selfuncs.c +++ b/src/backend/utils/adt/selfuncs.c @@ -3439,9 +3439,12 @@ estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows, reldistinct = clamp; /* - * Multiply by restriction selectivity. + * Estimate the number of groups observed in the expected number of + * rows, using a formula for selection without replacement, assuming + * uniform distribution. */ - reldistinct *= rel->rows / rel->tuples; + reldistinct *= (1 - powl(1 - rel->rows / rel->tuples, + rel->tuples / reldistinct)); /* * Update estimate of total distinct groups. diff --git a/src/test/regress/expected/subselect.out b/src/test/regress/expected/subselect.out index de64ca7..0fc93d9 100644 --- a/src/test/regress/expected/subselect.out +++ b/src/test/regress/expected/subselect.out @@ -807,27 +807,24 @@ select * from int4_tbl where explain (verbose, costs off) select * from int4_tbl o where (f1, f1) in (select f1, generate_series(1,2) / 10 g from int4_tbl i group by f1); - QUERY PLAN ----------------------------------------------------------------------- - Hash Join + QUERY PLAN +---------------------------------------------------------------- + Hash Semi Join Output: o.f1 Hash Cond: (o.f1 = "ANY_subquery".f1) -> Seq Scan on public.int4_tbl o Output: o.f1 -> Hash Output: "ANY_subquery".f1, "ANY_subquery".g - -> HashAggregate + -> Subquery Scan on "ANY_subquery" Output: "ANY_subquery".f1, "ANY_subquery".g - Group Key: "ANY_subquery".f1, "ANY_subquery".g - -> Subquery Scan on "ANY_subquery" - Output: "ANY_subquery".f1, "ANY_subquery".g - Filter: ("ANY_subquery".f1 = "ANY_subquery".g) - -> HashAggregate - Output: i.f1, (generate_series(1, 2) / 10) - Group Key: i.f1 - -> Seq Scan on public.int4_tbl i - Output: i.f1 -(18 rows) + Filter: ("ANY_subquery".f1 = "ANY_subquery".g) + -> HashAggregate + Output: i.f1, (generate_series(1, 2) / 10) + Group Key: i.f1 + -> Seq Scan on public.int4_tbl i + Output: i.f1 +(15 rows) select * from int4_tbl o where (f1, f1) in (select f1, generate_series(1,2) / 10 g from int4_tbl i group by f1); --------------6FE268C615142A79024ABE54 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers --------------6FE268C615142A79024ABE54--