Interview: How Database Indexing Works
Scanning an unsorted table of ten million users row by row will quickly choke your database. To fix this, databases build an index, which is a separate, sorted copy of a specific column paired with pointers back to the original rows. To search this sorted list without reading from top to bottom, the database organizes values into a hierarchical structure called a B-tree. The data is split into small blocks, while parent layers above them act as signposts that guide queries in the right direction. By navigating down these signposts, the database opens only the relevant block and jumps straight to the exact row in storage.
At real-world scale, just three or four B-tree levels can pinpoint one record among ten million in four quick steps instead of ten million disk checks. The major tradeoff is write overhead. While new rows simply get appended to the end of your main table, the index must remain sorted at all times. When a block fills up, the database has to split it and reorganize the signposts above it. If you put five indexes on one table, every single insert or update forces the database to maintain five separate trees. That is why production systems index only the columns that are frequently queried.
#Database #SystemDesign #SoftwareEngineering #TechExplained #SQL #CodingForBeginners
KodeKloud
...