percentile_approx_weight
返回带权重的 pth 百分位数近似值。percentile_approx_weight
是 PERCENTILE_APPROX
的加权版本,允许用户为每个输入值指定权重(一个常量值或数值列)。
此函数使用固定大小的内存,因此可以使用较少的内存来处理具有高基数的列。 它还可以用于计算 tp99 等统计信息。
语法
DOUBLE PERCENTILE_APPROX_WEIGHT(expr, BIGINT weight, DOUBLE p[, DOUBLE compression])
expr
:要计算百分位数的列。pth
:百分位数。范围:[0, 1]。例如,0.99
表示第 99 个百分位数。weight
:权重列。它必须是一个正的常量数字或列。compression
:(可选)压缩比。范围:[2048, 10000]。该值越大,精度越高,内存消耗越大,计算时间越长。如果未指定此参数或该值超出 [2048, 10000] 的范围,则使用默认值10000
。
示例
CREATE TABLE t1 (
c1 int,
c2 double,
c3 tinyint,
c4 int,
c5 bigint,
c6 largeint,
c7 string,
c8 double,
c9 date,
c10 datetime,
c11 array<int>,
c12 map<double, double>,
c13 struct<a bigint, b double>
)
DUPLICATE KEY(c1)
DISTRIBUTED BY HASH(c1)
BUCKETS 1
PROPERTIES ("replication_num" = "1");
insert into t1
select generate_series, generate_series, 11, 111, 1111, 11111, "111111", 1.1, "2024-09-01", "2024-09-01 18:00:00", [1, 2, 3], map(1, 5.5), row(100, 100)
from table(generate_series(1, 50000, 3));
-- use constant value as weight
mysql> select percentile_approx_weighted(c1, 1, 0.9) from t1;
+----------------------------------------+
| percentile_approx_weighted(c1, 1, 0.9) |
+----------------------------------------+
| 45000.39453125 |
+----------------------------------------+
1 row in set (0.07 sec)
-- use a numeric column as weight
mysql> select percentile_approx_weighted(c2, c1, 0.5) from t1;
+-----------------------------------------+
| percentile_approx_weighted(c2, c1, 0.5) |
+-----------------------------------------+
| 35355.9375 |
+-----------------------------------------+
1 row in set (0.07 sec)
-- use weight and compression to compute percentile
mysql> select percentile_approx_weighted(c2, c1, 0.5, 10000) from t1;
+------------------------------------------------+
| percentile_approx_weighted(c2, c1, 0.5, 10000) |
+------------------------------------------------+
| 35355.9375 |
+------------------------------------------------+
1 row in set (0.09 sec)
关键词
PERCENTILE_APPROX_WEIGHT,PERCENTILE_APPROX,PERCENTILE,APPROX