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 1kubI2-0002NF-Qv for pgsql-admin@arkaria.postgresql.org; Wed, 30 Dec 2020 13:13:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kubI1-0006SU-HW for pgsql-admin@arkaria.postgresql.org; Wed, 30 Dec 2020 13:13:21 +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 1kubI1-0006Rh-A1 for pgsql-admin@lists.postgresql.org; Wed, 30 Dec 2020 13:13:21 +0000 Received: from mailout.easymail.ca ([64.68.200.34]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kubHw-0003ne-Cy for pgsql-admin@postgresql.org; Wed, 30 Dec 2020 13:13:20 +0000 Received: from localhost (localhost [127.0.0.1]) by mailout.easymail.ca (Postfix) with ESMTP id 0226821243; Wed, 30 Dec 2020 13:13:14 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at emo06-pco.easydns.vpn Received: from mailout.easymail.ca ([127.0.0.1]) by localhost (emo06-pco.easydns.vpn [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 4OqKkCum61CS; Wed, 30 Dec 2020 13:13:13 +0000 (UTC) Received: from [192.168.0.5] (75-166-125-160.hlrn.qwest.net [75.166.125.160]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mailout.easymail.ca (Postfix) with ESMTPSA id AB82721242; Wed, 30 Dec 2020 13:13:08 +0000 (UTC) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.4\)) Subject: Re: how to make duplicate finding query faster? From: Scott Ribe In-Reply-To: Date: Wed, 30 Dec 2020 06:13:07 -0700 Cc: pgsql-admin@postgresql.org, krishna@thewebconz.com, pgsql-admin@lists.postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: References: To: Sachin Kumar X-Mailer: Apple Mail (2.3608.120.23.2.4) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > On Dec 30, 2020, at 12:36 AM, Sachin Kumar = wrote: >=20 > Hi All, >=20 > I am uploading data into PostgreSQL using the CSV file and checking if = there is any duplicates value in DB it should return a duplicate error. = I am using below mention query. >=20 > if Card_Bank.objects.filter( Q(ACCOUNT_NUMBER=3Dcard_number) = ).exists():=20 > flag=3D2 > else: > flag=3D1 > it is taking too much time i am using 600k cards in CSV. >=20 > Kindly help me in making the query faster. >=20 > I am using Python, Django & PostgreSQL. > --=20 >=20 > Best Regards,=20 > Sachin Kumar Are you checking one-by-one because your goal is not to fail the whole = upload that contains the duplicates, but rather to skip only the = duplicates? If that's the case, I think you'd be better off copying the CSV straight = into a temp table, using a join to delete duplicates from it, then = insert the remainder into the target table, and finally drop the temp = table.=