Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rWaXj-006CA3-2A for pgsql-general@arkaria.postgresql.org; Sun, 04 Feb 2024 11:20:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rWaXh-005Wht-OZ for pgsql-general@arkaria.postgresql.org; Sun, 04 Feb 2024 11:20:09 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rWaXh-005Whl-Am for pgsql-general@lists.postgresql.org; Sun, 04 Feb 2024 11:20:09 +0000 Received: from mail-ej1-x634.google.com ([2a00:1450:4864:20::634]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rWaXa-005PAd-PE for pgsql-general@lists.postgresql.org; Sun, 04 Feb 2024 11:20:08 +0000 Received: by mail-ej1-x634.google.com with SMTP id a640c23a62f3a-a35e65df2d8so465325366b.0 for ; Sun, 04 Feb 2024 03:20:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707045601; x=1707650401; darn=lists.postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=OOmnOudbLf3+uVTWm9ezUNFjW+k2jF27SbpECargPPA=; b=kKLbJhHDRaxEmdxBHCvlM8Xtk2xoczQjefeocQtJb8x4JOgy7wd7Lh80bNAlP6Iai1 0Kkiepv/bZqAP8XiWFGO22iUSQPPnv2Y02a5wdRJKDJVnj1x9eVTrLcRv7yPXZ7+jfdv Zd6sKBo13fTI9SfXenymQWaT+JubyegykgA0DQ84FmNJDHqBOTltUoQ8I5/qhnFCdAik G1G/daoMUayCDAOaaiffT3bkJXkHpMfZ1+I+YsT7P2GavsPsy2IbljlaHvS8BtH+1puB svI7laumGUyvpg7Km9EjDYwJVN5sUEs5uYLmfhk2cYy189qX0TeeQXo7Hra1BMfiG+oJ Fjxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707045601; x=1707650401; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OOmnOudbLf3+uVTWm9ezUNFjW+k2jF27SbpECargPPA=; b=SThb5oapTOfVgaWykMHOypwV9y84vcTU22PJSY7vCBkMfVew2/+ciQYUSH0Om946uU RZyNxWUA1z8bYUAPXoRFy7LD3UIi43TfXXFYRQaCBgQR+DAibL8w/FfEkToBTbwi/JKT GRPwCrqY0/PZtgyKJqKk+lH9m9MN+o1RipZ/icC97o/gBQeg+7IHTgiztAJvrxUYljC6 al9WOChxqVpn/WzFn7F0eFVUErAqusEk2l0dMBK2wMRVMAPs5CPboX2kf96gOEpYws/7 SGu6581c0Zd+PC4UOrd3xx6HCSIsaqACHKpIGotbsJy2R8iiZWt1ZBE2a3Y6mqJSYvho v7Nw== X-Gm-Message-State: AOJu0Yy671mW/jAfBeplsinMIRLhVqsVPJmuDokS4lj/07sMcTSzOyqT sZo3cqqYjowDIYa1jUWrzPm397XeN63T8TUE6PmWhkZGQQPF7lL7 X-Google-Smtp-Source: AGHT+IHNypM6jTTq6uQm0gBqLuwejW74MhQ0dN5CloFZ8ixVtt3Q0fjyoBNopJNWNFiUm0AQWEOtMA== X-Received: by 2002:a17:906:ca4a:b0:a37:9e2d:ee67 with SMTP id jx10-20020a170906ca4a00b00a379e2dee67mr626589ejb.60.1707045600654; Sun, 04 Feb 2024 03:20:00 -0800 (PST) Received: from smtpclient.apple (217-19-30-105.dsl.cambrium.nl. [217.19.30.105]) by smtp.gmail.com with ESMTPSA id cu9-20020a170906ba8900b00a37116e2885sm2672123ejd.84.2024.02.04.03.20.00 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 04 Feb 2024 03:20:00 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3774.300.61.1.2\)) Subject: Re: How to do faster DML From: Alban Hertroys In-Reply-To: Date: Sun, 4 Feb 2024 12:19:49 +0100 Cc: pgsql-general Content-Transfer-Encoding: quoted-printable Message-Id: <8F3E1C5C-5C3C-4F13-ACE8-30F9D30BBF26@gmail.com> References: To: Lok P X-Mailer: Apple Mail (2.3774.300.61.1.2) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 3 Feb 2024, at 13:20, Lok P wrote: >=20 > Hello All, > A non partitioned table having ~4.8 billion rows in it and having data = size as ~1.4TB , row size as ~313 bytes having ~127 columns in it. This = has got approx ~1billion+ duplicate rows inserted in it and we want to = get the duplicate data removed for this table and create a PK/unique = constraint back so as to not have the duplicate values in future. We are = struggling to do the same. ~4.8 billion rows of which ~1 billion are duplicates=E2=80=A6 Wait a = minute=E2=80=A6 Did you verify that your ID column is larger than 32-bits? Because if that=E2=80=99s a 32 bit integer, the range of values it can = hold is about 4.3 billion, after which it wraps around. With ~4.8 billion rows that would result in about ~0.5 billion repeated = ID values, giving you the reported ~1 billion duplicate ID's. If that=E2=80=99s the case, your duplicates obviously aren=E2=80=99t = really duplicates and you require a different type of solution. > Teammates suggested doing this using CTAS method, i.e. create a new = table with the unique record set and then drop the main table. Something = as below >=20 > create table TAB1_New > as > SELECT * from TAB1 A > where ID in > (select min(ID) from TAB1 > group by ID having count(ID)>=3D1 ); >=20 > But for the above to work faster , they mentioned to have an index = created on the column using which the duplicate check will be performed = i.e ID column. So, creating the index itself took ~2hrs+ and the index = size now shows as ~116GB. >=20 > Create index idx1 on TAB1(ID) Are your duplicates exact duplicates? Or is there an order of preference = among them? And if so, what really makes those rows unique? That matters for solutions on how to deduplicate these rows. Regards, Alban Hertroys -- If you can't see the forest for the trees, cut the trees and you'll find there is no forest.