X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 2BCCFD1B2A4 for ; Wed, 26 May 2004 23:06:31 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 53887-01 for ; Wed, 26 May 2004 23:06:13 -0300 (ADT) Received: from yass.opencloud.co.nz (yass.opencloud.co.nz [203.79.85.162]) by svr1.postgresql.org (Postfix) with ESMTP id 4BC7DD1B1A1 for ; Wed, 26 May 2004 23:06:08 -0300 (ADT) Received: from 203-79-104-60.cable.paradise.net.nz ([203.79.104.60] helo=opencloud.com) by yass.opencloud.co.nz with asmtp (Exim 3.35 #1 (Debian)) id 1BTAHZ-0000Fy-00; Thu, 27 May 2004 14:06:09 +1200 Message-ID: <40B54D12.7070804@opencloud.com> Date: Thu, 27 May 2004 14:06:10 +1200 From: Oliver Jowett User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6b) Gecko/20031205 Thunderbird/0.4 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Ceki Gulcu Cc: pgsql-jdbc@postgresql.org Subject: Re: getGeneratedKeys method References: <20040526162751.71010.qmail@web51004.mail.yahoo.com> In-Reply-To: <20040526162751.71010.qmail@web51004.mail.yahoo.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Spam-Status: No, hits=0.1 tagged_above=0.0 required=5.0 tests=RCVD_IN_SORBS X-Spam-Level: X-Archive-Number: 200405/177 X-Sequence-Number: 10187 Ceki Gulcu wrote: > Hello, > > Thanks for all the replies received so far. I really > appreciate it. > > One important point I failed to mention in my original > mail, was that we are using a prepared statement to > perform the initial batch of inserts via executeBatch, > followed by another prepared statement and invoking > executeBatch and a third prepared > statement+executeBatch. > > Your suggestions imply the use of regular statements > as opposed to prepared statements. I don't know > whether the performance boost is due to the use of > prepared statements or whether it's more the > "batching", that is regrouping the SQL statements in > one transaction. I initially thought you could batch the inserts in the approach I suggested, but of course addBatch and executeBatch live on a particular PreparedStatement so you can't do that. Ok, how about something like this instead: CREATE SEQUENCE id INCREMENT BY 100; Then limit batch size to 100, and for each batch do: SELECT nextval('id') -- let's assume this returns 1000 Next do the actual batch inserts, grouped by statement and computing ID values yourself based on the returned nextval + offset: INSERT INTO logging_event VALUES (1000, ...) INSERT INTO logging_event VALUES (1001, ...) INSERT INTO logging_event VALUES (1002, ...) -- ... INSERT INTO logging_event VALUES (1099, ...) INSERT INTO logging_event_property VALUES (1000, 'foo', ...) INSERT INTO logging_event_property VALUES (1000, 'bar', ...) INSERT INTO logging_event_property VALUES (1001, 'foo', ...) INSERT INTO logging_event_property VALUES (1001, 'bar', ...) -- etc INSERT INTO logging_event_exception VALUES (1000, 'line 1', ...) INSERT INTO logging_event_exception VALUES (1000, 'line 2', ...) INSERT INTO logging_event_exception VALUES (1001, 'line 1', ...) INSERT INTO logging_event_exception VALUES (1001, 'line 2', ...) -- etc Because of the INCREMENT BY clause, the generated IDs won't collide with another concurrent inserter. This should let you use both PreparedStatement and batch execution I think. There is a tradeoff between rate of ID consumption and maximum batch size to be made, but if you're using int8 for IDs then ID consumption is unlikely to be an issue.. -O