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

相关文章

MySQL的ALTER TABLE命令的使用解读

《MySQL的ALTERTABLE命令的使用解读》:本文主要介绍MySQL的ALTERTABLE命令的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、查看所建表的编China编程码格式2、修改表的编码格式3、修改列队数据类型4、添加列5、修改列的位置5.1、把列

rust 中的 EBNF简介举例

《rust中的EBNF简介举例》:本文主要介绍rust中的EBNF简介举例,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 什么是 EBNF?2. 核心概念3. EBNF 语法符号详解4. 如何阅读 EBNF 规则5. 示例示例 1:简单的电子邮件地址

Python 异步编程 asyncio简介及基本用法

《Python异步编程asyncio简介及基本用法》asyncio是Python的一个库,用于编写并发代码,使用协程、任务和Futures来处理I/O密集型和高延迟操作,本文给大家介绍Python... 目录1、asyncio是什么IO密集型任务特征2、怎么用1、基本用法2、关键字 async1、async

Pandas透视表(Pivot Table)的具体使用

《Pandas透视表(PivotTable)的具体使用》透视表用于在数据分析和处理过程中进行数据重塑和汇总,本文就来介绍一下Pandas透视表(PivotTable)的具体使用,感兴趣的可以了解一下... 目录前言什么是透视表?使用步骤1. 引入必要的库2. 读取数据3. 创建透视表4. 查看透视表总结前言

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

GORM中Model和Table的区别及使用

《GORM中Model和Table的区别及使用》Model和Table是两种与数据库表交互的核心方法,但它们的用途和行为存在著差异,本文主要介绍了GORM中Model和Table的区别及使用,具有一... 目录1. Model 的作用与特点1.1 核心用途1.2 行为特点1.3 示例China编程代码2. Tab

Golang的CSP模型简介(最新推荐)

《Golang的CSP模型简介(最新推荐)》Golang采用了CSP(CommunicatingSequentialProcesses,通信顺序进程)并发模型,通过goroutine和channe... 目录前言一、介绍1. 什么是 CSP 模型2. Goroutine3. Channel4. Channe

Java中的Opencv简介与开发环境部署方法

《Java中的Opencv简介与开发环境部署方法》OpenCV是一个开源的计算机视觉和图像处理库,提供了丰富的图像处理算法和工具,它支持多种图像处理和计算机视觉算法,可以用于物体识别与跟踪、图像分割与... 目录1.Opencv简介Opencv的应用2.Java使用OpenCV进行图像操作opencv安装j

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.做完再给你做过程分析,给出优化建议         就问你这一套下