Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How binary search is used in database indexing

I know how binary search works but I wanted to know practical uses for binary search... I searched through the internet and I found that the main use is data base indexing, but I couldn't understand how binary search could help in data base indexing.

like image 443
user1205603 Avatar asked Feb 24 '12 18:02

user1205603


1 Answers

Binary search allows you to quickly look up a record by its key, assuming the keys are already sorted. This is especially true if the number of keys is large. 32 key reads would be sufficient to find any single unique key within a collection of two billion sorted keys.

Binary search works this way because each search attempt cuts the number of records to search in half.

That said, databases typically use some other binary tree-like data structure such as b-trees or red-black trees to perform the indexing. Using a binary tree removes the requirement that the list of keys be sorted before searching.

like image 147
Robert Harvey Avatar answered Sep 21 '22 06:09

Robert Harvey