Improve INSERT-per-second performance of SQLite

后端 未结 10 2220
忘掉有多难
忘掉有多难 2020-11-21 04:41

Optimizing SQLite is tricky. Bulk-insert performance of a C application can vary from 85 inserts per second to over 96,000 inserts per second!

Background:

10条回答
  •  后悔当初
    2020-11-21 05:46

    If you care only about reading, somewhat faster (but might read stale data) version is to read from multiple connections from multiple threads (connection per-thread).

    First find the items, in the table:

    SELECT COUNT(*) FROM table
    

    then read in pages (LIMIT/OFFSET):

    SELECT * FROM table ORDER BY _ROWID_ LIMIT  OFFSET 
    

    where and are calculated per-thread, like this:

    int limit = (count + n_threads - 1)/n_threads;
    

    for each thread:

    int offset = thread_index * limit
    

    For our small (200mb) db this made 50-75% speed-up (3.8.0.2 64-bit on Windows 7). Our tables are heavily non-normalized (1000-1500 columns, roughly 100,000 or more rows).

    Too many or too little threads won't do it, you need to benchmark and profile yourself.

    Also for us, SHAREDCACHE made the performance slower, so I manually put PRIVATECACHE (cause it was enabled globally for us)

提交回复
热议问题