Sampling-based Estimation of the Number of Distinct Values in Distributed Environment

Jiajun Li, Zhewei Wei, Bolin Ding, Xiening Dai, Lu Lu, Jingren Zhou · DOI

Abstract

Estimating the Number of Distinct Values (NDV) is critical for database optimization and network security. Traditional methods face a trade-off between high I/O costs (scanning-based) and high communication overhead (sampling-based) in distributed environments. This paper introduces a novel distributed framework that leverages a mathematical relationship between unique values and set unions to calculate the "frequency of frequency" ($f_1$) using mergeable sketches. This approach reduces communication costs by orders of magnitude and provides a 10x speedup over standard MapReduce implementations while maintaining high estimation accuracy.

Introduction

In large-scale distributed systems, estimating cardinality (NDV) is a fundamental challenge. Scanning-based methods like HyperLogLog (HLL) are accurate but suffer from massive I/O as data grows. Conversely, sampling-based methods are fast on single machines but fail in distributed settings because the master node requires the global count of items that appear exactly once ($f_1$). Traditionally, obtaining $f_1$ requires every worker to send its entire frequency dictionary, creating a "communication wall" that limits scalability.

Methodology: Sketch-based Distributed Sampling

The proposed framework overcomes the communication bottleneck by expressing $f_1$ using $\ell_0$ norms (distinct counts). For two samples $X$ and $Y$, the global $f_1$ is derived as: $$f_1(X + Y) = \|X_{=1} + Y\|_0 - \|X\|_0 + \|X + Y_{=1}\|_0 - \|Y\|_0$$

Since $\| \cdot \|_0$ can be estimated efficiently using mergeable sketches like HyperLogLog, workers only need to transmit small sketches (kilobytes) instead of full dictionaries (gigabytes).

The distributed sampling architecture: local machines perform sampling and frequency analysis, then send small sketches to a master node for global NDV estimation.

Computation Framework

  1. Local Sampling: Each machine samples its local data partition.
  2. Pre-Merged Sketches: A binary tree structure is used to build intermediate merged sketches, avoiding $O(k^2)$ complexity.
  3. Global Estimation: The master node solves for $f_1$ using the received sketches and applies classic statistical estimators such as GEE, Shlosser, or Chao Lee.

Experimental Evaluation

The framework was evaluated using synthetic Poisson and Zipfian datasets (up to $10^{12}$ elements) and the Star Schema Benchmark (SSB) with 900 million rows.

Experimental results showing the distribution of various keys (a, c, e), the drastic reduction in communication cost (b), estimation precision (d), and the 10x time speedup compared to direct merging (f).

Key Results

  • Communication Savings: On the SSB revenue dataset, communication costs were reduced from over 1200 MB to just a few megabytes.
  • Computational Speedup: Spark-based experiments demonstrated that the method is nearly 10x faster than standard MapReduce implementations.
  • Scalability: The I/O cost remains flat as data scales, unlike traditional methods that see linear increases in shuffle I/O.
  • Accuracy: By using HLL with a parameter $b \ge 12$, the framework achieves high precision in $f_1$ estimation, leading to NDV results nearly identical to full-data access methods.

Conclusion

By repurposing sketches to estimate specific frequency counts, this framework enables mathematically sound sampling estimators to scale efficiently in distributed environments. While the method assumes $f_1$ is not infinitesimally small, this holds for most real-world distributions. Future work will explore extending this "sketch-of-sample" approach to other statistical properties like entropy.

Summary updated

BibTeX
@inproceedings{li2022sampling,
  author = {Jiajun Li and Zhewei Wei and Bolin Ding and Xiening Dai and Lu Lu and Jingren Zhou},
  title = {{Sampling-based Estimation of the Number of Distinct Values in Distributed Environment}},
  booktitle = {ACM SIGKDD Conference on Knowledge Discovery and Data Mining},
  year = {2022},
  doi = {10.1145/3534678.3539390},
}
Figure