HLL Approximate Deduplication
HLL (HyperLogLog) is an approximate deduplication solution based on a probabilistic algorithm. It estimates cardinality (distinct count) with compact state. The often-cited 1%-2% error is approximate and is not a bound for every dataset.
Use cases
Exact distinct counts can require substantial aggregation state and data exchange. HLL is an alternative when approximate counts meet the application’s accuracy requirements.
The HLL algorithm has the following characteristics:
| Dimension | Behavior |
|---|---|
| Storage | Compact sparse state for low cardinality and a fixed register array in dense form |
| Time complexity | O(n) |
| Error range | About 1%-2% (depends on the dataset and hash function) |
When the business can tolerate the error, using HLL for approximate deduplication is an effective way to reduce computational pressure and accelerate queries.
- Who it suits: scenarios with huge data volumes, tolerance for 1%-2% error, and a need to balance query performance and storage cost.
- How to use it: when creating a table, set the column type to
HLLand the aggregation function toHLL_UNION. During data loading, generate HLL values withHLL_HASH(). At query time, aggregate withHLL_UNION_AGG(). - Common uses: UV statistics, user deduplication, unique device count estimation.
What is HyperLogLog
HyperLogLog is an upgraded version of the LogLog algorithm. It provides imprecise cardinality (distinct count) estimation, and its mathematical foundation is the Bernoulli trial.
An intuitive explanation of the Bernoulli trial
- Assume a coin has two sides, and a single toss has a 50% probability of landing on either side.
- Keep tossing the coin until heads appears, and record this as one complete trial.
- Repeat for n trials, meaning heads has appeared n times. Let k denote the number of tosses in each trial, and let k_i denote the i-th trial.
- Across the n trials, there must be a maximum number of tosses, denoted as k_max (for example, in one trial it took 12 tosses for heads to appear).
Conclusions derived
- The number of tosses in each of the n Bernoulli trials is no greater than k_max.
- At least one of the n Bernoulli trials has a toss count equal to k_max.
As an intuition, observing a long run suggests more trials. The rough relation n ≈ 2^k_max illustrates the scale of the estimate, rather than an exact cardinality formula.
In other words, the observed run length provides information about the number of distinct hashed values. HLL applies a related idea to hashed values across multiple registers and combines their estimates, with corrections for small and large cardinalities.
Use HLL for approximate deduplication
Step 1: Create the table
Goal: Create an aggregate table that supports HLL deduplication.
Constraints:
Before running the example, select a database where you can create tables and load data.
- The target column type must be set to
HLL, and the aggregation function must be set toHLL_UNION. - An HLL-type column cannot be used as a Key column.
- You do not need to specify the length or default value. The length of an HLL column is internally controlled by the system based on the degree of data aggregation.
Example:
create table test_hll(
dt date,
id int,
name char(10),
province char(10),
os char(10),
uv hll hll_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: Convert raw detail data to an HLL column with HLL_HASH() and write it into the table.
Sample data (test_hll.csv):
2022-05-05,10001,test01,Beijing,windows
2022-05-05,10002,test01,Beijing,linux
2022-05-05,10003,test01,Beijing,macos
2022-05-05,10004,test01,Hebei,windows
2022-05-06,10001,test01,Shanghai,windows
2022-05-06,10002,test01,Shanghai,linux
2022-05-06,10003,test01,Jiangsu,macos
2022-05-06,10004,test01,Shaanxi,windows
Stream load command
curl --location-trusted --user "<sql-user>" -H "label:label_test_hll_load" \
-H "column_separator:," \
-H "columns:dt,id,name,province,os,uv=hll_hash(id)" -T test_hll.csv <stream-load-endpoint>/api/<database-name>/test_hll/_stream_load
Sample load result
This upstream example shows the expected success fields. Transaction IDs and timings vary by load.
{
"TxnId": 693,
"Label": "label_test_hll_load",
"TwoPhaseCommit": "false",
"Status": "Success",
"Message": "OK",
"NumberTotalRows": 8,
"NumberLoadedRows": 8,
"NumberFilteredRows": 0,
"NumberUnselectedRows": 0,
"LoadBytes": 320,
"LoadTimeMs": 23,
"BeginTxnTimeMs": 0,
"StreamLoadPutTimeMs": 1,
"ReadDataTimeMs": 0,
"WriteDataTimeMs": 9,
"CommitAndPublishTimeMs": 11
}
Step 3: Query data
Goal: Get approximate deduplication results through HLL aggregation functions.
Note:
Use functions such as
HLL_UNION_AGG()andHLL_CARDINALITY()to read cardinality estimates from HLL values.
Total UV
mysql> select HLL_UNION_AGG(uv) from test_hll;
+---------------------+
| hll_union_agg(`uv`) |
+---------------------+
| 4 |
+---------------------+
1 row in set (0.00 sec)
Equivalent form:
mysql> SELECT COUNT(DISTINCT uv) FROM test_hll;
+----------------------+
| count(DISTINCT `uv`) |
+----------------------+
| 4 |
+----------------------+
1 row in set (0.01 sec)
UV per day
mysql> select HLL_UNION_AGG(uv) from test_hll group by dt;
+---------------------+
| hll_union_agg(`uv`) |
+---------------------+
| 4 |
| 4 |
+---------------------+
2 rows in set (0.01 sec)
Related functions
| Function | Purpose |
|---|---|
HLL_UNION_AGG(hll) | Aggregation function used to compute the cardinality estimate of all matching data |
HLL_CARDINALITY(hll) | Computes the cardinality estimate of a single HLL column value |
HLL_HASH(column_name) | Generates an HLL column type, used during Insert or data loading (loading usage above) |
HLL_EMPTY() | Generates an empty HLL column, used to fill default values during insert or data loading |
The examples create persistent tables and consume storage and compute. After testing in a disposable database, drop test_hll if it is no longer needed.