类别:

聚合函数 (基数估算)、窗口函数

HLL_ESTIMATE

返回给定 HyperLogLog 状态的基数估计值。

HLL_ACCUMULATEHLL_COMBINE 生成的 HyperLogLog 状态可用于使用 HLL_ESTIMATE 函数计算基数估计值。

因此,HLL_ESTIMATE(HLL_ACCUMULATE(...)) 等效于 HLL(...)。

另请参阅:

HLLHLL_ACCUMULATEHLL_COMBINE

语法

HLL_ESTIMATE( <state> )
Copy

实参

state

一个表达式,其中包含通过调用 HLL_ACCUMULATEHLL_COMBINE 生成的状态信息。

示例

此示例说明如何使用三个相关函数 HLL_ACCUMULATEHLL_ESTIMATEHLL_COMBINE

创建简单的表和数据:

-- Create a sequence to use to generate values for the table.
CREATE OR REPLACE SEQUENCE seq92;
CREATE OR REPLACE TABLE sequence_demo (c1 INTEGER DEFAULT seq92.nextval, dummy SMALLINT);
INSERT INTO sequence_demo (dummy) VALUES (0);

-- Double the number of rows a few times, until there are 8 rows:
INSERT INTO sequence_demo (dummy) SELECT dummy FROM sequence_demo;
INSERT INTO sequence_demo (dummy) SELECT dummy FROM sequence_demo;
INSERT INTO sequence_demo (dummy) SELECT dummy FROM sequence_demo;
Copy

创建一个包含“state”的表,该“state”表示名为 sequence_demo 的表的当前近似基数信息:

CREATE OR REPLACE TABLE resultstate1 AS (
     SELECT hll_accumulate(c1) AS rs1
        FROM sequence_demo);
Copy

现在创建另一个表并添加数据。 (在更实际的情况下,用户可以将更多数据加载到第一个表中,并根据加载数据的时间将数据划分为非重叠集)。

CREATE OR REPLACE TABLE test_table2 (c1 INTEGER);
-- Insert data.
INSERT INTO test_table2 (c1) SELECT c1 + 4 FROM sequence_demo;
Copy

仅获取新数据的“状态”信息。

CREATE OR REPLACE TABLE resultstate2 AS 
  (SELECT hll_accumulate(c1) AS rs1 
     FROM test_table2);
Copy

合并两批行的“状态”信息:

CREATE OR REPLACE TABLE combined_resultstate (c1) AS 
  SELECT hll_combine(rs1) AS apc1
    FROM (
        SELECT rs1 FROM resultstate1
        UNION ALL
        SELECT rs1 FROM resultstate2
      )
      ;
Copy

获取行组合集的近似基数:

SELECT hll_estimate(c1) FROM combined_resultstate;
Copy

输出:

+------------------+
| HLL_ESTIMATE(C1) |
|------------------|
|               12 |
+------------------+
Copy
语言: 中文