Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 7FA419F942C for ; Fri, 20 Apr 2007 12:05:36 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 26690-10 for ; Fri, 20 Apr 2007 12:05:28 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from mail63.csoft.net (leary.csoft.net [205.205.214.5]) by postgresql.org (Postfix) with SMTP id 0108B9FB52E for ; Fri, 20 Apr 2007 12:05:27 -0300 (ADT) Received: (qmail 17336 invoked by uid 1112); 20 Apr 2007 15:05:23 -0000 Date: Fri, 20 Apr 2007 11:05:23 -0400 (EDT) From: Kris Jurka X-X-Sender: books@leary.csoft.net To: femski cc: pgsql-jdbc@postgresql.org Subject: Re: Poor addBatch performance. Why dosn't it use copy ? In-Reply-To: <10099075.post@talk.nabble.com> Message-ID: References: <10099075.post@talk.nabble.com> MIME-Version: 1.0 Content-Type: MULTIPART/MIXED; BOUNDARY="0-717849469-1177081523=:26355" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200704/77 X-Sequence-Number: 16926 This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --0-717849469-1177081523=:26355 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed On Fri, 20 Apr 2007, femski wrote: > I took this discussion in Postgres performance list and came out with > conclusion that its a > client side JDBC issue - so I am psting it here. > > I have a batch application that writes approx. 4 million rows into a narrow > (2 column) table. I am using JDBC addBatch/ExecuteBatch with auto commit > turned off. Batch size is 1000. So far I am seeing Postgres take > roughly five times (280 sec) the time it takes to do this in the Oracle > (60). This is on a Linux > server with Xeon woodcrest 5310 process. Plenty of memory. I have played > with many parameters on > the server side and they seem to have little effect - I am sure Postgres is > a very capable server and its > not a database server issue. Someone mentioned: > > "I actually went and looked at the JDBC api and realized 'addBatch' means to > run multiple stmts at once, not batch > inserting. femski, your best bet is to lobby the JDBC folks to build > support for 'copy' into the driver for faster bulk loads (or help out in > that regard). " This comment is inaccurate. addBatch is indeed useful for batch inserts. Consider the attached test case which inserts a million rows into a single column table using prepared statements with and without batches. I get: No batch: 148.92 With batch: 48.008 So using batch execution is about three times faster. Additionaly gains are possible by using multiple threads and multiple connections in your client to parallelize the work. This should provide linear scaling to the number of cores/io bandwidth you have available. Perhaps oracle is able to do this behind the scenes on the server, but postgresql does not. > Why doesn't the Postgres JDBC driver use "copy" for faster bulk insert ? > What is the best way to speedup > do bulk insert at this time or in near future (I was to use standard JDBC > API) ? > Automatically converting from insert to copy is possible in only limited circumstances that can be difficult to detect. For example copy cannot handle function calls in data, so you couldn't convert this to copy: INSERT INTO t VALUES (now(), ?). What if t is actually a view with an ON INSERT rule? Copy doesn't work with views. So some complicated parsing and analysis of the query is required. Sure it's possible, but it's non-trivial and no one has done the work. Kris Jurka --0-717849469-1177081523=:26355 Content-Type: TEXT/PLAIN; charset=US-ASCII; name=Batch.java Content-Transfer-Encoding: BASE64 Content-ID: Content-Description: Content-Disposition: attachment; filename=Batch.java aW1wb3J0IGphdmEuc3FsLio7DQoNCnB1YmxpYyBjbGFzcyBCYXRjaCB7DQoJ DQoJcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nIGFyZ3NbXSkgdGhy b3dzIEV4Y2VwdGlvbiB7DQoJCUNsYXNzLmZvck5hbWUoIm9yZy5wb3N0Z3Jl c3FsLkRyaXZlciIpOw0KCQlDb25uZWN0aW9uIGNvbm4gPSBEcml2ZXJNYW5h Z2VyLmdldENvbm5lY3Rpb24oImpkYmM6cG9zdGdyZXNxbDovL2xvY2FsaG9z dDo1ODMwL2p1cmthIiwianVya2EiLCIiKTsNCg0KCQlTdGF0ZW1lbnQgc3Rt dCA9IGNvbm4uY3JlYXRlU3RhdGVtZW50KCk7DQoJCXN0bXQuZXhlY3V0ZSgi RFJPUCBUQUJMRSBJRiBFWElTVFMgdCIpOw0KCQl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