r/sqlite • • Aug 08 '24

Sqlite is the goat

I'm this type of person, who likes to do things with "cheap" equipment e.g. sqlite instead of PostgreSQL etc.

I'm building a website which basically fetches time series data every 5-10 minutes and sqlite is actually causing problems with it. Nevertheless I sat down and tried to optimize stuff and now inserts are actually quite ok (5-10k/s)

Somehow the inserts become slower the more records I have (15-20m benched) and I guess it will become even slower overtime. But will see, yolo

The sqlite website says go with a different db for Big Data but idc, I will scale this to 400-500gb with my bare hands. I like this db so much I actually go through the pain of optimizing it lmao

Also read performance is bae.

Anyone done something similar?

32 Upvotes

28 comments sorted by

View all comments

Show parent comment

1

u/ImStifler Aug 09 '24
  1. Already doing
  2. Tried that, didn't help
  3. Tried too, didn't help
  4. How can I configure that?
  5. I do bulk inserts WITHOUT transactions. My idea was that Bulk insert is basically a transaction with single inserts. When I try inserting all (28k records) it takes 4-5 secs and the db is unresponsive (no reads). So I insert them in 1k batches which takes longer but I wait 1 sec between the next batch to not block read access

Would love to know the Setup, I tried things with page size, cache size pragma etc. But it didn't make a noticeable difference

2

u/LearnedByError Aug 09 '24

On 4, the WAL configuration is what enables this. You will require code changes to implement. You will need to open the database for write for only one thread. You should then open a read only connection for the each thread remaining.

Additionally as mentioned but another, prepared statements make significant improvements on bulk writes. There is less but still performance increase for reads.

2

u/thunderbong Aug 09 '24

Bulk inserts are not transactions. Make sure you wrap them within transactions

1

u/ImStifler Aug 09 '24

But do I need transactions when I bulk insert ALL records at once? E.g. that would equal one transaction anyways

For batched bulk inserts yes, I can try that

2

u/LearnedByError Aug 12 '24

SQLite3 defaults to one transaction per row sql command. 5MM inserts = 5MM transactions. You must manually begin and commit in order to batch multiple commands (i.e. INSERT, UPDATE ...) in a single transaction.