Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eVnaS-0001Gt-Ge for pgsql-general@arkaria.postgresql.org; Mon, 01 Jan 2018 00:04:16 +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 1eVnaR-0003jR-N0 for pgsql-general@arkaria.postgresql.org; Mon, 01 Jan 2018 00:04:15 +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 1eVnaR-0003jG-5s for pgsql-general@lists.postgresql.org; Mon, 01 Jan 2018 00:04:15 +0000 Received: from mail-io0-x241.google.com ([2607:f8b0:4001:c06::241]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eVnaJ-0000qS-FT for pgsql-general@postgresql.org; Mon, 01 Jan 2018 00:04:13 +0000 Received: by mail-io0-x241.google.com with SMTP id 14so39328682iou.2 for ; Sun, 31 Dec 2017 16:04:07 -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=dlJFg53JmJdXGM3silE8rgrVKohUwirQVHCB9QRUANI=; b=SxmL1MZjQjHpAzZe5NLD++rP5sbYHxR8BHP1dt0UL/M4y/VmSH6zmHz02dHzrcA8A6 JqVu9uDUYYD5FUKE6zFJPoZ2fAgywfjUNZOjB5564yxGW47O4iTnasB3q96LiHS74twq cqMJ9B0Q7cjvIwkvDYbZEtLsKjoe19VXl2/87X3Pp86172hM+i+hX0p5ub8WWwECGM6U beZykRAiSL1MxPPfIdwinLWSNGNrD8Stwhr7ufNvZntIpThjAqQl75eGXpB6Jnpejl18 P/1OQ/JOToV2NJonQRfFfCL+4/TvXmqwJx+lwn+wKxUDDzq0nugopPX5ZKVKNYOzciD2 OMHw== 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=dlJFg53JmJdXGM3silE8rgrVKohUwirQVHCB9QRUANI=; b=UlMK/qbssy6VbdRet3hBuBOXkHun4dHY2CJ1MDmJrPpkBImzK6Pz9HQ90F9dfmxhL0 eFQBjfRNSWYdkyDjvfcMAcS1TCE79rBVszfj1pR7tvRtQlZ+q79xuaTx8dBnGBiTbAaH qov6EthhiHS3svRDknOfsgQmin3Lr0IqaQuYatHHGp1DUosJe7Ot+mWDgJfWq+UYxy8s JcdOP4SJAgPUlcxGjT+0k3VTdVmYfyPQKkEdK2YH9qjwOdh7NYferK4ZcLzHLapHsQPh PoRTLdkN2BqIkAYWsVOcs0BxDQ/HhiM2NszmBuf3RsYoAEwUkc/e2qht48W5galPtYay 4TSg== X-Gm-Message-State: AKGB3mKYSQQHlrNJMuv5r8EymDPUxzs1NDNz/9T0ACYz9Ye6ICf3+U+G XR6jRoVtkvw2o//m+Y5B6C4= X-Google-Smtp-Source: ACJfBoulgYaAAG92DB3efU3ECLVF4d5zFeXooiPg2MBn3H+wBtTEC1oJzeTECXgQgNtPd7wZsifRPQ== X-Received: by 10.107.153.209 with SMTP id b200mr39950363ioe.245.1514765046304; Sun, 31 Dec 2017 16:04:06 -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 j192sm19809814ioj.49.2017.12.31.16.04.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 31 Dec 2017 16:04:04 -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 17:04:02 -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 >> 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 Thank you, and understood. Small clarification: the batches (393 or = 8646) are not single update statements, rather groupings of separate = updates sharing common restriction values. I don=E2=80=99t have the = hardware to replicate the problem.=20 The table being updated is described below. Due to an embarrassing = cut/paste error, I=E2=80=99ll be updating the three =E2=80=98events=E2=80=99= columns based on probandset_id, chrom, startbase, endbase (matching the = unique constraint). The first two fields (probandset_id and = probandset_id+chrom) are the possible partitions - strictly from a file = generation point of view. The updates are unique and atomic and could = be clumped into transactions arbitrarily. The function would have to = take seven args, the key plus the replacement values. Ultimate sql: update segment set events_less =3D i, events_equal =3D j, = events_greater =3D k where probanset_id =3D id and chromosomes =3D ch and = startbase =3D sb and endbase =3D eb; /* I might check that an update is needed be _less !=3D i and _equal !=3D = j */ Table "segment" Column | Type | Collation | Nullable | Default=20 ----------------+---------+-----------+----------+--------- id | uuid | | not null |=20 chrom | integer | | not null |=20 markerset_id | uuid | | not null |=20 probandset_id | uuid | | not null |=20 startbase | integer | | not null |=20 endbase | integer | | not null |=20 firstmarker | integer | | not null |=20 lastmarker | integer | | not null |=20 events_less | bigint | | not null | 0 events_equal | bigint | | not null | 0 events_greater | bigint | | not null | 0 Indexes: "segment_pkey" PRIMARY KEY, btree (id) "useg" UNIQUE CONSTRAINT, btree (probandset_id, chrom, startbase, = endbase) "segment_markerset_id_chrom_firstmarker_idx" btree (markerset_id, = chrom, firstmarker) Foreign-key constraints: "segment_probandset_id_fkey" FOREIGN KEY (probandset_id) REFERENCES = probandset(id) Referenced by: TABLE "segmentset_member" CONSTRAINT = "segmentset_member_segment_id_fkey" FOREIGN KEY (segment_id) REFERENCES = segment(id) > --=20 > David Rowley http://www.2ndQuadrant.com/ > PostgreSQL Development, 24x7 Support, Training & Services