Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hbuqf-0006jn-0L for pgsql-general@arkaria.postgresql.org; Fri, 14 Jun 2019 22:39:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hbuqb-0007vo-G0 for pgsql-general@arkaria.postgresql.org; Fri, 14 Jun 2019 22:39:01 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hbuqb-0007vh-09 for pgsql-general@lists.postgresql.org; Fri, 14 Jun 2019 22:39:01 +0000 Received: from mail-pf1-x441.google.com ([2607:f8b0:4864:20::441]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hbuqY-0001wN-2z for pgsql-general@lists.postgresql.org; Fri, 14 Jun 2019 22:38:59 +0000 Received: by mail-pf1-x441.google.com with SMTP id d126so2236559pfd.2 for ; Fri, 14 Jun 2019 15:38:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=references:user-agent:from:to:cc:subject:in-reply-to:date :message-id:mime-version:content-transfer-encoding; bh=m6sIh3Lfs1jitJzoKBeWFuS6KqcntPHy8U0Oy9YjqfU=; b=E6E0tz/HOLHqXeHqabDnWQcrfEKL4U6vB0MRSypX43/8bIk5QcAk3HHFcmyHmLvdA2 1LPmfC9ARp86tXNSfaP+KKE/6h4kVoxZNZO5jssxstWBRnVF71VEuSV0jh/KiIu0rzOA 163y5wRZh51l0w958/RGzWKbAE5W1dO0VP3kXFgVm1OzqW2CsFHFA60Ynf/lWlBCgFSP dZm6XDBvnjZumxI+6uMeTZMe1NvjmR4OcUkzqbn4LrdynKEStePn0QqSiGxQ8VAVH4jS 71XYrDqblskSoyKDcWNvaXPiulSl68f/WKpSvfHZ0caj8qYs+XpJMZexqMutdt7hICA/ 1MSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:references:user-agent:from:to:cc:subject :in-reply-to:date:message-id:mime-version:content-transfer-encoding; bh=m6sIh3Lfs1jitJzoKBeWFuS6KqcntPHy8U0Oy9YjqfU=; b=bFBqG9mXCx5GbBgmwzVhLcJeQ79+Hh8i+4pvo4dLR6BzcHG8Yxe0p4RzmJVgIzdAA1 zm789EbBANMYtWsO3goJ5uY4rXD8vSWB7PAIGnNZoBZd2ks4HDR6Q9FOGXazVRZyVnr3 0NjjSm6nfROaVGoM2UlKjMYlGU/hocUWWe81JuoKzZIJCNSXlYeo79IuiVyTpPzDB1zF PbrAqPS3qYq0wWe7LuWacY9cnMFQppw03D0ESgbug22bzy2vZ/uopn5ttwO8GyyGK3ni EtakiollVPoiUnkpJIMbFVPDjiJ/iK4T+K0ypvdAtyWRMHDInH4gfT1hAkICIT7PwkOC kMHw== X-Gm-Message-State: APjAAAXzusN6JJwZi4umCK9T/vzDl2vCVj1XmTACrN9ZnDL/1wKfXmEZ lxMIFylFh1cPyH84Zz5yWOY4+QheSc4= X-Google-Smtp-Source: APXvYqxqP52XBFmNeNkJHI0TW+5nnLh7WOwcKstzjNhpgzchJM/LvsdU34C2BDhAVDigNNM0VnMojw== X-Received: by 2002:a63:d415:: with SMTP id a21mr19741417pgh.229.1560551936792; Fri, 14 Jun 2019 15:38:56 -0700 (PDT) Received: from tim-desktop (124-169-11-231.dyn.iinet.net.au. [124.169.11.231]) by smtp.gmail.com with ESMTPSA id j9sm3809240pff.88.2019.06.14.15.38.54 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Fri, 14 Jun 2019 15:38:55 -0700 (PDT) References: User-agent: mu4e 1.3.1; emacs 26.2 From: Tim Cross To: pgsql-general@lists.postgresql.org Cc: pgsql-general@postgresql.org Subject: Re: Copy Bulk Ignore Duplicated In-reply-to: Date: Sat, 15 Jun 2019 08:38:49 +1000 Message-ID: <87o92zpzba.fsf@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Leandro Guimar=C3=A3es writes: > Hi, > > I have a scenario with a large table and I'm trying to insert it via a > COPY command with a csv file. > > Everything works, but sometimes my source .csv file has duplicated data > in the previously fulfilled table. If I add a check constraint and try to > run the COPY command I have an error that stops the whole insertion. > > I've tried to put the data in a tmp table and fill the main using > distinct this way (the fields and names are just examples): > > INSERT INTO final_table values (name, document) > SELECT DISTINCT name, document > FROM tmp_TABLE t1 > WHERE NOT EXISTS ( > SELECT 1 FROM final_table t2 > WHERE (t2.name, t2.document) > IS NOT DISTINCT FROM (t1.name, t1.document)) > > The problem is that my final_table is a large (and partitioned) table and > this query is taking a long time to execute. > > Someone have any idea (really guys anything would be great) how to solve > this situation? I need to ignore duplicates instead to have some error. > > I'm using* PostgreSQL 9.4* so I can't use "ON CONFLICT" and upgrade is not > an option. > Explain plan would probably shed some light, but I suspect your performance is being heavily hit by the sub query. Distinct is an expensive operation and you are performing it once for every distinct row in your temp table. It isn't clear what the primary key is for your final table - name + document seems suspicious given these seem to be the only two columns your inserting as well. You don't indicate what the data types are either - it document is something like 'text' then using it in a distinct clause is likely to have huge performance impact.=20 The first thing I'd do is to eliminate duplicates from your temp table as a separate statement or by pre-filtering the CSV before import. I would then try something like an outer join to identify rows in your temp table which don't exist in your final table and select from there to insert into the final table. You don't really need the distinct in the sub query as all you really need to know is if (name, document) exists - it doesn't matter if more than one exists (for this test).=20 If you really don't have something more specific for a primary key, depending on what data type 'document' is and how large it is, you may find adding a column which is a checksum of your 'document' field a useful addition. I have done this in the past where I had an application where name was not unique and we only wanted distinct instances of 'document' (document was a fairly large XML document in this case).=20 -- Tim Cross