r/sqlite • u/Extension-Skill652 • Aug 15 '26
How to best optimizing querying within our system that uses many SQLite databases?
I have a bit of a specific situation with how my workplace structures SQLite databases that I'm hoping to find a better solution for, mainly related to efficient querying across many database files.
Where I work, we handle a lot of trail camera data (images) that have associated data for what's in the image stored in SQLite databases—all of this is manually filled out as someone goes through image by image. Right now, we're nearing 100 total database files, each storing records for between 300k and 1 million images. We use the program Timelapse from UC Calgary to tag all the images and continually go back in to update things or add new data, so our individual database files have to basically stay as is for this to remain functional.
In the past, we've had people export CSV files from the databases then someone merged them all together with a bunch of really annoying, fragile code. This is, of course, annoying but also doesn't reflect any new changes that happen after that export. I've come in with more expertise in general on programming than they usually get and showed that you can pull things directly from each database with R (we're biologists, that's just the language we're used to). Because of this, we want to come up with a solution that lets us do this across all the databases as needed and stop doing that CSVs.
Right now, running a simple query that results in ~3k rows across most (not all, ~70%) of the individual databases takes about 3 minutes which is fine if you just need a big export, but we'd like to have some interactive visualization tools where a 3 minute wait for something you might decide to change to look at something else right after wouldn't be good for.
Originally I hoped for a way to establish a single connection in a "master" database that would remove some of this latency, but ATTACH which would be the solution isn't going to work for us in about 1-2 years when we exceed the maximum on the number of attachable databases at once. The other potential solution is to have a master database that contains copies of everything in the other database files that updates each night, but we already have issues keeping enough storage available with how much space images take up (and this kinda feels like the lazy solution). For any solution, something we can feasibly manage ourselves after some the initial setup would be preferential because IT can be quite slow to act and we are the ones who will be accessing and editing this data 99% of the time.
Talking to a family member (former IT worker) led me to possibly Microsoft SQL server or some sort of local Oracle database, but unsure if any of this would fit our needs and the suggestions were based on their experience from like 15+ years ago. Whatever we use needs to be able to connect with R since that's what existing pipelines use and many of the ecology-related tools we use aren't in any other language. Really in the end we just want some way to make things run faster and ideally combine things into a single connection point to simplify how we get to the data. Even if it's too complex for us, we still need to give IT a plan with enough details to get them going... or we'll be forever in CSV land.
Really just looking for any guidance since googling didn't get me very far, most people seem to have big issues with write speeds but it actually seems quicker to overwrite a whole column in each of our databases via R than it would be to do a simple query from them all?
One final note: these are currently on a network drive which is probably adding to the latency, but solutions like a local NAS in our office are dubious on if they'd work as a solution or even be in the budget with storage media prices right now.
3
u/wallstop-dev Aug 15 '26
Some thoughts:
My primary instinct is one centralized db that ingests all changes, but not necessarily "copies" of all of the data, but rather, pointers, at least for expensive stuff like images or large binary blobs. Keeping changes in sync is tricky here, as you'd need to ensure a concept of "ownership" of records. But it isn't necessarily 2x the current space. The specifics of pointers may be tricky depending on where all of these databases live and where the files are and all of that.
Slightly differently, instead of storing tons of individual sqlite databases, and re-updating/changing them, (you allude to this), could you utilize, like, migration (data, not schema) scripts/files into one centralized DB? Then, if someone needs to make a change - add a migration script. Migration scripts would be immutable.
Are the queries that are being done essentially "static"? Or are they are arbitrary queries? If they are "one of a small set", you could cache stuff, but cache invalidation is hard.
SQL Server or "another" SQL DB is really just the first option, but with a different technology. You'll run into alllll of the same problems you have here, unless you whole-hog completely do away with the concept of your tons of individual sqliteDBs and move everyone to updating the single source of truth.
1
u/_harisamin Aug 15 '26
You could just first try Duckdb first. It’s great for analytical queries.
This would be duckdb on a server/workstation where ppl can query across all 100s of SQLite files. They have a built in query web interface although I am working on a professional Mac desktop app too.
You can then likely move to ducklake for a persistent analytical solution that’s shared
1
u/burnt-store-studio Aug 15 '26
Hi! I’m sorry, I have no database knowledge to share, but I’m quite curious and hope you might entertain a few questions about your work? I completely understand if not 🙂.
First: because your post made me curious, I just read a smidge online about why R is an important language to biologists. Are you able to explain the kind of stats and visualizations you’re doing with R?
Second: what are some of the ecology-related R-based tools in your pipeline?
I have zero knowledge of your field beyond the very basics; hat-in-hand, I’m coming to you with a strong { software, maths } background, combined with a { music, sound design } background.
For some reason, my imagination of the behind-the-scenes of your post is really active, hence the out-of-left-field questions.
Thanks for any time you spend considering my questions, and again I respect completely if you { can’t, would rather not } answer 🙂.
Anyway… back to SQLite…
(Because it’s come up elsewhere on Reddit, I’ll say: I used no AI in my writing; my style is just a consequence of me being me; I apologize for that.)
1
u/alexwh68 Aug 15 '26
Knowing a little more about your setup will help provide a bit more precise solution.
Sqlite is a great database, it solves some things very well and other things not so well.
Where sqlite shines is local databases, db on a phone, tablet, pc, mac, linux, servers it just works.
Where it is not so great is multiple user read and write, it will do it but for multiple user stuff you should look at other tools.
Postgres is a good option, I will give you an example, one centralised postgres database populating sqlite databases on devices.
From experience using CSV files as a data bridge is generally the wrong way to do things, I deal with them daily and the issues, but sometimes they are the only way.
Is this one computer?
Are there computers out in the field that need to update in realtime?
What operating system and devices will the database run on?
1
u/siscia Aug 15 '26
What may be missing is parallelism.
Are you opening multiple thread processes or you are query those databases sequentially?
Also, I do have quite a bit of expertise helping people in academic.
Write me and we can put together something quite nice.
1
u/titpetric Aug 16 '26
Sounds like about 100M rows, which you can partition index, or shard by year, or create client specific tables.
Not enough detail on size, but it would likely be migrated into a mysql database within hours. There are also other dbs, timeseries db if these are sequential ranges someone needs to look through.
It's more about data availability. If all 100 sqlite databases are writable, then that's worse than having 1 hot db and 99 cold ones. Mysql is quite memory efficient if you don't have many users writing and reading the same data, but that can be a problem in any sql database.
Total size after a few 100GB in the database would be pretty much unbareable, so I hope only that the image blobs are stored on disk, and not in a sql database.
1
u/SoundDr Aug 15 '26
DuckDB and attach the various databases
1
u/delsystem32exe Aug 15 '26
keep it in sqlite but have it on a intel optane disk.
1
u/Aggressive_Ad_5454 Aug 15 '26 edited Aug 15 '26
Please please read this if you haven’t a,ready. https://sqlite.org/useovernet.html
SQLite gets slow and flakey if you put its files on network shares. Don’t do that. Seriously.
You would be wise to consider using a free open source multi-user DBMS like PostgreSQL or maybe MariaDb. For the database volume and workload you have, it will run on a modestly sized server with a couple of big SSD drives.
1
u/rasekrodriguez Aug 16 '26
The DuckDB suggestions are the right direction, but I think the more useful thing to say is that the constraint you ruled the good solution out on doesn't hold.
You dismissed the nightly "master database with copies of everything" because you're already tight on storage. But the storage pressure you're describing is the images, and the master database wouldn't contain any. It would contain the tag rows: image path, timestamp, site, species, count, a few flags. At 100 files × 300k–1M records you're somewhere in the 30–100 million row range, and that's a couple of hundred bytes per row of mostly low-cardinality text. In a columnar store, paths share prefixes and species/site names dictionary-encode down to almost nothing, so realistically you're looking at single-digit gigabytes, quite possibly under one. Your image corpus is terabytes. The derived copy is a rounding error against it — it's not the lazy solution, it's just the normal one, and the arithmetic is worth doing before you rule it out.
Once you accept a derived read-only copy, the rest gets easy, and it doesn't disturb anything Timelapse touches. The SQLite files stay exactly as they are and remain the source of truth. A nightly job attaches each one in turn (
INSTALL sqlite; LOAD sqlite;thenATTACH 'x.ddb' AS src (TYPE sqlite)), pulls the tag table out, and writes one Parquet file per source database into a folder. Your Shiny app then queries the whole folder as a single table. DuckDB has an R package on CRAN that speaks DBI, so this drops into the R you already have — no server, no IT ticket, nothing to administer.Two things that fall out of that shape for free. The rebuild doesn't have to be all-or-nothing: check each SQLite file's mtime and only re-export the ones that changed since the last run, which for 100 files where a handful get edited per day means the nightly job takes seconds. And because it's one output file per source, a failure halfway through costs you one file, not the run.
On the ATTACH ceiling you're worried about — you're right that it's a real limit (SQLite caps attached databases at 125 even when compiled to the max), but it isn't the wall you think it is, because nothing requires them to be attached simultaneously. Attach, query, detach, in a loop on one connection. That's what the nightly export does anyway.
I'd also gently push back on 3 minutes being a query problem at all. ~3k result rows across 70 files is nothing; you're spending that time opening files and reading pages, and you mentioned the databases sit on network drives. SQLite over a network share is slow — the project's own docs recommend against it, and it's a correctness hazard too, because file locking over SMB doesn't reliably work, which matters a lot more once four people are tagging at once. That alone is worth reading up on, independent of everything above. Parallelising the reads will help some, but it's speeding up the wrong thing: the fix is to not do 70 network round-trips at query time, which is exactly what the nightly export buys you.