Traditional Count Distinct
The database uses the MPP architecture, which can retain the detailed data when using Count Distinct. However, the Count Distinct feature requires multiple data shuffles during query processing, which consumes more resources and results in a linear decrease in performance as the data volume increases. The following scenario calculates UVs based on detailed data in table (dt, page, user_id).
The database computes data according to the following figure. It first groups data by the
page and user_id columns, and then counts the processed result.

- Note: The figure shows a schematic of 6 rows of data computed on two BE nodes.
uv grouping by page:
Benefits of Count Distinct with Bitmap
You can benefit from bitmaps in the following aspects compared with COUNT(DISTINCT expr):- Less storage space: If you use bitmap to compute the number of distinct values for INT32 data, the required storage space is only 1/32 of COUNT(DISTINCT expr). The database utilizes compressed roaring bitmaps to execute computations, further reducing storage space usage compared to traditional bitmaps.
- Faster computation: Bitmaps use bitwise operations, resulting in faster computation compared to COUNT(DISTINCT expr). In the database, the computation of the number of distinct values can be processed in parallel, leading to further improvements in query performance.
Usage notes
- Both bitmap indexing and bitmap Count Distinct use the bitmap technique. However, the purpose for introducing them and the problem they solve are completely different. The former is used to filter enumerated columns with a low cardinality, while the latter is used to calculate the number of distinct elements in the value columns of a data row.
- StarRocks 2.3 and later versions support defining a value column as BITMAP regardless of the table types (Aggregate table, Duplicate Key table, Primary Key table, or Unique Key table). However, the sort key of a table cannot be of the BITMAP type.
- When creating a table, you can define the value column as BITMAP and the aggregate function as BITMAP_UNION.
- You can only use roaring bitmaps to compute the number of distinct values for data of the following types: TINYINT, SMALLINT, INT, and BIGINT. For data of other types, you need to build global dictionaries.
Count Distinct with Bitmap
Take the calculation of page UVs as an example.-
Create an Aggregate table with a BITMAP column
visit_users, which uses the aggregate function BITMAP_UNION. -
Load data into this table.
Use INSET INTO to load data:
After data is loaded:
- In the row
page_id = 1, visit_date = '2020-06-23 01:30:30', thevisit_usersfield contains three bitmap elements (13, 23, 33). - In the row
page_id = 1, visit_date = '2020-06-23 02:30:30', thevisit_usersfield contains one bitmap element (13). - In the row
page_id = 2, visit_date = '2020-06-23 01:30:30', thevisit_usersfield contains one bitmap element (23).
- In the row
-
Calculate page UVs.
Global Dictionary
Currently, Bitmap-based Count Distinct mechanism requires the input to be integer. If the user needs to use other data types as input to the Bitmap, then the user needs to build their own global dictionary to map other types of data (such as string types) to integer types. There are several ideas for building a global dictionary.Hive table-based Global Dictionary
The global dictionary itself in this scheme is a Hive table, which has two columns, one for raw values and one for encoded Int values. The steps to generate the global dictionary are as follows:- De-duplicate the dictionary columns of the fact table to generate a temporary table
- Left join the temporary table and the global dictionary, add
new valueto the temporary table. - Encode the
new valueand insert it into the global dictionary. - Left join the fact table and the updated global dictionary, replace the dictionary items with IDs.