Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kuZYq-0007Kc-1z for pgsql-admin@arkaria.postgresql.org; Wed, 30 Dec 2020 11:22:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kuZYp-0000dE-0K for pgsql-admin@arkaria.postgresql.org; Wed, 30 Dec 2020 11:22:35 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kuZYo-0000d7-AS for pgsql-admin@lists.postgresql.org; Wed, 30 Dec 2020 11:22:34 +0000 Received: from out2-50.antispamcloud.com ([185.201.17.50]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kuZYl-0002Um-6E for pgsql-admin@lists.postgresql.org; Wed, 30 Dec 2020 11:22:33 +0000 Received: from hp309.hostpapa.com ([69.90.160.170]) by mx133.antispamcloud.com with esmtpsa (TLSv1.2:AES128-GCM-SHA256:128) (Exim 4.92) (envelope-from ) id 1kuZYc-000aMC-Sy; Wed, 30 Dec 2020 12:22:26 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=pendari.org ; s=default; h=Content-Type:MIME-Version:References:In-Reply-To:Message-ID: Date:Subject:Cc:To:From:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=I7eYPME8LJuJOIh9n3NrlwEfRQ3yWLiFB9HhuRM+qRE=; b=KWY57YVPmoO4oLgfsZjI8vzHX2 QziwS2W4yYXN2mpqSBRJBB1kxLFSIA1WinJ4WofU6M2887oG/qF7aP3xoPfXNEIYlWonVyKv03wey e1fJzywp5ht3NOzAwW5PyFiUkC01Twj52YCSvRgfP4KK6wUhWgml/N2B2463UrX7HYdUuf9QMq8Iz yekCfKSYsxiPKEZSd1RYK35ofTcNParOL/IKb9Oa0ZLrxtsUD+lk2nMZKRxqfERhuRg05OkCiGqlx Msfj8GeV98TnMvanHF8uVZzwXw+rv/bBvSa8sjzyWfppzThKRxi5tSde1f1rzv573EL/p41393yem GK6PTpvA==; Received: from [1.129.210.191] (port=50727 helo=[192.168.20.53]) by hp309.hostpapa.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1kuZXt-008X08-5j; Wed, 30 Dec 2020 22:21:39 +1100 From: "Gavan Schneider" To: "Sachin Kumar" Cc: "Holger Jakobs" , krishna@thewebconz.com, pgsql-admin@lists.postgresql.org Subject: Re: how to make duplicate finding query faster? Date: Wed, 30 Dec 2020 22:21:17 +1100 X-Mailer: MailMate (1.14r5757) Message-ID: <43C74F51-3099-4787-8C73-CE928CEAB01A@pendari.org> In-Reply-To: References: <5bf642e4-eb22-8eba-e96d-5b33b5010b03@jakobs.com> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="=_MailMate_0FDDB1C0-6436-4FC9-BF01-9DFB01A8A54E_=" X-OutGoing-Spam-Status: No, score=-1.0 X-Get-Message-Sender-Via: hp309.hostpapa.com: authenticated_id: list.pg.gavan@pendari.org X-Authenticated-Sender: hp309.hostpapa.com: list.pg.gavan@pendari.org X-AuthUser: list.pg.gavan@pendari.org X-Originating-IP: 69.90.160.170 X-Spampanel-Domain: outgoing-shared.hostpapa.com X-Spampanel-Username: outgoingshared Authentication-Results: antispamcloud.com; auth=pass (login) smtp.auth=outgoingshared@outgoing-shared.hostpapa.com X-Spampanel-Outgoing-Class: unsure X-Spampanel-Outgoing-Evidence: Combined (0.28) X-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MXKn6m+UeFXprlCOm3BAEb JtDkd5Uii0xoakAF3FKqcOP40kuJDclzVoznDI0Smv6yUqaxHDdsks4SA3vba9gXQc2Z3JKVmi72 ocgY5kMQSjs7DowYF5X92G1XxuvnCFAIOAo5MUnvjSm4mUbcXAzKel1wzeDoivDxNuwHQQgmqlDH lzmtD6WzsnugIMu3aOamYA== X-Report-Abuse-To: spam@quarantine10.antispamcloud.com List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --=_MailMate_0FDDB1C0-6436-4FC9-BF01-9DFB01A8A54E_= Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: quoted-printable On 30 Dec 2020, at 19:59, Sachin Kumar wrote: > This will not suffice our requirement. we have to validate that there > would not be any duplicate value in DB. we have done that earlier by > leaving DB to check if there is any duplicate and found duplicate = > value in > DB. > If the constraint was in place *before* any data was inserted into the = database then there will be no duplicates. Adding a constraint to a = database will not remove existing data which violates the constraint. > we have a table "card" and a single column "number " which we are = > updated > with a csv file with 600k numbers and require no duplicate number = > should be > there in the table. > One trap is that the data may not be the same even if it=E2=80=99s a = =E2=80=9Cduplicate=E2=80=9D. The =E2=80=9Cnumber=E2=80=9D may have space= s or other human = readable characters. It is a duplicate from the human perspective but = not the machine. These =E2=80=9Cduplicates=E2=80=9D will be also be very = hard to = find with a simple query. If OP is not already doing this I suggest the = =E2=80=9Cnumber=E2=80=9D should be stored in the database as BIGINT eg. cc_number BIGINT NON NULL UNIQUE, =E2=80=94 this will prevent any addit= ion = of a duplicate cc_number PRIMARY KEY (cc_number) =E2=80=94 or, more simply, in one line: cc_numb= er = BIGINT PRIMARY KEY Obviously the csv will have to be checked to ensure the number is a = legal integer value. There is nothing in the original post to say what is contained in the = card field. And it has not been stated if this field is also unique. = Assuming it has text then this will be more interesting to work with as = a human may see the duplication but not the machine, but if it=E2=80=99s = only = the number that has to be unique then it is straight forward. = > Please if can have a faster query can help us in achieving this = > requirement. > As noted by Holgar Jacobs up thread it is not possible for a query to = be faster than checks done at time the data is inserted. The data has to = be inserted and indexes created etc. If the duplicate key constraint is = violated then the duplicate is found. Otherwise the data has to be = inserted and a query run afterwards. Assuming there is a reasonably recent PostgreSQL hidden behind the = Django and Python, i.e., >9.5, then the INSERT can be attempted with INSERT INTO =E2=80=A6 ON CONFLICT DO NOTHING =E2=80=94 = https://www.postgresql.org/about/news/postgresql-131-125-1110-1015-9620-a= nd-9524-released-2111/ Once this is in place the data will be added to the table as fast as = that is possible, and with no duplicates. It will be very much slower if = every insertion has to be checked in the Django+Python stack. Basically = if speed is needed go to the database and talk in SQL. Regards Gavan Schneider (who can offer no help with Dango/Python) --=_MailMate_0FDDB1C0-6436-4FC9-BF01-9DFB01A8A54E_= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

On 30 Dec 2020, at 19:59, Sachin Kumar wrote:

This will not suffice our requirement. we have to validate that ther= e
would not be any duplicate value in DB. we have done that earlier by
leaving DB to check if there is any duplicate and found duplicate value i= n
DB.

If the constraint was in place before any data = was inserted into the database then there will be no duplicates. Adding a= constraint to a database will not remove existing data which violates th= e constraint.

we have a table "card" and a single column "number " which we are u= pdated
with a csv file with 600k numbers and require no duplicate number should = be
there in the table.

One trap is that the data may not be the same even if it=E2= =80=99s a =E2=80=9Cduplicate=E2=80=9D. The =E2=80=9Cnumber=E2=80=9D may h= ave spaces or other human readable characters. It is a duplicate from th= e human perspective but not the machine. These =E2=80=9Cduplicates=E2=80=9D= will be also be very hard to find with a simple query. If OP is not alre= ady doing this I suggest the =E2=80=9Cnumber=E2=80=9D should be stored in= the database as BIGINT eg.

cc_number BIGINT =
NON NULL UNIQUE,  =E2=80=94 this will prevent any addition of a duplicate=
 cc_number
PRIMARY KEY (cc_number)		=E2=80=94 or, more simply, in one line: cc_numbe=
r BIGINT PRIMARY KEY

Obviously the csv will have to be checked to ensure the n= umber is a legal integer value.

There is nothing in the original post to say what is cont= ained in the card field. And it has not been stated if this field is also= unique. Assuming it has text then this will be more interesting to work = with as a human may see the duplication but not the machine, but if it=E2= =80=99s only the number that has to be unique then it is straight forward= =2E

Please if can have a faster query can help us in achieving this requ= irement.

As noted by Holgar Jacobs up thread it is not possible f= or a query to be faster than checks done at time the data is inserted. Th= e data has to be inserted and indexes created etc. If the duplicate key c= onstraint is violated then the duplicate is found. Otherwise the data has= to be inserted and a query run afterwards.

Assuming there is a reasonably recent PostgreSQL hidden b= ehind the Django and Python, i.e., >9.5, then the INSERT can be attemp= ted with

INSERT INTO  =E2=80=
=A6  ON CONFLICT DO NOTHING =E2=80=94 https://www.postgresql.org/about/ne=
ws/postgresql-131-125-1110-1015-9620-and-9524-released-2111/

Once this is in place the data will be added to the table= as fast as that is possible, and with no duplicates. It will be very muc= h slower if every insertion has to be checked in the Django+Python stack.= Basically if speed is needed go to the database and talk in SQL.

Regards
Gavan Schneider (who can offer no help with Dango/Python)

--=_MailMate_0FDDB1C0-6436-4FC9-BF01-9DFB01A8A54E_=--