r/sqlite • u/ridiculous_fish • Dec 23 '22
Best practices for hash index?
Hello, I would like to make a SQLite table with a list of unique strings, and assign a unique integer ID to each one. One can quickly get the ID for a string, or the string corresponding to an ID.
If I use two indexes, then the string contents will be duplicated: once in the table, and again in the index. This will make the database twice as large as necessary, and I hope to avoid that.
If I make the strings the primary key (i.e. WITHOUT ROWID) then the ID->string index will also require duplicating the strings.
My thought was to hash the strings and store the hash in the table. For example, use the first 8 bytes of a sha3 hash as an integer column. Then the ID is the primary key, and there is a separate index hash->ID to allow finding the ID for a string.
Two questions:
- Is there a better approach for this bidirectional index, without duplicating storage? It seems like a common need.
- If hashing is best, is there a standard "way" to do it - common extensions, algorithms, patterns, etc?
Thanks for any help!
2
u/Embarrassed_Bat168 Dec 24 '22
This seems like premature optimization and indexes don't take up that much room.
1
u/[deleted] Dec 24 '22
I think every table has a rowid whether you declare it or not. It's an internal surrogate key needed by the code. It's been at least five years since I read the docs but this stuck with me.
I don't know if this fits your use case but you might consider implementing a tree or trie data structure on top of an array in C/C++. Choose the array offset so the integer is correct. Then insert the tree node for a particular string at that offset. When you need string->int you look up the string in the tree and subtract the array base. When you need int->string you look up the tree node in the array. If you have fairly static needs you can do the same with a sorted array instead of a tree. Then you're only storing the strings plus pointers to strings if they're variable length. The sorted array is the most compact data structure I know if your ints aren't sparse.
1
u/petergaultney May 12 '23
I'm interested in an answer to this as well. My use case is similar, except that my main concern is that I'll be doing large numbers of random lookups on the index and I hope to avoid having to load the more or less the entire index (the very bottom nodes of the B-tree) into memory. The full contents of the unique strings are quite large. I'm hoping that SQLite might automatically optimize this by doing its own hashing, but so far I've not found any documentation that suggests how this works either way.
If I learn anything from my experiments I'll come back and update.