> ## Documentation Index
> Fetch the complete documentation index at: /documents/llms.txt
> Use this file to discover all available pages before exploring further.

# percentile_approx

> percentile_approx 计算百分位数的近似值，支持单个或多个百分位，使用固定大小内存。

计算一个或多个百分位数的近似值。

> 该函数使用固定大小的内存，因此对于高基数的列可以使用更少的内存，可用于计算 tp99 等统计值。

## 语法

```Haskell theme={null}
DOUBLE PERCENTILE_APPROX(expr, DOUBLE|ARRAY<DOUBLE> p[, DOUBLE compression])
```

## 参数说明

`expr`: 被选取的表达式。

`p`: 支持的数据类型为 DOUBLE，p 的值介于 0 到 1 之间; 数组类型`ARRAY<DOUBLE>`, 数组每个值介于0 到 1之间。

`compression`: 可选参数，可设置范围是 \[2048, 10000]，值越大精度越高，内存消耗越大，计算耗时越长。该参数未指定或设置的值在 \[2048, 10000] 范围外，以 10000 的默认值运行。支持的数据类型为 DOUBLE 类型。

## 返回值说明

返回值为数值类型。

## 示例

```plain text theme={null}
MySQL > select `table`, percentile_approx(cost_time,0.99)
from log_statis
group by `table`;
+----------+--------------------------------------+
| table    | percentile_approx(`cost_time`, 0.99) |
+----------+--------------------------------------+
| test     |                                54.22 |
+----------+--------------------------------------+

MySQL > select `table`, percentile_approx(cost_time,0.99, 4096)
from log_statis
group by `table`;
+----------+----------------------------------------------+
| table    | percentile_approx(`cost_time`, 0.99, 4096.0) |
+----------+----------------------------------------------+
| test     |                                        54.21 |
+----------+----------------------------------------------+

MySQL > select percentile_approx(c2, [0.1, 0.5, 0.9], 10000) from t1;
+-----------------------------------------------+
| percentile_approx(c2, [0.1, 0.5, 0.9], 10000) |
+-----------------------------------------------+
| [4999.6005859375,25000,45000.3984375]         |
+-----------------------------------------------+
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.