Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eVqRt-0006c9-Fx for pgsql-general@arkaria.postgresql.org; Mon, 01 Jan 2018 03:07:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eVqRr-0000TX-M6 for pgsql-general@arkaria.postgresql.org; Mon, 01 Jan 2018 03:07:35 +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_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eVqRr-0000TM-Ch for pgsql-general@lists.postgresql.org; Mon, 01 Jan 2018 03:07:35 +0000 Received: from mail-it0-x242.google.com ([2607:f8b0:4001:c0b::242]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eVqRn-0006aj-6J for pgsql-general@postgresql.org; Mon, 01 Jan 2018 03:07:34 +0000 Received: by mail-it0-x242.google.com with SMTP id x28so35848419ita.0 for ; Sun, 31 Dec 2017 19:07:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=9+4dS4dNAkF8Z/skL3B6k/XPQ7u3JSScgfDYmMdMB9I=; b=ROI/tzO1dxjgVo1nZL3n14R4kSWahdAshapKGc5zNj8ckBDzF9Emq6vbN2J6e6P84F EJCQTw6NarmxFYIaUoplfTMCrN4Bkb2IuOoTnsYucpp81zmPLOJYuaocIpOliPUCZfhA eg9iJOKkkmhZmxUkV2xFqCt/me3/XCmupWV7xWztF8TEcAvOkuXqcqUZIobl0GLvNUOF ZWHUFuuMmXVoenctJrrvINw2QNCAMFtXXG5zDqzRz6YG4BZ/WqdCtEMO9davffQz+pSG KMeWs9wpbY1PwlEzVkd5pxJ8d4x97uZ6eMRxBLPtWp15S6stI3y+XpJt5c3YUUgjVV4a PEGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=9+4dS4dNAkF8Z/skL3B6k/XPQ7u3JSScgfDYmMdMB9I=; b=ZSEz+rgK4DDTKMP1mGxfgXtT427vkRdSiVeJF8tDGlGtcxkRT5JGjhLUokM1xhaJxC f6IkJCt5sWDCZlAjM98zf74e6eFx5zWgzbTiQDQBo7cZcGDGbE/gMRvM5UKCpCgwfi1G bf8ActIwmi+shFSy1Ch4jFYDX9sDdWVV2xlCEygozMPrFN2/L/y/lnDI2jK6yclYU+Cg P1fKct8+vgU4vXaMnGOczGe0EXaGpQ7G/LNMNyZjsrX+zlmQHmhPrkiscaLnJCKIwRFh 8W4jYYC/kZWDjsXXQW6BzfyTDIS8KPJOgCbnF+s2rb83nIdDJFLI9jJqZlbe+snIvpuJ ocAA== X-Gm-Message-State: AKGB3mKsMX20igss9xjngLci4UOgh9euXAN6O0fUddAFUOzHKwF+dnmL JA3LOEEC7h+q5TBjMMWROHE= X-Google-Smtp-Source: ACJfBovuXYB7wCmJgljiJQ8Mtx6kAsTSettJ6Lipe80P/UMtd1MZsLbq8nTdRzU8ZKw9ErsZN9emuw== X-Received: by 10.36.237.202 with SMTP id r193mr56778702ith.74.1514776047992; Sun, 31 Dec 2017 19:07:27 -0800 (PST) Received: from ?IPv6:2601:681:5500:1e3c:6563:f150:4f70:8266? ([2601:681:5500:1e3c:6563:f150:4f70:8266]) by smtp.gmail.com with ESMTPSA id i68sm22511550ioi.24.2017.12.31.19.07.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 31 Dec 2017 19:07:26 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 10.3 \(3273\)) Subject: Re: is single row update improved with function From: Rob Sargent In-Reply-To: Date: Sun, 31 Dec 2017 20:07:25 -0700 Cc: "pgsql-general@postgresql.org" Content-Transfer-Encoding: quoted-printable Message-Id: References: To: David Rowley X-Mailer: Apple Mail (2.3273) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > On Dec 31, 2017, at 4:31 PM, David Rowley = wrote: >=20 > On 1 January 2018 at 12:06, Rob Sargent wrote: >> I must update 3M of 100M records, with tuple specific modifications. = I can generate the necessary sql, but I=E2=80=99m wondering if files of = simple update statements affecting a single row is more effective than = files of a function call doing the same update given the necessary = values, including where clause restrictions? The query plan set by the = first should be decent for the remainder. >>=20 >> Alternatively, would a bulk load into a table of replacement values = and join info be the fastest way? >=20 > It's probably also worth thinking about this table's usage pattern. If > this table is an otherwise static table, then you may wish to think > about the little bit of bloat that doing the UPDATEs in a single > transaction would cause. >=20 Sorry, I didn=E2=80=99t address the question about the table's usage. = Currently we=E2=80=99re in a data loading phase and this table is almost = completed. Thereafter (post vacuum analyze) it will be 99.99% = read-only. The remainder will be updated in much the same fashion as = described early (set events_x =3D events_x + increment). This table will be analyzed a couple ways, mainly determining = significance threshold across various slices, each of which examines = roughly one tenth of the records. >> Either way I can break the updates into roughly 393 transactions = (7500 rows affected per tx) or 8646 transactions (350 rows per tx) if = less is more in this world. >=20 > If you were to perform the UPDATEs in batches it would allow you to > run a VACUUM between the UPDATEs. However, it might not be so > important as 3 million rows in 100 million is just 3%, so assuming all > your rows are the same size, then even doing this as a single > transaction would only cause 3% churn on the table. Possibly some of > the UPDATEs would reuse existing free space within the table, but if > they don't then it would only mean an extra 3% bloat. >=20 > As for which is faster. It's most likely going to depend on the query > plan for the UPDATE statements. If you need to perform 3 million seq > scans on the table, by doing 3 million individual statements, that's > likely not going to perform well. 3 million statements is likely not a > good option in any case as it means parsing and planning 3 million > UPDATE statements. Even your 393 statements might not be very good if > each of those UPDATEs must perform a seq scans on the 100 million row > table, but if each of those 393 statements can make use of an index to > easily get those 7500 rows, then that might be a better option than > doing the single UPDATE join method you mentioned. >=20 > It does sound like something you could take offline and benchmark if > performance is that critical. It's not really possible for us to tell > which is faster without seeing the schema, UPDATE statements and query > plans chosen. >=20 > --=20 > David Rowley http://www.2ndQuadrant.com/ > PostgreSQL Development, 24x7 Support, Training & Services