- Categories:
Aggregate functions (Frequency Estimation) , Window function syntax and usage
APPROX_ TOP_ K_ ACCUMULATE¶
Returns the Space-Saving summary at the end of aggregation. (For more information about the Space-Saving summary, see Estimating Frequent Values.)
The function APPROX_TOP_K discards its internal, intermediate state when the final cardinality estimate is returned. However, in certain advanced use cases, such as estimating incremental frequent values during bulk loading, you might want to keep the intermediate state, in which case you would use APPROX_TOP_K_ACCUMULATE instead of APPROX_TOP_K.
In contrast to APPROX_TOP_K, APPROX_TOP_K_ACCUMULATE does not return a frequency estimate of items. Instead, it returns the algorithm state itself. The intermediate state can later be:
- Combined (that is, merged) with intermediate states from separate but related batches of data.
- Processed by other functions that operate directly on the intermediate state, for example, APPROX_TOP_K_ESTIMATE. (For an example, see the Examples section below.)
- 导出到外部工具。
- See also:
语法
实参
expr您想要查找最常见值的表达式(例如列名称)。
counters这是在估算过程中一次可以跟踪的非重复值数量上限。
For example, if
countersis set to 100000, then the algorithm tracks 100,000 distinct values, attempting to keep the 100,000 most frequent values.The maximum number of
countersis100000(100,000).
使用说明
- Decimal-float (DECFLOAT) values aren’t supported.
示例
此示例说明如何使用三个相关函数 APPROX_TOP_K_ACCUMULATE、APPROX_TOP_K_ESTIMATE 和 APPROX_TOP_K_COMBINE。
Note
此示例使用比不同数据值更多的计数器,以便获得一致的结果。在实际应用中,非重复值的数量通常大于计数器的数量,因此近似值可能会有所不同。
此示例生成一个表,其中包含值为 1-8 的 8 行,以及另一个包含 8 行的表,其值为 5-12。因此,两个表的并集中比较常见的值是值 5-8,每个值的计数为 2。
创建简单的表和数据:
Create a table that contains the “state” that represents the current
approximate Top K information for the table named sequence_demo:
Now create a second table and add data. (In a more realistic situation, the user could have loaded more data into the first table and divided the data into non-overlapping sets based on the time that the data was loaded.)
仅获取新数据的“状态”信息。
合并两批行的“状态”信息:
获取组合行集的近似 Top K 值: