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 1iyeHN-0004MC-O4 for pgsql-general@arkaria.postgresql.org; Mon, 03 Feb 2020 16:08:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iyeGN-0000d2-OB for pgsql-general@arkaria.postgresql.org; Mon, 03 Feb 2020 16:07:51 +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 1iyeGN-0000co-At for pgsql-general@lists.postgresql.org; Mon, 03 Feb 2020 16:07:51 +0000 Received: from mta2.recol.net ([64.207.103.7]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iyeGK-0001Kn-EO for pgsql-general@lists.postgresql.org; Mon, 03 Feb 2020 16:07:49 +0000 Received: from wrkhors.com (99-105-125-193.uvs.stlsmo.sbcglobal.net [99.105.125.193]) by mta2.recol.net (Postfix) with ESMTPSA id 585BA39812C; Mon, 3 Feb 2020 11:07:46 -0500 (EST) Date: Mon, 3 Feb 2020 10:01:04 -0600 From: Steven Lembark To: pgsql-general@lists.postgresql.org Cc: Matthias Apitz , lembark@wrkhors.com Subject: Re: performance of loading CSV data with COPY is 50 times faster than Perl::DBI Message-ID: <20200203100104.2a9d3fcd.lembark@wrkhors.com> In-Reply-To: <20200131182441.GA8181@c720-r342378> References: <20200131182441.GA8181@c720-r342378> Organization: Workhorse Computing X-Mailer: Claws Mail 3.17.3 (GTK+ 2.24.32; x86_64-pc-linux-gnu) X-Workhorse: Quite MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, 31 Jan 2020 19:24:41 +0100 Matthias Apitz wrote: > Hello, > > Since ages, we transfer data between different DBS (Informix, Sybase, > Oracle, and now PostgreSQL) with our own written tool, based on > Perl::DBI which produces a CSV like export in a common way, i.e. an > export of Oracle can be loaded into Sybase and vice versa. Export and > Import is done row by row, for some tables millions of rows. > > We produced a special version of the tool to export the rows into a > format which understands the PostgreSQL's COPY command and got to know > that the import into PostgreSQL of the same data with COPY is 50 times > faster than with Perl::DBI, 2.5 minutes ./. 140 minutes for around 6 > million rows into an empty table without indexes. > > How can COPY do this so fast? DBI is a wonderful tool, but not intended for bulk transfer. It is useful for post-processing queries that extract specific data in ways that SQL cannot readily handle. One big slowdown is the cycle of pull-a-row, push-a-row involves signiicant latency due to database connections. That limits the throughput. Depending on who wrote the code, they may have extracted the rows as hashrefs rather than arrays; that can be a 10x slowdown right there. [I have no idea why so many people are so addicted to storing rows in hashes, but it is always a significant slowdown; and array slices are no more complicated than hash slices!] Where DBI is really nice is managing the copy: generating a useful basename, determining the path, deciding whether to zip the output, etc. Using metadata to determine which of the tables to back up and where to put the result, all of the automation you'd want to get flexible backups is nice in DBI. Bulk copies, probably not. -- Steven Lembark 3646 Flora Place Workhorse Computing St. Louis, MO 63110 lembark@wrkhors.com +1 888 359 3508