How does bitmap index speed up a query compared to btree index?

1k Views Asked by At

I think it will give you a better understanding about where I'm coming from by letting you know how I understand how Btree indices work fundamentally. I'm not a DBA and I'm asking this question as a layman with basic understanding of data structures.

The basic idea of an index is that it speeds up searches by skipping significant amount of records when searching through a database.

AFAIK, binary tree data structure, which I presume where Btree indices are based on, helps us to search without scanning the entire database by dividing the data into nodes. For oversimplified example, words that start from A to M are stored in left node, and words that start with N to Z are stored in right node on the first level of the tree. In this case when we search for the word "Jackfruit" it will only search on the left node skipping the right node saving us significant amount of time and IO.

In this sense, how does a bitmap index let us not scan the entire database when searching? If not, how does it speed up searches? Or is it just meant for compression?

enter image description here

Image taken from here

The image above is a conceptual illustration of a bitmap. Using that structure, how does a DB find rows? Does it scan all rows? In binary tree, that fact that you don't have to scan everything is exactly how it helps speed up the search. I can't see any explanation how exactly a DB gets an advantage in searching for rows using bitmap other than the fact that bitmap takes less space.

2

There are 2 best solutions below

3
On

Btree indexes are good for key searching (duplicates allowed, but mainly distinct values in the column, ie. SSN). Bitmap indexes are better in cases when you have a few distinct values like 'sex', 'state', 'color', and so on.

1
On

Oracle bitmap indexes are very different from standard b-tree indexes. In bitmap structures, a two-dimensional array is created with one column for every row in the table being indexed. Each column represents a distinct value within the bitmapped index. This two-dimensional array represents each value within the index multiplied by the number of rows in the table.

Please see http://www.dba-oracle.com/oracle_tips_bitmapped_indexes.htm for a more detailed explanation.