It is result day for a bank recruitment exam. Twenty thousand candidates sat the written test, and at five in the evening the result page goes live. You type your roll number, 104321, press Enter, and before you have time to worry, your name and marks are on the screen. Behind that page sits a table with one row for every candidate. The server did not read twenty thousand rows to find yours. It did not even read a hundred. It went almost straight to the right place, the way you go straight to the right page of a textbook by using the index at the back. A national ID database with crores of rows does the same, and so does your bank's app when it shows your last ten transactions out of millions.
How did it get there so fast? That's what this chapter is about. The trick is called indexing, and it's one of the database topics that bank and BCS papers ask about most often.
By the end of the chapter you'll be able to count what a search costs, in block reads, with and without an index. You'll tell dense from sparse, primary from secondary, and clustered from non-clustered. You'll draw a B+ tree, search it, insert into it and work out its height. You'll explain why a hash index can't answer a range query and where a bitmap index fits. And before you type CREATE INDEX, you'll be able to say whether that index will help or hurt.
We'll go in eight parts, from easy to hard: the cost of having no index, the simple one-level indexes, the multi-level index and the B+ tree that grows out of it, two specialists (the hash and bitmap indexes), and finally the SQL you write and the judgment you need.