Live data from Hacker News

The perils of UUID primary keys in SQLite

andersmurphy.com

31–40 of 117 posts

Re: The perils of UUID primary keys in SQLite

#31
post #30

Earlier quoted context omitted.

Fortunately we're seeing more JS DB libraries offering to read large numbers as the BigInt type.

But frustratingly, a JS BigInt is nothing like a BigInt in any other language. In JS - BigInt is 64bit integer. In anything else - BigInt is a arbitrarily large integer.

Hm? JavaScript BigInts are arbitrary precision, and you need to use methods like BigInt.asIntN(64, a) to convert them to 64 bits

Re: The perils of UUID primary keys in SQLite

#33
post #13

Isn't the solution just to use the rowid (after doing the read-id-after-insert dance)? How much trouble does SQLite reysing rowid's actually cause?

You don't even need to that. SQLite auto increments the ids and is a single writer (which you should be coordinating at the application level.

Regular rowids are definitely the way to go if you can use them.

Re: The perils of UUID primary keys in SQLite

#34
post #26
post #6

Earlier quoted context omitted.

':memory:' https://sqlite.org/inmemorydb.html

Except this source code is not using :memory: The linked source code has (defonce db (d/init-db! "db/db.db" {:pool-size 4 :pragma {:synchronous "FULL"}})) That's writing to disk.

Yes it's writing to disk (on a M1 mac which has terribly slow fsync). But, because of the transaction the fsync dance is done once per batch. Each row is the id + a 50 byte data blob.

There's only one index so there's no real write amplification. The numbers will go down as you add more data and indexes.

Re: The perils of UUID primary keys in SQLite

#36

Earlier quoted context omitted.

Oh yes, I meant don’t store as an ID in its string format!

It's just s dumb as storing dates as strings, but people still do it.

But also one of the recommended ways of doing it, as it has no native Datetime type.

Re: The perils of UUID primary keys in SQLite

#37
post #30

Earlier quoted context omitted.

Fortunately we're seeing more JS DB libraries offering to read large numbers as the BigInt type.

But frustratingly, a JS BigInt is nothing like a BigInt in any other language. In JS - BigInt is 64bit integer. In anything else - BigInt is a arbitrarily large integer.

This is simply not true? Or maybe I misunderstand what you mean?

Re: The perils of UUID primary keys in SQLite

#38
post #5

UUIDs are way over used. There is almost always a better key to use, usually a bigint for databases. If you're making some kind of leaderless distributed data store, then maybe, but even then there are other ID sharding strategies I'd go for first depending on the constraints. For a single database, bigints are smaller and faster, with less footguns. UUIDs can be nice for an opaque public ID, however I'd still prefer…

Yes this matters even more if you are doing a lot of joins. Naive string UUIDs are 32 bytes (though I use binary uuid in the post which is 16) compared to 8 bytes for a 64-bit int. This matters even more with sqlite as it uses varint encoding. The upshot of all this is your indexes take up a lot less space in memory.

Re: The perils of UUID primary keys in SQLite

#39

Earlier quoted context omitted.

Oh yes, I meant don’t store as an ID in its string format!

It's just s dumb as storing dates as strings, but people still do it.

But SQLite does not have a native datetime type so you have to use strings
Post reply on HN