It is similar to Book indexes at the end of the book
We have list of terms sorted alphabetically, so that the reader can find the interested term
for each term there is page number written so that reader can directly jump to the page
Let’s assume following data in person table
id
first_name
last_name
1
Liam
Miller
2
Fiona
Watson
3
Gary
Beckham
4
Harry
Miller
5
Louise
Smith
Suppose we create DB index on a column
-- create clustered index-- theoretically can be unique or not-- in practice it is always uniqueCREATE CLUSTERED INDEX idx_pnameON person (last_name, first_name);-- create non clustered indexCREATE INDEX idx_pnameON person (last_name, first_name);-- To create unique non-clustered index-- duplicate values are not allowedCREATE UNIQUE INDEX idx_pnameON person (last_name, first_name);
What happens is that internally, we will have a data structure, which will store list of rows sorted by last_name, and then first_name
The above query causes lookup in index idx_pname, since everything is already sorted, we can perform binary search in index to find last_name as Miller and then perform binary search on first_name as Liam on the rows with Miller as last_name