Every database picks a side: sort now or sort later. Most performance surprises you've hit come from which side it picked.
Think about your phone gallery. You could sort every photo into neat folders the moment you take it. Finding one later is quick, but every new photo means stopping to file it. Or you could dump everything into one big "Recent" pile. Saving is instant, but finding the photo from last month means scrolling forever.
You can't have both. Sorting is work, and the only question is when you do it. Every database makes this choice, and most of its weird behavior comes from it.
Disks are much faster at one long, continuous read or write than at jumping between scattered spots. Think of reading a chapter in order versus flipping to random pages. SSDs (the newer kind of storage) shrank that gap but didn't close it, because flash memory has to be erased in big blocks before it can be reused. Meanwhile data shows up in random order, so somebody has to sort it. That is just how it is.
A B-tree keeps everything sorted on disk in fixed-size chunks called pages, and updates them in place. To save a record, you find its page, read it, change it, and write it back. If a page gets full, it splits in two.
Reads are predictable. A lookup costs one or two real trips to disk, even in the worst case. And a range like "March to June" just walks through neighboring pages.
Writes cost more. Pages are usually 4 or 8 KB, so saving 100 bytes means writing thousands. Most databases also write the change to a log first, in case the power goes out, so those bytes land twice. This is called write amplification, and it's often ten times or more for a B-tree.
A log-structured merge-tree never reshuffles the shelf. New data goes into a sorted list in memory, with a log on disk as a crash backup. That's the whole write path.
When the list fills up, it gets written out in one long stretch as a file that is never changed again. An update is just a newer entry in a newer file. A delete writes a small marker called a tombstone (basically "ignore the old one").
Reads are where you pay. A record could be in memory or in any file, so you check newest first. Two things help.
Compaction is background work that competes with your normal work. Under steady heavy writes it can fall behind. Files pile up, reads slow down, and some databases deliberately slow your writes to let it catch up. So you get a database that was fast for six hours and then suddenly isn't.
Also, deleted data doesn't free space until compaction runs. A big delete can raise disk use for a while, because tombstones are records too.
LevelDB, RocksDB, Cassandra and HBase are LSM-trees. RocksDB sits quietly under many systems that never mention it.
| B-tree | LSM-tree | |
|---|---|---|
| Writing | scattered, in place | continuous, appended |
| Bytes written per byte saved | high | lower now, paid later by tidying |
| Finding one record | 1 to 2 disk trips, predictable | depends on file count |
| Reading a range | excellent | weaker; must merge files |
| Wasted space | half-empty pages | old data until tidying runs |
| Speed over time | steady | mostly fast, with tidying wobbles |
There's a formal version of this, the RUM conjecture: you can optimise reads, updates, or small size, and improving two costs you the third. Every "revolutionary" database is just a different point on that triangle, not a way out.
Usually nothing. Take the default. Three cases are worth a closer look.