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 1kuWlv-0001kF-C9 for pgsql-admin@arkaria.postgresql.org; Wed, 30 Dec 2020 08:23:55 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kuWls-0006SJ-FT for pgsql-admin@arkaria.postgresql.org; Wed, 30 Dec 2020 08:23:52 +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 1kuWls-0006SC-7t for pgsql-admin@lists.postgresql.org; Wed, 30 Dec 2020 08:23:52 +0000 Received: from jakobs.com ([85.214.83.89] helo=rs.plausibolo.de) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kuWll-0000NK-Gy for pgsql-admin@lists.postgresql.org; Wed, 30 Dec 2020 08:23:51 +0000 Received: from localhost (localhost [127.0.0.1]) by rs.plausibolo.de (Postfix) with ESMTP id 3B1B43805B8; Wed, 30 Dec 2020 09:23:44 +0100 (CET) Received: from rs.plausibolo.de ([127.0.0.1]) by localhost (h2367442.stratoserver.net [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id CFY7vWQfp5xC; Wed, 30 Dec 2020 09:23:44 +0100 (CET) Received: from [192.168.178.86] (p5b24967a.dip0.t-ipconnect.de [91.36.150.122]) by rs.plausibolo.de (Postfix) with ESMTPSA id EE3593804D2; Wed, 30 Dec 2020 09:23:43 +0100 (CET) Subject: Re: how to make duplicate finding query faster? To: pgsql-admin@lists.postgresql.org References: From: Holger Jakobs Message-ID: <5bf642e4-eb22-8eba-e96d-5b33b5010b03@jakobs.com> Date: Wed, 30 Dec 2020 09:23:42 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="tg8qX3C02YF5jBgNy8TcZjhxwzB8Ad0F5" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --tg8qX3C02YF5jBgNy8TcZjhxwzB8Ad0F5 Content-Type: multipart/mixed; boundary="xVmXS8SHkflbovkSkfLTptQAa9s8rItEa"; protected-headers="v1" From: Holger Jakobs To: pgsql-admin@lists.postgresql.org Message-ID: <5bf642e4-eb22-8eba-e96d-5b33b5010b03@jakobs.com> Subject: Re: how to make duplicate finding query faster? References: In-Reply-To: --xVmXS8SHkflbovkSkfLTptQAa9s8rItEa Content-Type: multipart/alternative; boundary="------------60DB514A57D39D69C484C220" Content-Language: de-DE This is a multi-part message in MIME format. --------------60DB514A57D39D69C484C220 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable Am 30.12.20 um 08:36 schrieb Sachin Kumar: > Hi All, > > 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=20 > error.=C2=A0 I am using below mention query. > > if Card_Bank.objects.filter( Q(ACCOUNT_NUMBER=3Dcard_number) ).exists()= : > =C2=A0 =C2=A0 =C2=A0 =C2=A0 flag=3D2 > =C2=A0 =C2=A0 =C2=A0 else: > =C2=A0 =C2=A0 =C2=A0 =C2=A0 flag=3D1 > it is taking too much time i am using 600k cards in CSV. > > Kindly help me in making the query=C2=A0faster. > > I am using Python, Django & PostgreSQL. > --=20 > > Best Regards, > Sachin Kumar I think it would be easier to not check the duplicates before, but let=20 the DB complain about duplicates. That would about slash the roundtrips to the DB in half. Instead of=20 check + insert there would be only an insert, which might fail every now = and then. Regards, Holger --=20 Holger Jakobs, Bergisch Gladbach, Tel. +49-178-9759012 --------------60DB514A57D39D69C484C220 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
Am 30.12.20 um 08:36 schrieb Sachin Kumar:
Hi All,

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.=C2=A0 I am using below mention query.=

if Card_Bank.objects.filter( Q(ACCOUNT_NUMBER=3Dcard_number)= ).exists():
=C2=A0 =C2=A0 =C2=A0 =C2=A0 flag=3D2
=C2=A0 =C2=A0 =C2=A0 else:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 flag=3D1
it is taking too much time i am using 600k cards in CSV.

Kindly help me in making the query=C2=A0faster.

I am using Python, Django & PostgreSQL.
--

Best Regards,
Sachin Kumar

I think it would be easier to not check the duplicates before, but let the DB complain about duplicates.

That would about slash the roundtrips to the DB in half. Instead of check + insert there would be only an insert, which might fail every now and then.

Regards,

Holger

--=20
Holger Jakobs, Bergisch Gladbach, Tel. +49-178-9759012
--------------60DB514A57D39D69C484C220-- --xVmXS8SHkflbovkSkfLTptQAa9s8rItEa-- --tg8qX3C02YF5jBgNy8TcZjhxwzB8Ad0F5 Content-Type: application/pgp-signature; name="OpenPGP_signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="OpenPGP_signature" -----BEGIN PGP SIGNATURE----- wmMEABEIACMWIQScuJCxWJK1p5BOebGKV5yTsxlhugUCX+w5DgUDAAAAAAAKCRCKV5yTsxlhuhLl AJ0eCYgUhIgtsb0V0NypG6SHVzL6dgCeMnyGCOoOz1dffZiIiuA60XTyEQE= =WVjX -----END PGP SIGNATURE----- --tg8qX3C02YF5jBgNy8TcZjhxwzB8Ad0F5--