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 1irALv-0007ti-AK for pgsql-general@arkaria.postgresql.org; Tue, 14 Jan 2020 00:46:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1irALu-0007Tn-2x for pgsql-general@arkaria.postgresql.org; Tue, 14 Jan 2020 00:46:38 +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 1irALt-0007Tg-NM for pgsql-general@lists.postgresql.org; Tue, 14 Jan 2020 00:46:37 +0000 Received: from mail-pf1-x434.google.com ([2607:f8b0:4864:20::434]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1irALm-0002U1-OL for pgsql-general@lists.postgresql.org; Tue, 14 Jan 2020 00:46:37 +0000 Received: by mail-pf1-x434.google.com with SMTP id w62so5704189pfw.8 for ; Mon, 13 Jan 2020 16:46:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=I7gU0sGWD/U7BvEajRvz1cg9RayM2onsWhs5uSVm7T0=; b=YHMUoYfrPtdmkxijdpur0oVFm5lzK5XQrgXHofTjAOoMr0BgXKOb4Sm5MqzpY6BHd3 c1xFjk8+yRsGsZF5ocpEXUq0pL0FRkCPism9yf/ImAsSPIfUz0rlvbSuI3l27S/Dqf4x 6dzhC1DEhGC2+Yqzfz9yCpMW6FYQ+DJoRCr+qINV4T7m5UuzzO8GYP4wxQy4J+5AqzNx qGDabhsvrRD0HUdGikzwoQBs4uOqejnTP0+yaU11DSgICgUbBFtQOlg9KjBH6xMe19s3 Huftb+JPcESK2k6sZCw7ZV33vU3xJks86R4wpCQSSpS9O1YiQ+s139q1DKVla2Dafij6 7l6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=I7gU0sGWD/U7BvEajRvz1cg9RayM2onsWhs5uSVm7T0=; b=dt/qe10azcJZJMBTfmmkJq+wteIvW20iXHh1/nN1jjhjqhUNLeA70rbBvRpbB080qi lz+PzHQqzJTGgw8Xcga1izgDS5FJKD/VgUD3gNZvR42TiTopZyYW9WfHNA7YqwmNjHIN 0/ewVikISSCQxhTguGvjt95OUZInWcQUTz6E8CSxIuxHfIaDF0OvHozVb80p97ztbAhj r0oklPhabtSlpsE/u+H6xC2Sg14rDQmixHsTfjSGK3H3EpRfDsgul97od0aUDvr759d9 R7y2p7LOyYPgP84XYvGQOBrbQCM0S5Ve0oKWlNMC1PyA6o+IBW4pi1L4HgFdeNYIRmMP 8/6w== X-Gm-Message-State: APjAAAU4sB9P1DvcfsajizY+Q9kDiRoroBCOkNauqUQcQnjPZl64mnrn vNcfMHdjefPmjF6+QCGmZBQ= X-Google-Smtp-Source: APXvYqyEIcpKOcZNNnkwZHhCEr5L+WBLRkJ49zOkFi2taSIBVK78cHEnEykAjsEs9AdI+oFKVPGPWw== X-Received: by 2002:a65:56c9:: with SMTP id w9mr22955405pgs.296.1578962788523; Mon, 13 Jan 2020 16:46:28 -0800 (PST) Received: from [10.106.128.47] ([155.100.47.7]) by smtp.gmail.com with ESMTPSA id o7sm16634549pfg.138.2020.01.13.16.46.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 13 Jan 2020 16:46:27 -0800 (PST) From: Rob Sargent Message-Id: Content-Type: multipart/alternative; boundary="Apple-Mail=_B910EFE0-445B-49B6-ABB4-4D75FB605484" Mime-Version: 1.0 (Mac OS X Mail 13.0 \(3608.40.2.2.4\)) Subject: Re: Worse performance with higher work_mem? Date: Mon, 13 Jan 2020 17:46:26 -0700 In-Reply-To: <8B3CD048-3DF2-47D6-875A-C935819C48FF@alaska.edu> Cc: Tom Lane , "pgsql-generallists.postgresql.org" To: Israel Brewster References: <17AAA60F-6CC8-4D5A-9944-9C144A81E609@alaska.edu> <5228.1578961177@sss.pgh.pa.us> <8B3CD048-3DF2-47D6-875A-C935819C48FF@alaska.edu> X-Mailer: Apple Mail (2.3608.40.2.2.4) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail=_B910EFE0-445B-49B6-ABB4-4D75FB605484 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Jan 13, 2020, at 5:41 PM, Israel Brewster = wrote: >=20 >> On Jan 13, 2020, at 3:19 PM, Tom Lane > wrote: >>=20 >> Israel Brewster > writes: >>> In looking at the explain analyze output, I noticed that it had an = =E2=80=9Cexternal merge Disk=E2=80=9D sort going on, accounting for = about 1 second of the runtime (explain analyze output here: = https://explain.depesz.com/s/jx0q = >). Since the machine has plenty of = RAM available, I went ahead and increased the work_mem parameter. = Whereupon the query plan got much simpler, and performance of said query = completely tanked, increasing to about 15.5 seconds runtime = (https://explain.depesz.com/s/Kl0S = >), most of which was in a = HashAggregate. >>> How can I fix this? Thanks. >>=20 >> Well, the brute-force way not to get that plan is "set enable_hashagg = =3D >> false". But it'd likely be a better idea to try to improve the = planner's >> rowcount estimates. The problem here seems to be lack of stats for >> either "time_bucket('1 week', read_time)" or "read_time::date". >> In the case of the latter, do you really need a coercion to date? >> If it's a timestamp column, I'd think not. As for the former, >> if the table doesn't get a lot of updates then creating an expression >> index on that expression might be useful. >>=20 >=20 > Thanks for the suggestions. Disabling hash aggregates actually made = things even worse: (https://explain.depesz.com/s/cjDg = ), so even if that wasn=E2=80=99t a = brute-force option, it doesn=E2=80=99t appear to be a good one. Creating = an index on the time_bucket expression didn=E2=80=99t seem to make any = difference, and my data does get a lot of additions (though virtually no = changes) anyway (about 1 additional record per second). As far as = coercion to date, that=E2=80=99s so I can do queries bounded by date, = and actually have all results from said date included. That said, I = could of course simply make sure that when I get a query parameter of, = say, 2020-1-13, I expand that into a full date-time for the end of the = day. However, doing so for a test query didn=E2=80=99t seem to make much = of a difference either: https://explain.depesz.com/s/X5VT = >=20 > So, to summarise: >=20 > Set enable_hasagg=3Doff: worse > Index on time_bucket expression: no change in execution time or query = plan that I can see > Get rid of coercion to date: *slight* improvement. 14.692 seconds = instead of 15.5 seconds. And it looks like the row count estimates were = actually worse. > Lower work_mem, forcing a disk sort and completely different query = plan: Way, way better (around 6 seconds) >=20 > =E2=80=A6so so far, it looks like the best option is to lower the = work_mem, run the query, then set it back? > --- I don=E2=80=99t see that you=E2=80=99ve updated the statistics? --Apple-Mail=_B910EFE0-445B-49B6-ABB4-4D75FB605484 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8

On Jan 13, 2020, at 5:41 PM, Israel Brewster <ijbrewster@alaska.edu> wrote:

On = Jan 13, 2020, at 3:19 PM, Tom Lane <tgl@sss.pgh.pa.us> = wrote:

Israel Brewster <ijbrewster@alaska.edu> writes:
In looking at the explain analyze output, I = noticed that it had an =E2=80=9Cexternal merge Disk=E2=80=9D sort going = on, accounting for about 1 second of the runtime (explain analyze output = here: https://explain.depesz.com/s/jx0q <https://explain.depesz.com/s/jx0q>). Since the machine = has plenty of RAM available, I went ahead and increased the work_mem = parameter. Whereupon the query plan got much simpler, and performance of = said query completely tanked, increasing to about 15.5 seconds runtime = (https://explain.depesz.com/s/Kl0S <https://explain.depesz.com/s/Kl0S>), most of which was = in a HashAggregate.
How can I fix this? Thanks.

Well, the brute-force way not to = get that plan is "set enable_hashagg =3D
false".  But = it'd likely be a better idea to try to improve the planner's
rowcount estimates.  The problem here seems to be lack = of stats for
either "time_bucket('1 week', read_time)" or = "read_time::date".
In the case of the latter, do you = really need a coercion to date?
If it's a timestamp = column, I'd think not.  As for the former,
if the = table doesn't get a lot of updates then creating an expression
index on that expression might be useful.


Thanks for the suggestions. Disabling = hash aggregates actually made things even worse: (https://explain.depesz.com/s/cjDg), so even if that = wasn=E2=80=99t a brute-force option, it doesn=E2=80=99t appear to be a = good one. Creating an index on the time_bucket expression didn=E2=80=99t = seem to make any difference, and my data does get a lot of additions = (though virtually no changes) anyway (about 1 additional record per = second). As far as coercion to date, that=E2=80=99s so I can do queries = bounded by date, and actually have all results from said date included. = That said, I could of course simply make sure that when I get a query = parameter of, say, 2020-1-13, I expand that into a full date-time for = the end of the day. However, doing so for a test query didn=E2=80=99t = seem to make much of a difference either: https://explain.depesz.com/s/X5VT

So, to summarise:

Set enable_hasagg=3Doff: = worse
Index on time_bucket expression: no change in = execution time or query plan that I can see
Get rid = of coercion to date: *slight* improvement. 14.692 seconds instead of = 15.5 seconds. And it looks like the row count estimates were actually = worse.
Lower work_mem, forcing a disk sort and = completely different query plan: Way, way better (around 6 = seconds)

=E2=80=A6= so so far, it looks like the best option is to lower the work_mem, run = the query, then set it back?
---

I don=E2=80=99t see that you=E2=80=99ve updated the = statistics?


= --Apple-Mail=_B910EFE0-445B-49B6-ABB4-4D75FB605484--