Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

slow sqlite insert using the jdbc drivers in java

I just inserted 1million records into a simple sqlite table with five columns. It took a whooping 18 hours in java using the jdbc drivers! I did the same thing in python2.5 and it took less than a minute. The speed for select queries seem fine. I think this is an issue with the jdbc drivers.

Is there a faster driver for sqlite3 in java?

Speed of inserting large numbers of rows is important for my schema migration script, and I'd rather not have to use an external script to do the migrations if I don't have to.

EDIT: fixed with connection.setAutoCommit(false); thanks Mark Rushakoff for hinting me to the solution :)

like image 725
Charles Ma Avatar asked Jul 30 '09 00:07

Charles Ma


People also ask

Why is SQLite so slow?

The SQLite docs explains why this is so slow: Transaction speed is limited by disk drive speed because (by default) SQLite actually waits until the data really is safely stored on the disk surface before the transaction is complete. That way, if you suddenly lose power or if your OS crashes, your data is still safe.

Does JDBC support SQLite?

SQLite JDBC is a library for accessing SQLite databases through the JDBC API.


2 Answers

Did you have your queries autocommitted? That could explain why it took so long. Try wrapping them in a begin / end so that it doesn't have to do a full commit for every insert.

This page explains begin/end transaction, while the FAQ touches on inserts/autocommits.

like image 157
Mark Rushakoff Avatar answered Nov 16 '22 03:11

Mark Rushakoff


If you want to further optimize, you can look into batching your insert queries together. So you can change 1 million inserts to 1000 inserts of 1000 batches.

like image 37
brianw220 Avatar answered Nov 16 '22 03:11

brianw220