Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1alFmx-0004D5-EA for pgsql-hackers@arkaria.postgresql.org; Wed, 30 Mar 2016 13:03:59 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1alFmw-0001Xm-1v for pgsql-hackers@arkaria.postgresql.org; Wed, 30 Mar 2016 13:03:58 +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 1alFmt-0001WI-I4 for pgsql-hackers@postgresql.org; Wed, 30 Mar 2016 13:03:55 +0000 Received: from mail-pf0-x233.google.com ([2607:f8b0:400e:c00::233]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1alFml-0007DV-IR for pgsql-hackers@postgresql.org; Wed, 30 Mar 2016 13:03:53 +0000 Received: by mail-pf0-x233.google.com with SMTP id n5so42826084pfn.2 for ; Wed, 30 Mar 2016 06:03:47 -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=FySJTVXw1fCbOTdzyOyhqO23AK9U4yoyuw34lZpG3zA=; b=Tc633xiw8uf82bAGBj0LzwBu+fkoFBwqhsOZeEsKc3l74YIDrai1xZvjkwRPO+3OJx PIfD24uynBk5BroIjs7h+nghvTEA/rYuzYrBKbAl6la20tlq+Vh8K1US+Fp7kwY5tgKc 9hKH4CFdYqV/QjTgX8GU2gxiP3eNSeWekc0Zpf5C+fd4sWnNMEAlJXKtSjEwtOZ0WUbj s2yNh139W7t5Ohq/G5JhXXzQuvRl5YXevbL1NeSOkrW9kuBjKuUOh9xbgXuLoX+DgmZc 6nhNi3pQnrR0vyGJ74D65nJb3TcBKE02TUH++0Vc4V8NMyX5OutJ4H+hZwuPOOvvFkCV rEzQ== 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=FySJTVXw1fCbOTdzyOyhqO23AK9U4yoyuw34lZpG3zA=; b=GYaNZUPm3Q6HUZf3TG72h5sCC8gtyqYRUXAbvcWjMSH2+woW4dcdbRXHtue2L7pABf yKU85+1Bjou8AMgsQqXx+HoypWNLJ5g0TJWHEkSis/t2WZ0wR0rhlpBWUS/b5e/o0vmG VfRlddEP7E4sknbvnWrbGPCe+SAcLz+siDEzN9LocYNQPVZ3frs6p0MciK9A2VUriNpq 7E7jBcPMLqQvchNnT1W922ZwZ2rL4YqjoCKzddAm2NKt7l0SnrVsdgXjnAjD2tI/mLnY 9KnLKMwMYB86AXXRNv9VNodrhou2oRLjoky7SAksAMqWuw/lF0kl4y0fgwY1J3aPwyg3 pPjw== X-Gm-Message-State: AD7BkJIzdIja9f5xbNEJQRIdEMfqd33cOeSQL1o2k64cPFOKBQnYMt462zdo9HzvXUQPwp2H X-Received: by 10.98.69.193 with SMTP id n62mr12838731pfi.46.1459343026351; Wed, 30 Mar 2016 06:03:46 -0700 (PDT) Received: from [10.137.2.14] ([61.8.221.8]) by smtp.gmail.com with ESMTPSA id qh8sm5962847pac.40.2016.03.30.06.03.43 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 30 Mar 2016 06:03:45 -0700 (PDT) Subject: Re: improving GROUP BY estimation To: Alexander Korotkov 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> <715c5c66-5fe1-a9aa-e28d-6c4114ba0fd6@2ndquadrant.com> Cc: Mark Dilger , PostgreSQL-development , Dean Rasheed From: Tomas Vondra Message-ID: <3457190a-29df-8ecc-6208-a780b8faeb64@2ndquadrant.com> Date: Wed, 30 Mar 2016 15:03:41 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:47.0) Gecko/20100101 Thunderbird/47.0a2 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------0727417844B022F84AA1AB58" 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. --------------0727417844B022F84AA1AB58 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Hi, On 03/22/2016 03:40 PM, Alexander Korotkov wrote: > > I think you should send a revision of patch including comments proposed > by Deam Rasheed. > I'm switching patch status to waiting on author in commitfest. > Attached is v4 of the patch - the only difference w.r.t. v3 is that I've used the comment as proposed by Dean. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --------------0727417844B022F84AA1AB58 Content-Type: binary/octet-stream; name="estimate-num-groups-v4.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="estimate-num-groups-v4.patch" diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c index a6555e9..00755da 100644 --- a/src/backend/utils/adt/selfuncs.c +++ b/src/backend/utils/adt/selfuncs.c @@ -3439,9 +3439,20 @@ estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows, reldistinct = clamp; /* - * Multiply by restriction selectivity. + * Update the estimate based on the restriction selectivity. + * + * This uses the formula for the expected number of distinct values + * when selecting with replacement from a collection where each of + * the distinct values occurs an equal number of times (a uniform + * distribution of values). This is a very close approximation to + * the more correct method of selecting without replacement when + * the number of distinct values is quite large --- for example, + * see http://www.adellera.it/investigations/distinct_balls/. + * Additionally, it also turns out to work very well even when the + * number of distinct values is small. */ - 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); --------------0727417844B022F84AA1AB58 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 --------------0727417844B022F84AA1AB58--