BigQuery Clustered Table 简介 - 聚簇表

2023-12-24 22:36

本文主要是介绍BigQuery Clustered Table 简介 - 聚簇表,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Clustered Table的定义

聚簇可以提高某些类型的查询(例如,使用过滤条件子句的查询和聚合数据的查询)的性能。当通过查询作业或加载作业将数据写入聚簇表时,BigQuery 会使用聚簇列中的值对这些数据进行排序。这些值用于将数据整理到 BigQuery 存储的多个块中。当您提交的查询包含基于聚簇列过滤数据的子句时,BigQuery 会使用已排序的块来避免扫描不必要的数据。 如果表或分区低于 1 GB,则聚簇表和未聚簇表之间的查询性能可能不会有显著差异。

以下是创建聚簇表的示例:

CREATE TABLE dataset.clustered_table
(column1 INT64,column2 STRING,column3 DATE,...
)
CLUSTER BY column1;

在上面的示例中,通过将column1列指定为聚簇键,dataset.clustered_table将按照column1的值进行排序和分组。

值得注意的是,创建聚簇表是一个一次性的操作,无法在表创建后更改聚簇键。如果需要更改聚簇键,您需要创建一个新的聚簇表并将数据导入其中。

通俗点来讲, Clustered表就是存放数据会已 设定的 字段来排序, 这样以这个字段来做filter 就不用全表扫描了(2分法?), 跟索引的原理类似。

还有 Clustered 表和分区表 并不冲突, 1张表既可以分区 也可以 Cluster , 不过如何分区和Cluster 都是同1个列的我觉得意义不大, 如果是两个不同的field的话代表查询时要同时加上这个两个field的filter, 总之看实际需求!




准备测试数据

本码农发文从不只发定义, 既然上面说了大数量量才有效果, 我们就准备个大的

[gateman@manjaro-x13 chapter-02]$ ls -lh 
total 401M
-rw-r--r-- 1 gateman gateman 401M Dec 24 01:24 automobiles.csv
-rwxrwxrwx 1 gateman gateman 2.7K May  5  2022 chapter-02-steps.sql
-rwxrwxrwx 1 gateman gateman  250 May  5  2022 link_to_automobiles_dataset.txt
[gateman@manjaro-x13 chapter-02]$ head automobiles.csv 
maker,model,mileage,manufacture_year,engine_displacement,engine_power,body_type,color_slug,stk_year,transmission,door_count,seat_count,fuel_type,date_created,date_last_seen,price_eur
ford,galaxy,151000,2011,2000,103,,,None,man,5,7,diesel,2015-11-14 18:10:06.838319+00,2016-01-27 20:40:15.46361+00,10584.75
skoda,octavia,143476,2012,2000,81,,,None,man,5,5,diesel,2015-11-14 18:10:06.853411+00,2016-01-27 20:40:15.46361+00,8882.31
bmw,,97676,2010,1995,85,,,None,man,5,5,diesel,2015-11-14 18:10:06.861792+00,2016-01-27 20:40:15.46361+00,12065.06
skoda,fabia,111970,2004,1200,47,,,None,man,5,5,gasoline,2015-11-14 18:10:06.872313+00,2016-01-27 20:40:15.46361+00,2960.77
skoda,fabia,128886,2004,1200,47,,,None,man,5,5,gasoline,2015-11-14 18:10:06.880335+00,2016-01-27 20:40:15.46361+00,2738.71
skoda,fabia,140932,2003,1200,40,,,None,man,5,5,gasoline,2015-11-14 18:10:06.894643+00,2016-01-27 20:40:15.46361+00,1628.42
skoda,fabia,167220,2001,1400,74,,,None,man,5,5,gasoline,2015-11-14 18:10:06.915376+00,2016-01-27 20:40:15.46361+00,2072.54
bmw,,148500,2009,2000,130,,,None,auto,5,5,diesel,2015-11-14 18:10:06.924123+00,2016-01-27 20:40:15.46361+00,10547.74
skoda,octavia,105389,2003,1900,81,,,None,man,5,5,diesel,2015-11-14 18:10:06.936239+00,2016-01-27 20:40:15.46361+00,4293.12
[gateman@manjaro-x13 chapter-02]$ wc -l automobiles.csv 
3552913 automobiles.csv
[gateman@manjaro-x13 chapter-02]$

如上图, 有个automobile.csv 的测试数据集
400 多MB
355万行

用libreOffice 打开这个csv 直接提示数据函数超过了1,048,576的上限(2的20次方), 只能打开1,048,576 部分的数据。
别笑, 微软的Excel也是这个上限, 而且LibreOffice 还能对这个1,048,576 正常作处理。

在这里插入图片描述

LibreOffice 的确神器, 相比之下另1个office 软件 OnlyOffice 直接报错内存不够。




上传数据表到BigQuery

[gateman@manjaro-x13 chapter-02]$ bq load --autodetect --source_format=CSV DS2.automobile_o automobiles.csv Updates are available for some Google Cloud CLI components.  To install them,
please run:$ gcloud components update/home/gateman/devtools/google-cloud-sdk/platform/bq/bq.py:17: DeprecationWarning: 'pipes' is deprecated and slated for removal in Python 3.13import pipes
Upload complete.
Waiting on bqjob_r3a61ac818de36f56_0000018c97cbb415_1 ... (12s) Current status: DONE   
[gateman@manjaro-x13 chapter-02]$ bq show DS2.automobile_o
/home/gateman/devtools/google-cloud-sdk/platform/bq/bq.py:17: DeprecationWarning: 'pipes' is deprecated and slated for removal in Python 3.13import pipes
Table jason-hsbc:DS2.automobile_oLast modified                Schema                Total Rows   Total Bytes   Expiration   Time Partitioning   Clustered Fields   Total Logical Bytes   Total Physical Bytes   Labels  ----------------- --------------------------------- ------------ ------------- ------------ ------------------- ------------------ --------------------- ---------------------- -------- 24 Dec 01:51:31   |- maker: string                  3552912      305423773                                                         305423773             71803365                       |- model: string                                                                                                                                                      |- mileage: integer                                                                                                                                                   |- manufacture_year: integer                                                                                                                                          |- engine_displacement: integer                                                                                                                                       |- engine_power: integer                                                                                                                                              |- body_type: string                                                                                                                                                  |- color_slug: string                                                                                                                                                 |- stk_year: string                                                                                                                                                   |- transmission: string                                                                                                                                               |- door_count: string                                                                                                                                                 |- seat_count: string                                                                                                                                                 |- fuel_type: string                                                                                                                                                  |- date_created: timestamp                                                                                                                                            |- date_last_seen: timestamp                                                                                                                                          |- price_eur: float                                                                                                                                                   

这样我们就得到1个355万行的非分区和非clustered的表. 表名: automobile_o




创建1个具有相同数据的Cluster表

需求是, 我想这个Clustered表的是按maker, manufacture_year (厂家和生产年份) 来排序存储的.

方法1: 使用bq command 上传多次, 但是加上–clustering_fileds 的参数

[gateman@manjaro-x13 chapter-02]$ bq load --autodetect --source_format=CSV --clustering_fields=maker,manufacture_year DS2.automobile_c automobiles.csv
/home/gateman/devtools/google-cloud-sdk/platform/bq/bq.py:17: DeprecationWarning: 'pipes' is deprecated and slated for removal in Python 3.13import pipes
Upload complete.
Waiting on bqjob_r1b1de085f8ee2e04_0000018c982d988a_1 ... (10s) Current status: DONE   
[gateman@manjaro-x13 chapter-02]$ bq show DS2.automobile_c
/home/gateman/devtools/google-cloud-sdk/platform/bq/bq.py:17: DeprecationWarning: 'pipes' is deprecated and slated for removal in Python 3.13import pipes
Table jason-hsbc:DS2.automobile_cLast modified                Schema                Total Rows   Total Bytes   Expiration   Time Partitioning      Clustered Fields       Total Logical Bytes   Total Physical Bytes   Labels  ----------------- --------------------------------- ------------ ------------- ------------ ------------------- ------------------------- --------------------- ---------------------- -------- 24 Dec 03:38:30   |- maker: string                  3552912      305423773                                      maker, manufacture_year   305423773                                            |- model: string                                                                                                                                                             |- mileage: integer                                                                                                                                                          |- manufacture_year: integer                                                                                                                                                 |- engine_displacement: integer                                                                                                                                              |- engine_power: integer                                                                                                                                                     |- body_type: string                                                                                                                                                         |- color_slug: string                                                                                                                                                        |- stk_year: string                                                                                                                                                          |- transmission: string                                                                                                                                                      |- door_count: string                                                                                                                                                        |- seat_count: string                                                                                                                                                        |- fuel_type: string                                                                                                                                                         |- date_created: timestamp                                                                                                                                                   |- date_last_seen: timestamp                                                                                                                                                 |- price_eur: float   

方法2: 用sql 基于已存在的表创建

create table DS2.automobile_c2
cluster by maker, manufacture_year
as 
select * from DS2.automobile_o

效果是一样的

[gateman@manjaro-x13 chapter-02]$ bq show DS2.automobile_c2
/home/gateman/devtools/google-cloud-sdk/platform/bq/bq.py:17: DeprecationWarning: 'pipes' is deprecated and slated for removal in Python 3.13import pipes
Table jason-hsbc:DS2.automobile_c2Last modified                Schema                Total Rows   Total Bytes   Expiration   Time Partitioning      Clustered Fields       Total Logical Bytes   Total Physical Bytes   Labels  ----------------- --------------------------------- ------------ ------------- ------------ ------------------- ------------------------- --------------------- ---------------------- -------- 24 Dec 03:38:04   |- maker: string                  3552912      305423773                                      maker, manufacture_year   305423773                                            |- model: string                                                                                                                                                             |- mileage: integer                                                                                                                                                          |- manufacture_year: integer                                                                                                                                                 |- engine_displacement: integer                                                                                                                                              |- engine_power: integer                                                                                                                                                     |- body_type: string                                                                                                                                                         |- color_slug: string                                                                                                                                                        |- stk_year: string                                                                                                                                                          |- transmission: string                                                                                                                                                      |- door_count: string                                                                                                                                                        |- seat_count: string                                                                                                                                                        |- fuel_type: string                                                                                                                                                         |- date_created: timestamp                                                                                                                                                   |- date_last_seen: timestamp                                                                                                                                                 |- price_eur: float                                                                                                                                                          




比较查询性能

我们已经同时具有 一般表 automobile_o 和 clustered 表automobile_c 接下来就是比较查询性能了
当然国际管理, 查询之前都必须禁用cache !
在这里插入图片描述







首先先检查表大小

[gateman@manjaro-x13 chapter-02]$ bq show DS2.automobile_o
/home/gateman/devtools/google-cloud-sdk/platform/bq/bq.py:17: DeprecationWarning: 'pipes' is deprecated and slated for removal in Python 3.13import pipes
Table jason-hsbc:DS2.automobile_oLast modified                Schema                Total Rows   Total Bytes   Expiration   Time Partitioning   Clustered Fields   Total Logical Bytes   Total Physical Bytes   Labels  ----------------- --------------------------------- ------------ ------------- ------------ ------------------- ------------------ --------------------- ---------------------- -------- 24 Dec 01:51:31   |- maker: string                  3552912      305423773                                                         305423773             71803365                       |- model: string                                                                                                                                                      |- mileage: integer                                                                                                                                                   |- manufacture_year: integer                                                                                                                                          |- engine_displacement: integer                                                                                                                                       |- engine_power: integer                                                                                                                                              |- body_type: string                                                                                                                                                  |- color_slug: string                                                                                                                                                 |- stk_year: string                                                                                                                                                   |- transmission: string                                                                                                                                               |- door_count: string                                                                                                                                                 |- seat_count: string                                                                                                                                                 |- fuel_type: string                                                                                                                                                  |- date_created: timestamp                                                                                                                                            |- date_last_seen: timestamp                                                                                                                                          |- price_eur: float                                                                                                                                                   [gateman@manjaro-x13 chapter-02]$ bq show DS2.automobile_c
/home/gateman/devtools/google-cloud-sdk/platform/bq/bq.py:17: DeprecationWarning: 'pipes' is deprecated and slated for removal in Python 3.13import pipes
Table jason-hsbc:DS2.automobile_cLast modified                Schema                Total Rows   Total Bytes   Expiration   Time Partitioning      Clustered Fields       Total Logical Bytes   Total Physical Bytes   Labels  ----------------- --------------------------------- ------------ ------------- ------------ ------------------- ------------------------- --------------------- ---------------------- -------- 24 Dec 03:38:30   |- maker: string                  3552912      305423773                                      maker, manufacture_year   305423773             66970872                       |- model: string                                                                                                                                                             |- mileage: integer                                                                                                                                                          |- manufacture_year: integer                                                                                                                                                 |- engine_displacement: integer                                                                                                                                              |- engine_power: integer                                                                                                                                                     |- body_type: string                                                                                                                                                         |- color_slug: string                                                                                                                                                        |- stk_year: string                                                                                                                                                          |- transmission: string                                                                                                                                                      |- door_count: string                                                                                                                                                        |- seat_count: string                                                                                                                                                        |- fuel_type: string                                                                                                                                                         |- date_created: timestamp                                                                                                                                                   |- date_last_seen: timestamp                                                                                                                                                 |- price_eur: float

可以看出两个表具有相同的schema, 相同的数据行和数据大小, 305423773 bytes = 291.27 MB




先看正常表 automobile_o
无论是select * from DS2.automobile_o 还是 select * from DS2.automobile_o where maker=‘bmw’

的处理数据都是291.27MB (全表处理) , 正常操作, 相当不省钱
在这里插入图片描述

好了, 下面对比查询clustered 表 DS2.automobile_c

在这里插入图片描述

当我们输入sql时, web_ui 预估的will process 数据还是291.27MB? 这是因为跟分区表不一样, bigquery在查询之前并不能准确预估clustered 表的预估数据。

因为分区表可以明白地根据查询条件得到sql 将在那几个表分区里查询, 而分区表存在多少个分区, 每个分区的数据行这些元数据BigQuery是清楚的, 其他无关表分区可以忽略

但是, clustered表的原理是数据按照某个or若干列的排序存放。

例如上面的sql条件是maker=‘bmw’, 在clustered表中的 所有bmw 的数据都应该存放一起, 但是BigQuery并没有这些数据的元数据, 例如bmw的数据有多少行, 大概占全表的什么位置, 这些数据都要执行知道才知道, 所以这里预估的will process就是全表了

但是,当我们执行这条数据后, 在真正的query信息得到实际处理的data 量是99 MB, Clustered 生效!

在这里插入图片描述

宝马毕竟是大厂, 如果查询条件换成冷门豪车阿士顿马田, 数据处理量会更加少
在这里插入图片描述

下一条sql
select * from DS2.automobile_c where manufacture_year<1994
在这里插入图片描述

结果还是全表扫描

因为虽然manufacture_year 也是clustered列的一部分, 但是存储排序是先拍maker 再排 manufacture_year, 如果只查询manufacture_year, 还是会导致全表扫描, 跟索引的最左原则一样。




结下来
两个列一齐查
在这里插入图片描述
跟只差maker 的处理数据量居然一样, 可能我的测试数据样本还不足够大的原因。




分区表和Clustered 一齐上

既然要查两个field
那么我就建1个 用manufacture_year 来分区, maker来cluster的表, 作为对比

首先查出 这个表的manufacture_year 的上下限
在这里插入图片描述

0 到2017

然后我们用下面的sql来建一张分区和clustered表 DS2.automobile_pc

create table DS2.automobile_pc
partition by Range_bucket(manufacture_year, generate_array(0,2017,10))
cluster by maker
as 
select * from DS2.automobile_o

在这里插入图片描述

这时 当我们输入sql时, bq 已经能预估分区后的will process数据是51.52MB了, 但是在这些分区, 相同maker的数据还是黏在一齐的, 也就是将bq不必要扫描每个完整表分区
在这里插入图片描述
我们执行它
在这里插入图片描述
可以看出最终只process了 37MB数据比预估的要少, 看来如果固定用两个field来查询的话, 用分区 + clustered 表的确很暴力有效!!

这篇关于BigQuery Clustered Table 简介 - 聚簇表的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/533371

相关文章

ASIO网络调试助手之一:简介

多年前,写过几篇《Boost.Asio C++网络编程》的学习文章,一直没机会实践。最近项目中用到了Asio,于是抽空写了个网络调试助手。 开发环境: Win10 Qt5.12.6 + Asio(standalone) + spdlog 支持协议: UDP + TCP Client + TCP Server 独立的Asio(http://www.think-async.com)只包含了头文件,不依

业务协同平台--简介

一、使用场景         1.多个系统统一在业务协同平台定义协同策略,由业务协同平台代替人工完成一系列的单据录入         2.同时业务协同平台将执行任务推送给pda、pad等执行终端,通知各人员、设备进行作业执行         3.作业过程中,可设置完成时间预警、作业节点通知,时刻了解作业进程         4.做完再给你做过程分析,给出优化建议         就问你这一套下

容器编排平台Kubernetes简介

目录 什么是K8s 为什么需要K8s 什么是容器(Contianer) K8s能做什么? K8s的架构原理  控制平面(Control plane)         kube-apiserver         etcd         kube-scheduler         kube-controller-manager         cloud-controlle

【Tools】AutoML简介

摇来摇去摇碎点点的金黄 伸手牵来一片梦的霞光 南方的小巷推开多情的门窗 年轻和我们歌唱 摇来摇去摇着温柔的阳光 轻轻托起一件梦的衣裳 古老的都市每天都改变模样                      🎵 方芳《摇太阳》 AutoML(自动机器学习)是一种使用机器学习技术来自动化机器学习任务的方法。在大模型中的AutoML是指在大型数据集上使用自动化机器学习技术进行模型训练和优化。

SaaS、PaaS、IaaS简介

云计算、云服务、云平台……现在“云”已成了一个家喻户晓的概念,但PaaS, IaaS 和SaaS的区别估计还没有那么多的人分得清,下面就分别向大家普及一下它们的基本概念: SaaS 软件即服务 SaaS是Software-as-a-Service的简称,意思是软件即服务。随着互联网技术的发展和应用软件的成熟, 在21世纪开始兴起的一种完全创新的软件应用模式。 它是一种通过Internet提供

vue2实践:el-table实现由用户自己控制行数的动态表格

需求 项目中需要提供一个动态表单,如图: 当我点击添加时,便添加一行;点击右边的删除时,便删除这一行。 至少要有一行数据,但是没有上限。 思路 这种每一行的数据固定,但是不定行数的,很容易想到使用el-table来实现,它可以循环读取:data所绑定的数组,来生成行数据,不同的是: 1、table里面的每一个cell,需要放置一个input来支持用户编辑。 2、最后一列放置两个b

LIBSVM简介

LIBSVM简介 支持向量机所涉及到的数学知识对一般的化学研究者来说是比较难的,自己编程实现该算法难度就更大了。但是现在的网络资源非常发达,而且国际上的科学研究者把他们的研究成果已经放在网络上,免费提供给用于研究目的,这样方便大多数的研究者,不必要花费大量的时间理解SVM算法的深奥数学原理和计算机程序设计。目前有关SVM计算的相关软件有很多,如LIBSVM、mySVM、SVMLight等,这些

urllib与requests爬虫简介

urllib与requests爬虫简介 – 潘登同学的爬虫笔记 文章目录 urllib与requests爬虫简介 -- 潘登同学的爬虫笔记第一个爬虫程序 urllib的基本使用Request对象的使用urllib发送get请求实战-喜马拉雅网站 urllib发送post请求 动态页面获取数据请求 SSL证书验证伪装自己的爬虫-请求头 urllib的底层原理伪装自己的爬虫-设置代理爬虫coo

新一代车载(E/E)架构下的中央计算载体---HPC软件架构简介

老规矩,分享一段喜欢的文字,避免自己成为高知识低文化的工程师: 屏蔽力是信息过载时代一个人的特殊竞争力,任何消耗你的人和事,多看一眼都是你的不对。非必要不费力证明自己,无利益不试图说服别人,是精神上的节能减排。 无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事.而不是让内心的烦躁、焦虑、毁掉你本就不多的热情和定力。 时间不知不觉中,快要来到夏末秋初。一年又过去了一大半,成

AI学习指南深度学习篇-带动量的随机梯度下降法简介

AI学习指南深度学习篇 - 带动量的随机梯度下降法简介 引言 在深度学习的广阔领域中,优化算法扮演着至关重要的角色。它们不仅决定了模型训练的效率,还直接影响到模型的最终表现之一。随着神经网络模型的不断深化和复杂化,传统的优化算法在许多领域逐渐暴露出其不足之处。带动量的随机梯度下降法(Momentum SGD)应运而生,并被广泛应用于各类深度学习模型中。 在本篇文章中,我们将深入探讨带动量的随