r/coding • • Mar 25 '21

SQLite is not a toy database

https://antonz.org/sqlite-is-not-a-toy-database/
269 Upvotes

44 comments sorted by

30

u/3meopceisamazing Mar 25 '21

Wow I had no idea SQLite could do all that. Amazing stuff!

27

u/andrerav Mar 25 '21

SQLite is absolutely amazing.

But.

I wish the support for geospatial data wasn't an ugly hack (Spatialite). And I also wish that there was an actual data type for datetimes.

9

u/BossOfTheGame Mar 26 '21

I wish it has an option to create a hash index, but that feature hasn't gone anywhere in 11 years: http://sqlite.1065341.n5.nabble.com/Feature-request-hash-index-td23367.html.

Is O(1) lookup too much to ask for on unique integer row ids?

2

u/[deleted] Mar 26 '21

wait wait. sqlite doesn’t hash indices? what the fuck

i thought any sane db did that

10

u/spinwizard69 Mar 26 '21

SQlite is sane for what it was targeting. No piece of software does everything thus you have to make choices about what to support. The KISS principal is at play here, there are plenty of complex, hard to use databases out there.

4

u/Beliriel Mar 26 '21

Is a hash lookup really that hard to implement?

1

u/bik1230 Mar 26 '21

They have a strong guarantee of never changing the disk format to ensure comparability across decades and decades. It may be that there's no simple way to add hash indices without changing the file format.

There's probably some way, but there's limited room for additions in the format, so Hipp wants to be very selective with additions. As an example, there is room for two more data types, and those are being saved in case something really important comes up in the future that really can't be done with one of the preexisting types (eg JSON and dates are both supported via functions on text).

There's also the question of priorities. A lot of new features to sqlite are driven by requests from well paying companies. For example page level checksumming was recently added at the request of a bunch of German companies.

1

u/[deleted] Apr 04 '21

Hashes are very easy to implement, but are a lot less useful in a typical DB query, compared to a b-tree. For example a hash won't help you sort by a column. It won't help you look up a range either (like "find users of age between 18 and 75").

Most coders are familiar with hashes and believe that's the only type of index. They aren't.

→ More replies (8)

1

u/[deleted] Mar 26 '21

okay, i completely agree with you

2

u/[deleted] Apr 03 '21

Actually most use btree. You don’t need a hash for this.

0

u/USER_NAME-Chad- Mar 26 '21 edited Mar 26 '21

Since sqllite is written in for TCL I have extended the functionality a lot by writing custom functions. It's a pain sometimes but also works really well.

Edit: Used in rather than for.

6

u/Rogntudjuuuu Mar 26 '21

Sqllite isn't written in TCL.

4

u/USER_NAME-Chad- Mar 26 '21 edited Mar 26 '21

Okay, so I guess I'll have to be specific. Not written in TCL. But as a C based extension for TCL with all of the original bindings being for TCL. So I'd say that it's very closely associated. Don't believe me?

From the book SQLite and Tcl, by Richard Hipp:

"SQLite is written in ANSI-C and comes bundled with bindings for Tcl. (Third-party binds for about two dozen other languages are also available on the internet. Only the TCL bindings are included with the core package.)"

-1

u/e_j_white Mar 26 '21

Does the datetime issue matter when using an ORM? I've used things like SQLAlchemy in Python and I'm pretty sure all of that is handled behind the scenes.

edit: typo

4

u/remy_porter Mar 26 '21

Right, it's handled behind the scenes, sure- but being able to index datetimes in the database would be a huge performance optimization for certain kinds of queries.

1

u/e_j_white Mar 26 '21

Oh, totally agreed. I didn't realize datetimes couldn't be indexed, that's definitely not optimal.

2

u/remy_porter Mar 26 '21

I mean, they aren't in the database, so you have to index them as another type, so you might turn them into ints or strings or something, but it's just that much more of a pain in the ass.

1

u/bik1230 Mar 26 '21

Sqlite has a whole bunch of functions for dealing with dates as either strings or numbers and you can totally index with those no problem.

→ More replies (1)

2

u/Kyraimion Mar 26 '21

I've used it effectively in various languages and the issue never got in the way. Having to re-parse timestamps for comparisons might become a performance problem, but it was never relevant in my projects

26

u/JRandomHacker172342 Mar 26 '21

I think one of the most impressive things about SQLite is its unbelievable test suite

9

u/SamHennessy Mar 26 '21

To add on, this is a cool concept about a to use it in a production environment to simplify things a lot if 99.9% uptime is acceptable https://litestream.io/blog/why-i-built-litestream/

4

u/SP3NGL3R Mar 26 '21

this is great. the number of times I'm fighting vendor data (CSV or JSON) at my work is endless. I'll surely be implementing these little things tomorrow. much appreciated.

3

u/Paddy3118 Mar 26 '21

I hate "big opaque boxes" in any flow I put together and when I need a DB I use SQLite as it is more "discoverable". One file, one process, tight standard lib Python integration.

2

u/Recolance Mar 26 '21

Yeah sql lite is good. Use it all the time. Big fan

2

u/bdforbes Mar 25 '21

It's a great option for simple, low overhead data modelling if you're a data analyst or data scientist using e.g. R or Python. If you use R and dplyr you can even use dplyr syntax directly on the DB using dbplyr

3

u/CodyEngel Mar 26 '21

Also good for mobile.

1

u/maus80 Apr 03 '21

SQLite is great, but I wish there was better support for online structure changes, such as alter column: https://stackoverflow.com/questions/4007014/alter-column-in-sqlite

2

u/[deleted] Apr 04 '21

A recent update significantly expanded SQLite's ALTER TABLE support:

https://sqlite.org/lang_altertable.html

Still not everything, but you can add columns for ex.

-12

u/o11c Mar 25 '21

Nobody cares about rows.

How many commits can it do per second without sacrificing reliability?

"Coalesce commits in user code" is not an answer.

6

u/quintus_horatius Mar 26 '21

90%+ of database work is reading, not writing.

Writing is actually pretty easy. The real power of sqlite, and sql in general, are the queries to select data back out. As the author points out, sqlite is actually quite rich in that regard.

The most frequent uses case I've seen for sqlite are static (or nearly static) database files that are used for fast lookup access. That's certainly not the only use case, but it's the most frequent I've seen.

If you're so worried about write speed, dump your shit to flat files and post-process into databases as needed.

5

u/[deleted] Mar 26 '21

[removed] — view removed comment

5

u/ptoki Mar 26 '21

AFAIR you could put dates or floats into strings and vice versa. Not sure if that is still true.

1

u/bik1230 Mar 26 '21

If you need to impose limits you can use check. Slightly annoying, but works.