views:

6852

answers:

9

I have a file of about 30000 lines of data I want to load into a sqlite3 database. Is there a faster way that generating insert statements for each line of data?

The data is space delimited and maps directly to the sqlite3 table. Is there any sort of bulk insert method for adding volume data to a database?

Has anyone devised some deviously wonderful way of doing this if it's not built in?

I should preface this by asking is there a c++ way to do it from the API?

Thanks.

+28  A: 
  • wrap all INSERTs in a transaction, even if there's a single user, it's far faster.
  • use prepared statements.
Javier
True for most (all?) SQL databases.
stesch
PRAGMA journal_mode = MEMORY;Might be helpful for some people
witkamp
+9  A: 

You can also try tweaking a few parameters to get extra speed out of it. Specifically you probably want PRAGMA synchronous = OFF;.

Ant P.
pragma synchronous = OFF is a bad idea - it'll hardly impact performance at all for bulk inserts, and your DB will be corrupted on a power failure. A much better idea is to wrap your inserts in a transaction.
Eamon Nerbonne
Thanks, that was so useful.
Ahmadreza
Wrapping the INSERTS in a TRANSACTION and usingPRAGMA journal_mode = MEMORY; Will prevent the INSERTs from hitting the disk until the end of the transaction.
witkamp
+11  A: 
  • Increase PRAGMA default_cache_size to a much larger number. This will increase the number of pages cached in memory.

  • Wrap all inserts into a single transaction rather than one transaction per row.

  • Use compiled SQL statements to do the inserts.
  • Finally, as already mentioned, if you are willing forgo full ACID compliance, set PRAGMA synchronous = OFF;.
ceretullis
+2  A: 

The following is a nice compendium of tips:

SQLite optimization

Depending on the size of the data and the amount of RAM available, one of the best performance gains will occur by setting sqlite to use an all-in-memory database rather than writing to disk.

For in-memory databases, pass NULL as the filename argument to sqlite3_open and make sure that TEMP_STORE is defined appropriately

(All of the above text is excerpted from my own answer to a separate sqlite-related question)

que que
The link points to an incomplete document. There is less information than one would hope for,
Richard
+13  A: 

You want to use the .import command. See below:

[ramanujan:~]$cat demotab.txt
44      92
35      94
43      94
195     49
66      28
135     93
135     91
67      84
135     94

[ramanujan:~]$echo "create table mytable ( col1 int, col2 int);" | sqlite3 foo.sqlite
[ramanujan:~]$echo ".import demotab.txt mytable"  | sqlite3 foo.sqlite

[ramanujan:~]$sqlite3 foo.sqlite
-- Loading resources from /Users/ramanujan/.sqliterc
SQLite version 3.6.6.2
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> select * from mytable;
col1    col2
44      92
35      94
43      94
195     49
66      28
135     93
135     91
67      84
135     94

Note that this bulk loading command is not SQL but rather a custom feature of sqlite. As such it has a weird syntax because we're passing it via echo to the interactive command line interpreter, sqlite3.

In Postgres the equivalent is COPY FROM: http://www.postgresql.org/docs/8.1/static/sql-copy.html

In MySQL it is LOAD DATA LOCAL INFILE: http://dev.mysql.com/doc/refman/5.1/en/load-data.html

One last thing: remember to be careful with the value of .separator. That is a very common gotcha when doing bulk inserts.

sqlite> .show .separator
     echo: off
  explain: off
  headers: on
     mode: list
nullvalue: ""
   output: stdout
separator: "\t"
    width:

You should explicitly set the separator to be a space, tab, or comma before doing .import.

ramanujan
+1, great answer
torial
+1  A: 

If you are just inserting once, I may have a dirty trick for you.

The idea is simple, first inserting into a memory database, then backup and finally restore to your original database file.

I wrote the detailed steps at my blog. :)

circle
A: 

There is no way to bulk insert, but there is a way to write large chunks to memory, then commit them to the database. For the C/C++ API, just do:

sqlite3_exec(db, "BEGIN TRANSACTION", NULL, NULL, NULL);

...(INSERT statements)

sqlite3_exec(db, "COMMIT TRANSACTION", NULL, NULL, NULL);

Assuming db is your database pointer.

scott
+1  A: 

Have a look at a prepared-transaction-insert-commit scenario (as already suggested)!

http://codesnippets.joyent.com/posts/show/2412

tollot
+1  A: 

RE: "Is there a faster way that generating insert statements for each line of data?"

First: Cut it down to 2 SQL statements by making use of Sqlite3's Virtual table API e.g.

create virtual table vtYourDataset using yourModule;
-- Bulk insert
insert into yourTargetTable (x, y, z)
select x, y, z from vtYourDataset;

The idea here is that you implement a C interface that reads your source data set and present it to SQlite as a virtual table and then you do a SQL copy from the source to the target table in one go. It sounds harder than it really is and I've measured huge speed improvements this way.

Second: Make use of the other advise provided here i.e. the pragma settings and making use of a transaction.

Third: Perhaps see if you can do away with some of the indexes on the target table. That way sqlite will have less indexes to update for each row inserted

Hannes de Jager