Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1j2itx-0005gt-4W for pgsql-hackers@arkaria.postgresql.org; Fri, 14 Feb 2020 21:53:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1j2itw-0005cA-01 for pgsql-hackers@arkaria.postgresql.org; Fri, 14 Feb 2020 21:53:31 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1j2itv-0005c2-HA for pgsql-hackers@lists.postgresql.org; Fri, 14 Feb 2020 21:53:31 +0000 Received: from mail-pl1-x644.google.com ([2607:f8b0:4864:20::644]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1j2its-0004t7-UK for pgsql-hackers@postgresql.org; Fri, 14 Feb 2020 21:53:30 +0000 Received: by mail-pl1-x644.google.com with SMTP id b22so4194861pls.12 for ; Fri, 14 Feb 2020 13:53:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=j-davis-com.20150623.gappssmtp.com; s=20150623; h=message-id:subject:from:to:cc:date:in-reply-to:references :mime-version:content-transfer-encoding; bh=fPZ1mT4Bew4ouzGor8q47PYMoIsiPFhXkvqXQ57KBOA=; b=v8niv8kWv5IhDl4m7g2hVbeQLBnSa0/0YevUBH3yBABBk1gxg8hwkYl6uVi4o3/Tw3 hiibyxncdBZTvUSxz/0jwml4qPYX61CdrxGxXM6QiBlACUQO5w8YkxX/B3uemKlYSu1a fFUs5QmjkE4+uVykjZYANddmwERqGbsOqurhH5jK0e6Zoq7tqHR0VZCgkwWymzjV6TFE dR7Ph8+XkbIYfL/brEy0daNhYupdIGcASGDCXBz9qTPShCVE605Vy57KNouXIe1CmQq0 1TQ+fMDYtJj+eJnikvry+dMGvERzUdAO7u8dXI+yDV0Ee77pNO+VJw9YNugW7vQuXmKN OAnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:cc:date:in-reply-to :references:mime-version:content-transfer-encoding; bh=fPZ1mT4Bew4ouzGor8q47PYMoIsiPFhXkvqXQ57KBOA=; b=QSBBB2JRhpFd1dK+QaTE0A7Ec2D6GcLYSKiAOlvUXvYuE7WJiSFX2KBlfW/0Jyurne y/PiFp6W9uFc5v/1nDmnrJ9vlFuVwvr8qYnPGEF0pjJ5+QmmQseInPbCcGAcBA4it6f4 YXBtVShE2sre76lzUk4XgFudzEE2x9DOVndo0w0tkyYXIIVEYa++6E7r4vUJcxBPFkSq Z9uIf9Tva87WBDcG7sU/y50l3c7omJQfee8/WivTXWOJ0TpN/mmo5IF50XZE2sVtNysv z1r7voUjLmKw5eaczbEv3UCeikBIZz15e3UwAYlsc8b8LB/d34cA5hPewZR6xljmYPPg GafA== X-Gm-Message-State: APjAAAUBWWF1pgE/uso8o+fte/rxXdMFmvb6LaIYzkQl5CbCNAM+q5Ba 90mc7q6CY0UdI5ktzRkmIppvnQ== X-Google-Smtp-Source: APXvYqzIgllEXGpvuyHm5gXAXV/hI2PqAWXxwMRQ6gjXSasPwUROpKrdP9mfruKW1Awg3p5YJ8ew+Q== X-Received: by 2002:a17:90b:243:: with SMTP id fz3mr6240634pjb.29.1581717206220; Fri, 14 Feb 2020 13:53:26 -0800 (PST) Received: from jdavis ([2600:1010:b06c:925d:6458:80b:898f:69bc]) by smtp.gmail.com with ESMTPSA id t11sm7881477pgi.15.2020.02.14.13.53.24 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 14 Feb 2020 13:53:25 -0800 (PST) Message-ID: <00a0ce96e20119fc7e35e773a2bbc161ef3a206c.camel@j-davis.com> Subject: Re: Memory-Bounded Hash Aggregation From: Jeff Davis To: Peter Geoghegan Cc: Heikki Linnakangas , Tomas Vondra , Taylor Vesely , Adam Lee , Melanie Plageman , PostgreSQL Hackers Date: Fri, 14 Feb 2020 13:53:22 -0800 In-Reply-To: <3bed775951a72dbdf8446c6750936e38dcf107b9.camel@j-davis.com> References: <507ac540ec7c20136364b5272acbcd4574aa76ef.camel@j-davis.com> <20190703001753.s5e5cme3ucjgeu6c@development> <20190711155520.zqipi23o3yybx7ym@development> <9be86fc1adc315f69b8af4b379087ab451008d8a.camel@j-davis.com> <20190802064405.GA94036@mars.local> <617fea8258ba3df7bc8b21cd7f3afc219f2383d9.camel@j-davis.com> <20190802152123.ichmacgx3y3x7dc3@development> <20191214173225.3kzsbncr6brpxdll@development> <775d754c-9a61-5363-6998-f87d15f1b592@iki.fi> <6e2b4a4475a690fa515829950aeed935696b0df3.camel@j-davis.com> <97c46a59c27f3c38e486ca170fcbc618d97ab049.camel@j-davis.com> <8bf8d5f77db49cc1f032b6c7c0cfe0d6da89d64a.camel@j-davis.com> <7e58d8c57690373d66e0a07e8cf683f7d141c74b.camel@j-davis.com> <3bed775951a72dbdf8446c6750936e38dcf107b9.camel@j-davis.com> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.28.5-0ubuntu0.18.04.1 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Wed, 2020-02-12 at 21:51 -0800, Jeff Davis wrote: > On Mon, 2020-02-10 at 15:57 -0800, Jeff Davis wrote: > > Attaching latest version (combined logtape changes along with main > > HashAgg patch). > > I ran a matrix of small performance tests to look for regressions. I ran some more tests, this time comparing Hash Aggregation to Sort+Group. Summary of trends: group key complexity : favors Hash group key size : favors Hash group size : favors Hash higher work_mem : favors Sort[1] data set size : favors Sort[1] number of aggregates : favors Hash[2] [1] I have closed the gap a bit with some post-experiment tuning. I have just begun to analyze this case so I think there is quite a bit more room for improvement. [2] Could use more exploration -- I don't have an explanation. Data sets: t20m_1_int4: ~20 million groups of size ~1 (uniform) t1m_20_int4: ~1 million groups of size ~20 (uniform) t1k_20k_int4: ~1k groups of size ~20k (uniform) also, text versions of each of those with collate "C.UTF-8" Results: 1. A general test to vary the group size, key type, and work_mem. Query: select i from $TABLE group by i offset 100000000; work_mem='4MB' +----------------+----------+-------------+--------------+ | | sort(ms) | hashagg(ms) | sort/hashagg | +----------------+----------+-------------+--------------+ | t20m_1_int4 | 11852 | 10640 | 1.11 | | t1m_20_int4 | 11108 | 8109 | 1.37 | | t1k_20k_int4 | 8575 | 2732 | 3.14 | | t20m_1_text | 80463 | 12902 | 6.24 | | t1m_20_text | 58586 | 9252 | 6.33 | | t1k_20k_text | 21781 | 5739 | 3.80 | +----------------+----------+-------------+---- ----------+ work_mem='32MB' +----------------+----------+-------------+--------------+ | | sort(ms) | hashagg(ms) | sort/hashagg | +----------------+----------+-------------+--------------+ | t20m_1_int4 | 9656 | 11702 | 0.83 | | t1m_20_int4 | 8870 | 9804 | 0.90 | | t1k_20k_int4 | 6359 | 1852 | 3.43 | | t20m_1_text | 74266 | 14434 | 5.15 | | t1m_20_text | 56549 | 10180 | 5.55 | | t1k_20k_text | 21407 | 3989 | 5.37 | +----------------+----------+-------------+--------------+ 2. Test group key size data set: 20m rows, four int4 columns. Columns a,b,c are all the constant value 1, forcing each comparison to look at all four columns. Query: select a,b,c,d from wide group by a,b,c,d offset 100000000; work_mem='4MB' Sort : 30852ms HashAgg : 12343ms Sort/HashAgg : 2.50 In theory, if the first grouping column is highly selective, then Sort may have a slight advantage because it can look at only the first column, while HashAgg needs to look at all 4. But HashAgg only needs to perform this calculation once and it seems hard enough to show this in practice that I consider it an edge case. In "normal" cases, it appears that more grouping columns significantly favors Hash Agg. 3. Test number of aggregates Data Set: same as for test #2 (group key size). Query: select d, count(a),sum(b),avg(c),min(d) from wide group by d offset 100000000; work_mem='4MB' Sort : 22373ms HashAgg : 17338ms Sort/HashAgg : 1.29 I don't have an explanation of why HashAgg is doing better here. Both of them are using JIT and essentially doing the same number of advancements. This could use more exploration, but the effect isn't major. 4. Test data size Data 400 million rows of four random int8s. Group size of one. Query: select a from t400m_1_int8 group by a offset 1000000000; work_mem='32MB' Sort : 300675ms HashAgg : 560740ms Sort/HashAgg : 0.54 I tried increasing the max number of partitions and brought the HashAgg runtime down to 481985 (using 1024 partitions), which closes the gap to 0.62. That's not too bad for HashAgg considering this is a group size of one with a simple group key. A bit more tuning might be able to close the gap further. Conclusion: HashAgg is winning in a lot of cases, and this will be an important improvement for many workloads. Not only is it faster in a lot of cases, but it's also less risky. When an input has unknown group size, it's much easier for the planner to choose HashAgg -- a small downside and a big upside. Regards, Jeff Davis