BITMAP Precise Deduplication
BITMAP aggregates maintain sets of integer identifiers that can be reused for exact distinct counts. Compared with repeatedly deduplicating detail rows, precomputed sets can reduce aggregation work. Query time and storage depend on cardinality, value distribution, and the query plan.
How COUNT DISTINCT is implemented
Traditional precise deduplication relies on count distinct. Suppose the source data is as follows, and you need to perform precise deduplication on the name column:
| id | name |
|---|---|
| 1 | bob |
| 2 | alex |
| 3 | jack |
| 4 | tom |
| 5 | bob |
| 6 | alex |
When you run select count(distinct name) from t, one possible distributed plan computes the result as shown in the following diagram: it first does a group by on the name column to perform the first-stage deduplication, then shuffles the data and performs a second-stage deduplication, and finally computes count.

Because COUNT DISTINCT has to keep the detailed data during computation and requires shuffling, queries become slower as the data volume grows. Bitmap precise deduplication is designed to solve the performance problems of COUNT DISTINCT on large data volumes.
Use cases
Bitmap maps detailed data to bit positions, retaining a set of identifiers instead of full detail records. Consider using Bitmap for precise deduplication in the following scenarios:
| Scenario | Description |
|---|---|
| Query acceleration | Bitmap set operations can reuse pre-aggregated identifiers |
| Compressed storage | RoaringBitmap compresses sets of integer identifiers. Storage depends on value distribution and cardinality, rather than one allocated bit per input record |
Limitations:
- Direct bitmap construction requires nonnegative integer identifiers in the supported unsigned range. Use
TO_BITMAP()for supported integer input - For precise deduplication on other data types, you must build an additional global dictionary
- Columns of type Bitmap cannot be used as Key columns
VeloDB Cloud implements Bitmap precise deduplication based on RoaringBitmap. For the underlying principles and details, see RoaringBitmap.
Use BITMAP for precise deduplication
The overall workflow consists of three steps: create the table -> load data -> query the data.
Step 1: Create the table
Goal: Declare the target column as Bitmap type and configure the aggregate function BITMAP_UNION.
Before running the example, select a database where you can create tables and load data.
- When using Bitmap for deduplication, set the target column type to
Bitmapand the aggregate function toBITMAP_UNIONin theCREATE TABLEstatement - Columns of type Bitmap cannot be used as Key columns
Example: Create an aggregate table test_bitmap, where the id column represents the visiting user ID and the uv column has type BITMAP and uses the aggregate function BITMAP_UNION to aggregate data.
create table test_bitmap(
dt date,
id int,
name char(10),
province char(10),
os char(10),
uv bitmap bitmap_union
)
Aggregate KEY (dt,id,name,province,os)
distributed by hash(id) buckets 10;
Step 2: Load data
Use the Stream Load endpoint generated for your warehouse, including its protocol, for <stream-load-endpoint>. Replace <sql-user> and <database-name> with a SQL user that has load privileges and the database where you created the table. curl prompts for the password. Use the generated endpoint and TLS settings from Connect, and follow redirects only to the warehouse hosts listed there. See Stream Load for connection requirements.
Goal: Load the raw detailed data via Stream Load, and convert it to Bitmap type during loading using to_bitmap(id).
Sample data (test_bitmap.csv):
2022-05-05,10001,test 01,Beijing,windows
2022-05-05,10002,test 01,Beijing,linux
2022-05-05,10003,test 01,Beijing,macos
2022-05-05,10004,test 01,Hebei,windows
2022-05-06,10001,test 01,Shanghai,windows
2022-05-06,10002,test 01,Shanghai,linux
2022-05-06,10003,test 01,Jiangsu,macos
2022-05-06,10004,test 01,Shaanxi,windows
Stream Load command:
curl --location-trusted --user "<sql-user>" -H "label:label_test_bitmap_load" \
-H "column_separator:," \
-H "columns:dt,id,name,province,os, uv=to_bitmap(id)" -T test_bitmap.csv <stream-load-endpoint>/api/<database-name>/test_bitmap/_stream_load
Step 3: Query the data
Goal: Read the deduplicated result of the Bitmap column using the bitmap_union_count aggregate function.
Note:
Use
BITMAP_UNION_COUNT()for the distinct-count aggregate. Other bitmap functions, such asBITMAP_TO_STRING(), can inspect bitmap contents when needed.
Scenario 1: Total UV
mysql> select bitmap_union_count(uv) from test_bitmap;
+---------------------+
| bitmap_union_count(`uv`) |
+---------------------+
| 4 |
+---------------------+
1 row in set (0.00 sec)
This is equivalent to:
mysql> SELECT COUNT(DISTINCT id) FROM test_bitmap;
+----------------------+
| count(DISTINCT `id`) |
+----------------------+
| 4 |
+----------------------+
1 row in set (0.01 sec)
Scenario 2: UV per day
mysql> select bitmap_union_count(uv) from test_bitmap group by dt;
+---------------------+
| bitmap_union_count(`uv`) |
+---------------------+
| 4 |
| 4 |
+---------------------+
2 rows in set (0.01 sec)
FAQ
Q1: Which data types does Bitmap support for precise deduplication?
Use nonnegative integer identifiers accepted by TO_BITMAP(). To deduplicate strings or other values exactly, map them to unique integer IDs with a collision-free global dictionary. Hashing arbitrary strings into bitmap IDs does not guarantee exact counts.
Q2: Can a Bitmap column be used as a Key column?
No. In the Aggregate Key table shown here, the bitmap is a value column with BITMAP_UNION. Bitmap columns cannot be key columns.
Q3: Why can the raw values in a Bitmap column not be queried directly?
A bitmap stores a set of integer identifiers rather than the original detail records. Use BITMAP_UNION_COUNT() for an aggregate count or an inspection function such as BITMAP_TO_STRING() to view set members.
Q4: What advantages does Bitmap have over COUNT DISTINCT?
| Comparison | COUNT DISTINCT | BITMAP precise deduplication |
|---|---|---|
| Computation method | Keep detailed data + shuffle for deduplication | Bit operations |
| Query speed | Depends on plan, data, and cardinality | Pre-aggregation can reduce repeated work |
| Resource usage | Depends on aggregation state and shuffle | Depends on cardinality, distribution, and compression |
| Data types | Any type | Integer types only; other types require a global dictionary |
The examples create persistent tables and consume storage and compute. After testing in a disposable database, drop test_bitmap if it is no longer needed.