|
|
|
|
|
by elbee
4687 days ago
|
|
The B-tree implementations used in a lot of databases have tweaks, but they are surprisingly similar to the textbook descriptions. In general, B-trees are actually a very useful data structure when:
a) Your data is much larger than main memory (e.g. terabytes/petabytes of data and gigabytes of RAM).
b) You want to support random insert/update/delete, seek, and next/previous key.
c) There has to be a reasonable constraint on storage space (i.e. data density/compaction).
In those cases the log(n) performance of a b-tree is extremely hard to improve on. Alternate data structures a useful if:
1. The ratio of ram/disk is more favourable, in which case using an improved in-memory data structure can be more efficient.
2. Not all operations have to be supported. In particular eliminating the next/previous key operations allows something like a hashtable to be used. |
|
* Binary searches suck
The memory accesses of a binary search look pretty much like complete random access, so prefetching is useless. Once your b+ tree is big enough that most of it won't fit in L2, you've got a tight inner loop that's waiting on DRAM at every single iteration.
This one is at least more or less solvable without changing the rest of the design, but then if you also want to solve the other main issue it gets harder.
* With small btree nodes, the overhead of looking up and locking btree nodes (even assuming they're all cached in memory) kills you.
Trouble is if you make your btree nodes big enough to fix that overhead, they're now so big that rewriting them when you insert a single key is just ridiculous.
So where you end up is to get anywhere near the highest possible performance you're forced into log structured btree nodes. Which actually has its advantages for solving the first problem, but now what you've got is really a hybrid b+ tree/compacting data structure.
Source: bcache author.