pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feedBatches of single-insert statements vs batches of multi-insert statements
7+ messages / 5 participants
[nested] [flat]
* Batches of single-insert statements vs batches of multi-insert statements
@ 2016-06-05 18:17 Christopher Deckers <chrriis@gmail.com>
2016-06-05 18:33 ` Re: Batches of single-insert statements vs batches of multi-insert statements Vladimir Sitnikov <sitnikov.vladimir@gmail.com>
0 siblings, 1 reply; 7+ messages in thread
From: Christopher Deckers @ 2016-06-05 18:17 UTC (permalink / raw)
To: pgsql-jdbc
Hi all,
I tried all sorts of query rewrite to boost a huge sequence of insert
statements and here are my findings. All is run on a local Postgres 9.5.3,
on a 64-bit Windows box with 32GB RAM, i7@3.6GHz.
1. Batches of single insert statements (original case):
Batch of: INSERT INTO SomeTable (Col1, Col2) VALUES (?, ?)
-> 52 seconds
2. Batches of single insert statements to an unlogged table, then copy:
CREATE UNLOGGED TABLE SomeUnloggedTable AS SELECT * FROM SomeTable WHERE 1
= 0
Batch of: INSERT INTO SomeUnloggedTable (Col1, Col2) VALUES (?, ?)
INSERT INTO SomeTable SELECT * FROM SomeUnloggedTable
DROP TABLE SomeUnloggedTable
-> 43 seconds (15 seconds in the copy from unlogged to target table)
3. Batches of 100 multi-insert (followed by batches of single inserts for
remainder):
Batch of: INSERT INTO SomeTable (Col1, Col2) VALUES (?, ?), (?, ?), (?,
?)...
-> 30 seconds
So, rewriting the batch of single insert statements to batches of 100
multi-insert statements almost doubled the speed.
Could someone educate me as to what happens internally to the JDBC
single-insert batch that makes it so different from a multi-insert batch?
Rewriting our code to use multi-value inserts is more cumbersome and not
applicable everywhere. It also makes the code database-specific as all the
engines we support do not all handle this syntax.
These considerations actually lead me to this old thread:
https://www.postgresql.org/message-id/55130DC8.2070508%40redhat.com
Anyway, these tests definitely show that the performance of the standard
JDBC batch API could potentially be improved in Postgres internally with a
bit of magic :)
Isn't it possible (either in the driver or in the engine) to aggregate the
single-insert statements of a batch to turn them into a multi-insert
equivalent? It would not have to handle all cases: simply handling basic
commands like "INSERT INTO X (a, b, c, ...) VALUES (?, ?, ?, ...)" would
help a lot.
If this should not be done in the JDBC driver, then in which area could
such handling be done?
Or have I missed something?
If needed, I am ready to offer my assistance with testing (I can tweak
parameters, install dev drivers, modify my code to run alternate
statements, experiment, etc.).
Cheers,
-Christopher
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Batches of single-insert statements vs batches of multi-insert statements
2016-06-05 18:17 Batches of single-insert statements vs batches of multi-insert statements Christopher Deckers <chrriis@gmail.com>
@ 2016-06-05 18:33 ` Vladimir Sitnikov <sitnikov.vladimir@gmail.com>
2016-06-05 20:39 ` Re: Batches of single-insert statements vs batches of multi-insert statements Christopher Deckers <chrriis@gmail.com>
2016-06-05 20:48 ` Re: Batches of single-insert statements vs batches of multi-insert statements Thomas Kellerer <spam_eater@gmx.net>
0 siblings, 2 replies; 7+ messages in thread
From: Vladimir Sitnikov @ 2016-06-05 18:33 UTC (permalink / raw)
To: Christopher Deckers <chrriis@gmail.com>; pgsql-jdbc
Hi Christopher,
>Could someone educate me as to what happens internally to the JDBC
single-insert batch that makes it so different from a multi-insert batch?
There are significant overheads at the database level itself to initialize
and cleanup resources required for a "insert values" statement.
This is a known issue for backend developers, however it does not seem to
have easy solution.
See
https://www.postgresql.org/message-id/CAB=Je-EDZwVH7FgpDOa0ad8a7a6D=1QoYmvW2kQqSAML2gf+Uw@mail.gmail...
>If this should not be done in the JDBC driver, then in which area could
such handling be done?
>Or have I missed something?
Believe me or not, pgjdbc has already that feature implemented.
Jeremy did that in https://github.com/pgjdbc/pgjdbc/pull/491
The feature is disabled by default (so everybody can give it a try before
we make it default), and we plan to activate it by default.
To activate multi-inserts, you should add reWriteBatchedInserts=true
connection parameter.
You need 9.4.1209-SNAPSHOT version (you can grab that from Maven Central or
build from sources).
We all would be grateful if you could test it and let us know if it works
for you.
Vladimir
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Batches of single-insert statements vs batches of multi-insert statements
2016-06-05 18:17 Batches of single-insert statements vs batches of multi-insert statements Christopher Deckers <chrriis@gmail.com>
2016-06-05 18:33 ` Re: Batches of single-insert statements vs batches of multi-insert statements Vladimir Sitnikov <sitnikov.vladimir@gmail.com>
@ 2016-06-05 20:39 ` Christopher Deckers <chrriis@gmail.com>
2016-06-08 12:46 ` Re: Batches of single-insert statements vs batches of multi-insert statements Jeremy Whiting <jwhiting@redhat.com>
1 sibling, 1 reply; 7+ messages in thread
From: Christopher Deckers @ 2016-06-05 20:39 UTC (permalink / raw)
To: pgsql-jdbc
Hi Vladimir,
Thanks for your answer! It explains well the current situation.
Believe me or not, pgjdbc has already that feature implemented.
>
Oh good! But it definitely needs more testing and fixing :)
I got: "Batch entry 0 INSERT INTO XXXX".
Next exception: "VALUES lists must all be the same length".
I narrowed down the issue to a simple test case, see attached file.
Funny enough, if I change the name of the table, it seems to work. I used
the convention that we have in our production code.
Note that this test case does not produce any error if I remove the
"reWriteBatchedInserts=true" parameter.
Please let me know if I can be of any help!
-Christopher
--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc
Attachments:
MultiInsertTest.java (application/octet-stream, 2.5K) download
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Batches of single-insert statements vs batches of multi-insert statements
2016-06-05 18:17 Batches of single-insert statements vs batches of multi-insert statements Christopher Deckers <chrriis@gmail.com>
2016-06-05 18:33 ` Re: Batches of single-insert statements vs batches of multi-insert statements Vladimir Sitnikov <sitnikov.vladimir@gmail.com>
2016-06-05 20:39 ` Re: Batches of single-insert statements vs batches of multi-insert statements Christopher Deckers <chrriis@gmail.com>
@ 2016-06-08 12:46 ` Jeremy Whiting <jwhiting@redhat.com>
2016-06-08 13:15 ` Re: Batches of single-insert statements vs batches of multi-insert statements Christopher Deckers <chrriis@gmail.com>
0 siblings, 1 reply; 7+ messages in thread
From: Jeremy Whiting @ 2016-06-08 12:46 UTC (permalink / raw)
To: Christopher Deckers <chrriis@gmail.com>; +Cc: pgsql-jdbc
Hi Christopher,
I suggest you avoid trying to hard coding data values in the statement
sql. Instead use parameter place-holders (?) and use the API to bind the
value. You will find it works much better for you. Like this ....
String insert = "INSERT INTO T_AbCd (T_AbCd_ID, SomeDate, ASmallInt)
VALUES (?, ?, ?)";
PreparedStatement pst = conn.prepareStatement(insert,
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
pst.setLong(1, 24202712L);
pst.setDate(2, new java.sql.Date(2014-1900, 3-1, 21));
pst.setInt(3, 0);
pst.addBatch();
pst.setLong(1, 24202713L);
pst.setDate(2, new java.sql.Date(2014-1900, 3-1, 22));
pst.setInt(3, 0);
pst.addBatch();
pst.executeBatch();
The reason is the batched re-write feature builds dynamically the sql
issued to the back end. Using parameter place-holders. So you can see
hard coding data values isn't going to work without some horribly
complex statement parsing added to the driver. This time parsing data
and recognizing all data values and type permutations. yikes
I'd say you have these options. If you want to enable batched re-writing.
a) Change your code using the same style of coding as the above example.
Re-deploying to production.
b) If re-deploy to production isn't possible in the short term try using
Byteman [1]. Byteman will unobtrusively mend your existing code. See the
attached Byteman rule. The attached rule fixes your test class.
c) Provide a patch to add implement sql statement parsing support for
hard coded data. I see you have already started to try this [2].
Regards,
Jeremy
[1] http://byteman.jboss.org/
[2] https://github.com/pgjdbc/pgjdbc/pull/580
On 05/06/16 21:39, Christopher Deckers wrote:
> Hi Vladimir,
>
> Thanks for your answer! It explains well the current situation.
>
> Believe me or not, pgjdbc has already that feature implemented.
>
>
> Oh good! But it definitely needs more testing and fixing :)
> I got: "Batch entry 0 INSERT INTO XXXX".
> Next exception: "VALUES lists must all be the same length".
>
> I narrowed down the issue to a simple test case, see attached file.
>
> Funny enough, if I change the name of the table, it seems to work. I
> used the convention that we have in our production code.
> Note that this test case does not produce any error if I remove the
> "reWriteBatchedInserts=true" parameter.
>
> Please let me know if I can be of any help!
> -Christopher
>
>
>
--
Jeremy Whiting
Senior Software Engineer, Middleware Performance Team
Red Hat
------------------------------------------------------------
Registered Address: Red Hat UK Ltd, 64 Baker Street, 4th Floor, London. W1U 7DF. United Kingdom.
Registered in England and Wales under Company Registration No. 03798903. Directors: Directors:Michael Cunningham (US), Michael O'Neill(Ireland), Eric Shander (US)
--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc
Attachments:
FixMultiInsertTest.btm (text/plain, 488B) download
# Rules to fix test code
RULE Fix hard coded data in sql parameter
CLASS postgres.MultiInsertTest
METHOD main
AT LINE 36
IF TRUE
DO $insert = "INSERT INTO T_AbCd (T_AbCd_ID, SomeDate, ASmallInt) VALUES (?, ?, ?)";
ENDRULE
RULE Fix hard coded data in sql first
CLASS postgres.MultiInsertTest
METHOD main
AT LINE 41
IF TRUE
DO $pst.setInt(3, 0);
ENDRULE
RULE Fix hard coded data in sql second
CLASS postgres.MultiInsertTest
METHOD main
AT LINE 38
IF TRUE
DO $pst.setInt(3, 0);
ENDRULE
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Batches of single-insert statements vs batches of multi-insert statements
2016-06-05 18:17 Batches of single-insert statements vs batches of multi-insert statements Christopher Deckers <chrriis@gmail.com>
2016-06-05 18:33 ` Re: Batches of single-insert statements vs batches of multi-insert statements Vladimir Sitnikov <sitnikov.vladimir@gmail.com>
2016-06-05 20:39 ` Re: Batches of single-insert statements vs batches of multi-insert statements Christopher Deckers <chrriis@gmail.com>
2016-06-08 12:46 ` Re: Batches of single-insert statements vs batches of multi-insert statements Jeremy Whiting <jwhiting@redhat.com>
@ 2016-06-08 13:15 ` Christopher Deckers <chrriis@gmail.com>
0 siblings, 0 replies; 7+ messages in thread
From: Christopher Deckers @ 2016-06-08 13:15 UTC (permalink / raw)
To: Jeremy Whiting <jwhiting@redhat.com>; +Cc: pgsql-jdbc
Hi Jeremy,
> I suggest you avoid trying to hard coding data values in the statement
> sql.
>
The resulting SQL is not hand-crafted, and is built through all sorts of
layers depending on various rules. The end result that I exposed does not
tell how it was built.
Note that from a theoretical point of view, if I were to add parameters for
fixed values, then I would send through the wire as many times this
parameter, while if it is part of the statement the driver can decide to
only send the dynamic parameters. Also, we do have external multi-insert
logic for that specific part of the code, and it also improves speed with
jTDS; but SQL Server is limited to 2000 dynamic parameters, so if we waste
those parameters with static values, less batches can go through at once.
In any case, it must be supported by the batch rewrite feature, whether
people like the style or not :)
> c) Provide a patch to add implement sql statement parsing support for hard
> coded data. I see you have already started to try this [2].
>
In fact, the latest version of my patch hooks into the existing query
parsing, and just has to record the start and end of the values block. That
combined with the positions of the parameters made the latest version
reliable.
That being said, there is currently no safety in case we put too many
batches overflowing Postgres limits, which is what I will try to work on
when I get some free time.
Cheers,
-Christopher
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Batches of single-insert statements vs batches of multi-insert statements
2016-06-05 18:17 Batches of single-insert statements vs batches of multi-insert statements Christopher Deckers <chrriis@gmail.com>
2016-06-05 18:33 ` Re: Batches of single-insert statements vs batches of multi-insert statements Vladimir Sitnikov <sitnikov.vladimir@gmail.com>
@ 2016-06-05 20:48 ` Thomas Kellerer <spam_eater@gmx.net>
2016-06-05 21:18 ` Re: Re: Batches of single-insert statements vs batches of multi-insert statements Dave Cramer <pg@fastcrypt.com>
1 sibling, 1 reply; 7+ messages in thread
From: Thomas Kellerer @ 2016-06-05 20:48 UTC (permalink / raw)
To: pgsql-jdbc
Vladimir Sitnikov schrieb am 05.06.2016 um 20:33:
> The feature is disabled by default (so everybody can give it a try before we make it default), and we plan to activate it by default.
> To activate multi-inserts, you should add reWriteBatchedInserts=true connection parameter.
> You need 9.4.1209-SNAPSHOT version (you can grab that from Maven Central or build from sources).
Is there a reason why that build is not available on https://jdbc.postgresql.org/download.html ?
--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Re: Batches of single-insert statements vs batches of multi-insert statements
2016-06-05 18:17 Batches of single-insert statements vs batches of multi-insert statements Christopher Deckers <chrriis@gmail.com>
2016-06-05 18:33 ` Re: Batches of single-insert statements vs batches of multi-insert statements Vladimir Sitnikov <sitnikov.vladimir@gmail.com>
2016-06-05 20:48 ` Re: Batches of single-insert statements vs batches of multi-insert statements Thomas Kellerer <spam_eater@gmx.net>
@ 2016-06-05 21:18 ` Dave Cramer <pg@fastcrypt.com>
0 siblings, 0 replies; 7+ messages in thread
From: Dave Cramer @ 2016-06-05 21:18 UTC (permalink / raw)
To: Thomas Kellerer <spam_eater@gmx.net>; +Cc: pgsql-jdbc
It's a snapshot, not a release
Dave Cramer
davec@postgresintl.com
www.postgresintl.com
On 5 June 2016 at 16:48, Thomas Kellerer <spam_eater@gmx.net> wrote:
> Vladimir Sitnikov schrieb am 05.06.2016 um 20:33:
>
>> The feature is disabled by default (so everybody can give it a try before
>> we make it default), and we plan to activate it by default.
>> To activate multi-inserts, you should add reWriteBatchedInserts=true
>> connection parameter.
>> You need 9.4.1209-SNAPSHOT version (you can grab that from Maven Central
>> or build from sources).
>>
>
> Is there a reason why that build is not available on
> https://jdbc.postgresql.org/download.html ?
>
>
>
>
>
>
> --
> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-jdbc
>
^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2016-06-08 13:15 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-06-05 18:17 Batches of single-insert statements vs batches of multi-insert statements Christopher Deckers <chrriis@gmail.com>
2016-06-05 18:33 ` Vladimir Sitnikov <sitnikov.vladimir@gmail.com>
2016-06-05 20:39 ` Christopher Deckers <chrriis@gmail.com>
2016-06-08 12:46 ` Jeremy Whiting <jwhiting@redhat.com>
2016-06-08 13:15 ` Christopher Deckers <chrriis@gmail.com>
2016-06-05 20:48 ` Thomas Kellerer <spam_eater@gmx.net>
2016-06-05 21:18 ` Dave Cramer <pg@fastcrypt.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox