impala高级设置set之APPX_COUNT_DISTINCT

2023-11-06

官网地址

https://impala.apache.org/docs/build/html/topics/impala_appx_count_distinct.html

When the APPX_COUNT_DISTINCT query option is set to TRUE, Impala implicitly converts COUNT(DISTINCT) operations to the NDV() function calls. The resulting count is approximate rather than precise. Enable the query option when a tolerable amount of error is acceptable in order to obtain faster query results than with a COUNT (DISTINCT) queries.

Type: Boolean; recognized values are 1 and 0, or true and false; any other value interpreted as false

Default: false (shown as 0 in output of SET statement)

简单的来说就是设置APPX_COUNT_DISTINCT=1后 也就是true 会在select count(distinct column)的时候查询的更快但是查询结果不一定准确

实战

select count(distinct company_id) from ia_fdw_b_profile_product_info--4.97s 27664540

set APPX_COUNT_DISTINCT=1

select count(distinct company_id) from ia_fdw_b_profile_product_info--1.38s 29165564

这里可以看到设置之后查询速度快了几倍,但是误差也不小,所以慎用。。。

NDV()

这里提到了一个ndv函数,但是我在show functions的时候没看到,后面凑巧在官网看到了。

也学习一番

https://impala.apache.org/docs/build/html/topics/impala_ndv.html#ndv

简介

An aggregate function that returns an approximate value similar to the result of COUNT(DISTINCT col), the "number of distinct values". It is much faster than the combination of COUNT and DISTINCT, and uses a constant amount of memory and thus is less memory-intensive for columns with high cardinality.

一种聚合函数,返回的值与COUNT(DISTINCT col)差不多,对值去重。它比count(distinct )快的多,并使用固定的内存量,因此对于具有高基数的列,内存占用较少。

语法:

NDV([DISTINCT | ALL] expression [,scale])

说实话这个表达式我还没看懂。。 举例使用就是 select ndv(colum) from table =select count(distinct column) from table。这个scale是一个精度,但是根本写了就报错。

Note: The optional argument scale must be an integer and can be in the range from 1 to 10 and maps to a precision used by the HyperLogLog (HLL) algorithm with the following mapping formula:

precision = scale + 8

Therefore a scale of 1 is mapped to a precision of 9 and a scale of 10 is mapped to a precision of 18.

Without the optional argument, the precision which determines the total number of different estimators in the HLL algorithm will be still 10.

A large precision value generally produces a better estimation with less error than a small precision value. This is due to the extra number of estimators involved. The expense is at the need of extra memory. For a given precision p, the amount of memory used by the HLL algorithm is in the order of 2^p bytes.

When provided a scale of 10 against a total of 22 distinct data sets loaded into external Impala tables, the error will be computed as abs(<true_unique_value> - <estimated_unique_value>) / <true_unique_value>

The scale of 10, mapped to the precision of 18, yielded the worst estimation error at 0.42% (for one set of 10 million integers), and average error no more than 0.17%. This was at the cost of 256Kb of memory for the internal data structure per evaluation of the HLL algorithm.

官方案例

Examples:

The following example queries a billion-row table to illustrate the relative performance of COUNT(DISTINCT) and NDV(). It shows how COUNT(DISTINCT) gives a precise answer, but is inefficient for large-scale data where an approximate result is sufficient. The NDV() function gives an approximate result but is much faster.

select count(distinct col1) from sample_data;
+---------------------+
| count(distinct col1)|
+---------------------+
| 100000              |
+---------------------+
Fetched 1 row(s) in 20.13s

select cast(ndv(col1) as bigint) as col1 from sample_data;
+----------+
| col1     |
+----------+
| 139017   |
+----------+
Fetched 1 row(s) in 8.91s

The following example shows how you can code multiple NDV() calls in a single query, to easily learn which columns have substantially more or fewer distinct values. This technique is faster than running a sequence of queries with COUNT(DISTINCT) calls.

select cast(ndv(col1) as bigint) as col1, cast(ndv(col2) as bigint) as col2,
    cast(ndv(col3) as bigint) as col3, cast(ndv(col4) as bigint) as col4
  from sample_data;
+----------+-----------+------------+-----------+
| col1     | col2      | col3       | col4      |
+----------+-----------+------------+-----------+
| 139017   | 282       | 46         | 145636240 |
+----------+-----------+------------+-----------+
Fetched 1 row(s) in 34.97s

select count(distinct col1) from sample_data;
+---------------------+
| count(distinct col1)|
+---------------------+
| 100000              |
+---------------------+
Fetched 1 row(s) in 20.13s

select count(distinct col2) from sample_data;
+----------------------+
| count(distinct col2) |
+----------------------+
| 278                  |
+----------------------+
Fetched 1 row(s) in 20.09s

select count(distinct col3) from sample_data;
+-----------------------+
| count(distinct col3)  |
+-----------------------+
| 46                    |
+-----------------------+
Fetched 1 row(s) in 19.12s

select count(distinct col4) from sample_data;
+----------------------+
| count(distinct col4) |
+----------------------+
| 147135880            |
+----------------------+
Fetched 1 row(s) in 266.95s

其实我到这里也没看太懂这个函数有什么用?唯一的用处就是算去重值的时候会快一点。。。 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

impala高级设置set之APPX_COUNT_DISTINCT 的相关文章

随机推荐

  • WebTransport 开播的应用实践之路

    动手点关注 干货不迷路 Web开播的业务挑战 无论是本地软件推流还是Web推流 都需要解决推流抖动 画面高糊 音频卡顿等问题 在现有的Web技术环境下 如何稳定地把高质量的音视频流呈现给更多用户 是我们技术团队攻克的重点 从技术角度来解读一
  • Hashtable和HashMap、ConcurrentHashMap 之间的区别

    Hashtable和HashMap的区别 HashMap和Hashtable都是哈希表数据结构 但是Hashtable是线程安全的 HashMap是线程不安全的 Hashtable实现线程安全就是简单的把关键方法都加上了synchroniz
  • 企业项目实战----CDN加速的实现

    前言 CDN加速对企业非常重要 体现在哪呢 举个例子 A企业的后端服务器在杭州 用户遍布全国 让全国的用户都去访问企业A在杭州的后端服务器你觉得可行吗 肯定不可行呀 第一 后端服务器承受不了全国这么巨大的访问量 第二 访问速度慢 要经过的陆
  • 提升职场价值,把握成长方向

    来自 IT人的职场进阶 同样的职场起点 为什么几年后大家差距很大 如果想快速升职加薪 有什么好方法吗 如何才能做到持续且快速的成长 这些疑惑都离不开一个本质问题 职场价值 因为企业用人的核心出发点是 你能否为企业创造价值 你的价值和薪酬职级
  • MSP430 EEPROM-24C512使用总结及代码说明

    MSP430 EEPROM 24C512使用总结及代码说明 https wenku baidu com view 61f407d6f705cc175527094b html
  • hooks中useMemo和useCallback详解

    要想学习useMemo必须要先知道React memo 这两者都有一定的优化作用 memo的作用 当数据变化时 代码会重新执行一遍 但是子组件数据没有变化也会执行 这个时候可以使用memo将子组件封装起来 让子组件的数据只在发生改变时才会执
  • sudo rosdep init ERROR: cannot download default sources list from:

    在sudo rosdep init时出现的错误ERROR cannot download default sources list from https raw githubusercontent com ros rosdistro mas
  • 安装一个虚拟服务器,一个云服务器可以装虚拟机么

    一个云服务器可以装虚拟机么 内容精选 换一换 虚拟IP地址用于为网卡提供第二个IP地址 同时支持与多个云服务器的网卡绑定 从而实现多个云服务器之间的高可用性 登录管理控制台 单击管理控制台左上角的 选择区域和项目 选择 计算 gt 云耀云服
  • WEB前端命名规范

    https www cnblogs com ysx215 p 7461777 html
  • 数组指针 行指针 列指针

    概念 我们把指向数组的指针叫做数组指针 后面还会学到指针数组 这两个是不一样的 根据中学语文偏正词组的知识可以知道 前者是指针 后者是数组 一般指针变量 int a 2 3 1 2 3 4 5 6 int P a 0 0 int p a 0
  • 短视频账号矩阵系统如何技术嵌入Chatgpt?

    将GPT Generative Pre trained Transformer 嵌入短视频账号矩阵系统需要以下步骤 1 获取GPT模型 可以自行训练或使用开源的预训练模型 如GPT 2 GPT 3等 2 导入GPT模型 将GPT模型导入到短
  • Metronic学习-1-替换google字体,让页面打开更流畅

    Metronic是一款强大的后台模板 包括很多组件 接触过很多后台模板 有Layui AdminLTE Inspinia hui 感觉Layui适合快速开发 Layui封闭性很强 对于前端不太熟悉的话 只能按模仿 如果需要深入学习 需要花费
  • Html-根据不同的分辨率设置不同的背景图片

    media only screen and min width 1024px 当分辨率width gt 1024px 时使用1 jpg作为背景图片 bg background url images 1 jpg no repeat media
  • Reactor模型与Proactor模型

    1 Reactor模型 1 1 什么是Reactor模式 它是基于IO多路复用与线程池 Reactor模式的核心组成部分包括Reactor和处理资源池 进程池或线程池 Reactor负责监听和分配事件 处理资源池负责处理事件 Reactor
  • 详解用 matplotlib 绘制动态条形图

    详解用 matplotlib 绘制动态条形图 端午安康 近日看到联合国网站提供的世界人口数据集 其中一个子数据集包含了各国 1950 2015年的人口数据 假日值班 有自由的时间 就基于这个数据集 用 matplotlib 实现了一个世界人
  • 无法打开文件“xxx.lib”错误的解决办法

    原因 pragma comment lib xxx lib 默认和引用的CPP文件在一个文件夹中 解决方法1 将xxx lib和调用pragma comment的源文件放在一个目录 注意是调用它的源文件 不是头文件 解决方法2 也可以在xx
  • UE4UE5 打包安卓报错总结UnrealBuildTool failed解决

    报错 Android armv7 gradle rungradle bat UnrealBuildTool failed 解决方法 1 替换gradle包 下载地址 http services gradle org distribution
  • 0.目标检测基础知识

    1 IOU交并比 1 交并比 import cv2 import numpy as np img np zeros 512 512 3 np uint8 此大小的黑色画布 img fill 255 画布填255 变成白色画布 RecA 50
  • H264/AVC-帧内预测相邻像素推导过程

    帧内预测过程会以相邻块的像素值做参考 来预测当前块的像素值 以Intra 4x4为例 如下图所示 需要用到的13个相邻像素值 那么如何获取这13个像素值 本文要主要说明如何获取帧内预测所用到的相邻像素 对应参考文档6 4 5 6 4 9小节
  • impala高级设置set之APPX_COUNT_DISTINCT

    官网地址 https impala apache org docs build html topics impala appx count distinct html When the APPX COUNT DISTINCT query o