Synchronizing sqlite database from memory to file

后端 未结 3 1786
梦如初夏
梦如初夏 2020-12-31 18:21

I\'m writing an application which must log information pretty frequently, say, twice in a second. I wish to save the information to an sqlite database, however I don\'t mind

相关标签:
3条回答
  • 2020-12-31 18:44

    Let's assume you have an on-disk database called 'disk_logs' with a table called 'events'. You could attach an in-memory database to your existing database:

    ATTACH DATABASE ':memory:' AS mem_logs;
    

    Create a table in that database (which would be entirely in-memory) to receive the incoming log events:

    CREATE TABLE mem_logs.events(a, b, c);
    

    Then transfer the data from the in-memory table to the on-disk table during application downtime:

    INSERT INTO disk_logs.events SELECT * FROM mem_logs.events;
    

    And then delete the contents of the existing in-memory table. Repeat.

    This is pretty complicated though... If your records span multiple tables and are linked together with foreign keys, it might be a pain to keep these in sync as you copy from an in-memory tables to on-disk tables.

    Before attempting something (uncomfortably over-engineered) like this, I'd also suggest trying to make SQLite go as fast as possible. SQLite should be able to easily handly > 50K record inserts per second. A few log entries twice a second should not cause significant slowdown.

    0 讨论(0)
  • 2020-12-31 18:55

    If you're executing each insert within it's own transaction - that could be a significant contributor to the slow-downs you're seeing. Perhaps you could:

    • Count the number of records inserted so far
    • Begin a transaction
    • Insert your record
    • Increment count
    • Commit/end transaction when N records have been inserted
    • Repeat

    The downside is that if the system crashes during that period you risk loosing the un-committed records (but if you were willing to use an in-memory database, than it sounds like you're OK with that risk).

    0 讨论(0)
  • 2020-12-31 18:55

    A brief search of the SQLite documentation turned up nothing useful (it wasn't likely and I didn't expect it).

    Why not use a background thread that wakes up every 10 minutes, copies all of the log rows from the in-memory database to the external database (and deletes them from the in-memory database). When your program is ready to end, wake up the background thread one last time to save the last logs, then close all of the connections.

    0 讨论(0)
提交回复
热议问题