I’ve a mobile application. My client has a large data set ~100.000 records. It’s updated frequently.
When we sync we need to copy from one database to another.
I’ve attached the second database to the main, and run an insert into table select * from sync.table.
This is extremely slow, it takes about 10 minutes I think.
I noticed that the journal file gets increased step by step.
How can I speed this up?
EDITED 1
I have indexes off, and I have journal off.
Using
insert into table select * from sync.table
it still takes 10 minutes.
EDITED 2
If I run a query like
select id,invitem,invid,cost from inventory where itemtype = 1
order by invitem limit 50
it takes 15-20 seconds.
The table schema is:
CREATE TABLE inventory
('id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
'serverid' INTEGER NOT NULL DEFAULT 0,
'itemtype' INTEGER NOT NULL DEFAULT 0,
'invitem' VARCHAR,
'instock' FLOAT NOT NULL DEFAULT 0,
'cost' FLOAT NOT NULL DEFAULT 0,
'invid' VARCHAR,
'categoryid' INTEGER DEFAULT 0,
'pdacategoryid' INTEGER DEFAULT 0,
'notes' VARCHAR,
'threshold' INTEGER NOT NULL DEFAULT 0,
'ordered' INTEGER NOT NULL DEFAULT 0,
'supplier' VARCHAR,
'markup' FLOAT NOT NULL DEFAULT 0,
'taxfree' INTEGER NOT NULL DEFAULT 0,
'dirty' INTEGER NOT NULL DEFAULT 1,
'username' VARCHAR,
'version' INTEGER NOT NULL DEFAULT 15
)
Indexes are created like
CREATE INDEX idx_inventory_categoryid ON inventory (pdacategoryid);
CREATE INDEX idx_inventory_invitem ON inventory (invitem);
CREATE INDEX idx_inventory_itemtype ON inventory (itemtype);
I am wondering, the insert into … select * from isn’t the fastest built-in way to do massive data copy?
EDITED 3
SQLite is server-less, so please stop voting a particular answer, because that is not the answer I’m sure.
I don’t think that attaching the two databases and running
INSERT INTO foo (SELECT * FROM bar)is the fastest way to do this. If you are synching between a handheld device and a server (or another device) could the transport mechanism be the bottleneck? Or are the two database files already on the same filesysem? If the filesystem on the device is slower flash-memory, could this be a bottleneck?Are you able to compile/run the raw SQLite C code on your device? (I think that the RAW sqlite3 amalgamation should compile for WinCE/Mobile) If so, and you are willing:
It should be possible for to write a small stand-alone executable to copy/synchronize the 100K records between the two databases extremely quickly.
I’ve posted some of what I learned about optimizing SQLite inserts here: Improve INSERT-per-second performance of SQLite?
Edit: Tried this out with real code…
I don’t know all the steps involved in building a Windows Mobile executable, but the SQLite3 amalgamation should compile out-of-the box using Visual Studio. Here is a sample
main.cprogram that opens two SQLite databases (both have to have the same schema – see the#define TABLEstatement) and executes a SELECT statement and then binds the resulting rows to an INSERT statement:On my Windows desktop machine this code copies 100k records from
source.sqlitetodest.sqlitein 1.20 seconds. I don’t know exactly what kind of performance you’ll see on a mobile device with flash memory (but I am curious).