CST363 - Week 5

When developers mention "slow indexes," they don't mean the index is broken or corrupted. They mean the query is still slow even though it’s using an index. That sounds backward at first because indexes are supposed to speed things up. The confusion comes from a shallow understanding of how index lookups actually work.

Most people think the database walks the tree, finds the value, and returns the result. But that's just the first part. In reality, there are three phases.

The first is tree traversal. This part is usually fast. Indexes don’t go very deep, maybe four or five levels max, so it takes only a few disk reads to get through the tree.

The second phase is scanning the leaf nodes. Say you're looking for the value 57. The database finds one match but has to keep scanning the next leaf nodes to make sure there aren't more. If the value appears often, it ends up reading a lot of leaf nodes.

The third phase is the real cost. Each match in the index points to a row in the table. These rows aren’t packed together neatly. They’re scattered all over the place. So now the database has to do a bunch of random disk reads just to grab the data. That’s where the performance hit comes in.

The index is working. The slowness comes from the high number of random table lookups, not from a broken index structure.

Still, people see bad performance and assume the tree must be the problem. So they rebuild the index, hoping it’ll help. But tree traversal wasn’t the issue, so rebuilding changes nothing.

Once you understand how index lookups actually work, the slowness makes sense. The index does its job, but the nature of the query and how the data is laid out causes all the overhead.

Comments

Popular Posts