ClickHouse-物化视图

这篇具有很好参考价值的文章主要介绍了ClickHouse-物化视图。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

官方文档

什么是物化视图

ClickHouse 中物化视图(Materialized View)是一种预先计算并缓存结果的视图,它存储在磁盘上并自动更新,典型的空间换时间思路。物化视图是一种优化技术,它可以加速查询操作,降低系统负载,并提高查询性能。

创建语法:

CREATE [MATERIALIZED] VIEW [IF NOT EXISTS] [db.]table_name [TO[db.]name] [ENGINE = engine] [POPULATE] AS SELECT ...
物化视图工作流程

当你创建一个物化视图时,ClickHouse 会计算该视图的结果,并将结果存储在磁盘上。然后,当你查询该视图时,ClickHouse 会直接从磁盘上的结果中获取数据,而不需要重新计算。

物化视图可以基于一个或多个表创建,并可以使用 SQL 查询语句定义。它可以使用各种查询操作进行更新,例如 Insert、Update、Delete 。当数据源表发生更改时,物化视图会自动更新,以保持结果的一致性。

注意:使用物化视图,可以在查询性能和数据一致性之间进行权衡。物化视图可以提高查询性能,但会增加数据更新和维护的开销。

使用示例

这边是以官方提供的数据来操作。example-datasets

  • 创建数据库以及表,这里给出 sql,也可以去上面给的地址拿:

    DROP DATABASE IF EXISTS git;
    CREATE DATABASE git;
    
    CREATE TABLE git.commits
    (
        hash String,
        author LowCardinality(String),
        time DateTime,
        message String,
        files_added UInt32,
        files_deleted UInt32,
        files_renamed UInt32,
        files_modified UInt32,
        lines_added UInt32,
        lines_deleted UInt32,
        hunks_added UInt32,
        hunks_removed UInt32,
        hunks_changed UInt32
    ) ENGINE = MergeTree ORDER BY time;
    
    CREATE TABLE git.file_changes
    (
        change_type Enum('Add' = 1, 'Delete' = 2, 'Modify' = 3, 'Rename' = 4, 'Copy' = 5, 'Type' = 6),
        path LowCardinality(String),
        old_path LowCardinality(String),
        file_extension LowCardinality(String),
        lines_added UInt32,
        lines_deleted UInt32,
        hunks_added UInt32,
        hunks_removed UInt32,
        hunks_changed UInt32,
    
        commit_hash String,
        author LowCardinality(String),
        time DateTime,
        commit_message String,
        commit_files_added UInt32,
        commit_files_deleted UInt32,
        commit_files_renamed UInt32,
        commit_files_modified UInt32,
        commit_lines_added UInt32,
        commit_lines_deleted UInt32,
        commit_hunks_added UInt32,
        commit_hunks_removed UInt32,
        commit_hunks_changed UInt32
    ) ENGINE = MergeTree ORDER BY time;
    
    CREATE TABLE git.line_changes
    (
        sign Int8,
        line_number_old UInt32,
        line_number_new UInt32,
        hunk_num UInt32,
        hunk_start_line_number_old UInt32,
        hunk_start_line_number_new UInt32,
        hunk_lines_added UInt32,
        hunk_lines_deleted UInt32,
        hunk_context LowCardinality(String),
        line LowCardinality(String),
        indent UInt8,
        line_type Enum('Empty' = 0, 'Comment' = 1, 'Punct' = 2, 'Code' = 3),
    
        prev_commit_hash String,
        prev_author LowCardinality(String),
        prev_time DateTime,
    
        file_change_type Enum('Add' = 1, 'Delete' = 2, 'Modify' = 3, 'Rename' = 4, 'Copy' = 5, 'Type' = 6),
        path LowCardinality(String),
        old_path LowCardinality(String),
        file_extension LowCardinality(String),
        file_lines_added UInt32,
        file_lines_deleted UInt32,
        file_hunks_added UInt32,
        file_hunks_removed UInt32,
        file_hunks_changed UInt32,
    
        commit_hash String,
        author LowCardinality(String),
        time DateTime,
        commit_message String,
        commit_files_added UInt32,
        commit_files_deleted UInt32,
        commit_files_renamed UInt32,
        commit_files_modified UInt32,
        commit_lines_added UInt32,
        commit_lines_deleted UInt32,
        commit_hunks_added UInt32,
        commit_hunks_removed UInt32,
        commit_hunks_changed UInt32
    ) ENGINE = MergeTree ORDER BY time;
    
  • 使用s3 函数,INSERT INTO SELECT 插入数据

    INSERT INTO git.commits SELECT *
    FROM s3('https://datasets-documentation.s3.amazonaws.com/github/commits/clickhouse/commits.tsv.xz', 'TSV', 'hash String,author LowCardinality(String), time DateTime, message String, files_added UInt32, files_deleted UInt32, files_renamed UInt32, files_modified UInt32, lines_added UInt32, lines_deleted UInt32, hunks_added UInt32, hunks_removed UInt32, hunks_changed UInt32');
    
    INSERT INTO git.file_changes SELECT *
    FROM s3('https://datasets-documentation.s3.amazonaws.com/github/commits/clickhouse/file_changes.tsv.xz', 'TSV', 'change_type Enum(\'Add\' = 1, \'Delete\' = 2, \'Modify\' = 3, \'Rename\' = 4, \'Copy\' = 5, \'Type\' = 6), path LowCardinality(String), old_path LowCardinality(String), file_extension LowCardinality(String), lines_added UInt32, lines_deleted UInt32, hunks_added UInt32, hunks_removed UInt32, hunks_changed UInt32, commit_hash String, author LowCardinality(String), time DateTime, commit_message String, commit_files_added UInt32, commit_files_deleted UInt32, commit_files_renamed UInt32, commit_files_modified UInt32, commit_lines_added UInt32, commit_lines_deleted UInt32, commit_hunks_added UInt32, commit_hunks_removed UInt32, commit_hunks_changed UInt32');
    
    INSERT INTO git.line_changes SELECT *
    FROM s3('https://datasets-documentation.s3.amazonaws.com/github/commits/clickhouse/line_changes.tsv.xz', 'TSV', 'sign Int8, line_number_old UInt32, line_number_new UInt32, hunk_num UInt32, hunk_start_line_number_old UInt32, hunk_start_line_number_new UInt32, hunk_lines_added UInt32,\n    hunk_lines_deleted UInt32, hunk_context LowCardinality(String), line LowCardinality(String), indent UInt8, line_type Enum(\'Empty\' = 0, \'Comment\' = 1, \'Punct\' = 2, \'Code\' = 3), prev_commit_hash String, prev_author LowCardinality(String), prev_time DateTime, file_change_type Enum(\'Add\' = 1, \'Delete\' = 2, \'Modify\' = 3, \'Rename\' = 4, \'Copy\' = 5, \'Type\' = 6),\n    path LowCardinality(String), old_path LowCardinality(String), file_extension LowCardinality(String), file_lines_added UInt32, file_lines_deleted UInt32, file_hunks_added UInt32, file_hunks_removed UInt32, file_hunks_changed UInt32, commit_hash String,\n    author LowCardinality(String), time DateTime, commit_message String, commit_files_added UInt32, commit_files_deleted UInt32, commit_files_renamed UInt32, commit_files_modified UInt32, commit_lines_added UInt32, commit_lines_deleted UInt32, commit_hunks_added UInt32, commit_hunks_removed UInt32, commit_hunks_changed UInt32');
    
    
  • 创建一个物化视图,查每个用户每天 commits 数量:

    create materialized view git.commits_mv
    engine SummingMergeTree
    order by (dt, author)
    as select
    toDate(time) as dt, author, count() as n from git.commits group by dt, author order by dt asc;
    

    SummingMergeTree 表引擎主要用于只关心聚合后的数据,而不关心明细数据的场景,它能够在合并分区的时候按照预先定义的条件聚合汇总数据,将同一分组下的多行数据汇总到一行,可以显著的 减少存储空间并加快数据查询的速度

    注意:这里创建物化视图,并没有数据。需要写入数据,后面会提到。至于为什么不用 POPULATE,因为在填充历史数据的期间, 新进入的这部分数据会被忽略掉, 所以如果对准确性要求非常高, 应慎用。

    -- POPULATE 版
    create materialized view git.commits_mv
    engine SummingMergeTree
    order by (dt, author)
    POPULATE as select
    toDate(time) as dt, author, count() as n from git.commits group by dt, author order by dt asc;
    
    -- ClickHouse 官方并不推荐使用 populated,因为在创建视图过程中插入表中的数据并不会写入视图,会造成数据的丢失。
    
  • 如果创建时,无使用 POPULATE 的话,通过 insert into 写入数据:

    insert into git.commits_mv
    select toDate(time) as dt, author, count() as n from git.commits group by dt, author order by dt asc;
    

    如果无报错的话,此时应该是能看视图的数据的。也可以验证下,在源数据有新增的情况下,是否会更新到视图里:

    -- 写一条数据看看是否会自动更新视图
    insert into git.commits (hash, author, time, message, files_added, files_deleted, files_renamed, files_modified, lines_added, lines_deleted, hunks_added, hunks_removed, hunks_changed) values ('488610bd96415bdb8a718135676cxdf6a665829922', 'Nikita Taranov', '2022-11-30 18:22:24', 'impl (#43709)', 2, 0, 0, 3, 50, 31, 5, 1, 1);
    

    结果:是会更新的。但是你多新增几条的话,commits_mv 视图里,并没有对其汇总?在使用物化视图(SummingMergeTree 引擎)的时候,也需要按照聚合查询来写 sql,因为虽然 SummingMergeTree 会自己预聚合,但是并不是实时的,具体执行聚合的时机并 不可控。

  • 即查询的 sql 如下:文章来源地址https://www.toymoban.com/news/detail-679118.html

    select dt, author, sum(n) from git.commits_mv group by dt ,author order by dt desc;
    
注意事项
  • 在创建 materialized view(简称 MV) 时,不要使用 POPULATE 关键字,而是在物化视图表建好之后将数据导入。
  • 在使用 MV 的聚合引擎时(例:SummingMergeTree),也需要按聚合查询来写 sql,因为聚合时间不可控。

到了这里,关于ClickHouse-物化视图的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • ClickHouse与Doris数据库比较

    都说“实践是检验真理的唯一标准”,光说不练假把式,那么本文就通过实际的测试来感受一下Doris和clickhouse在读写方面的性能差距,看看Doris盛名之下,是否真有屠龙之技;clickhouse长锋出鞘,是否敢缚苍龙? 废话不多说,上货。 在这里,我使用多台物理机搭建了clickhouse和

    2024年01月22日
    浏览(37)
  • 大数据ClickHouse(五):数据库引擎介绍与实例演示

    文章目录 数据库引擎介绍与实例演示 一、Ordinary默认数据库引擎 二、MySQL数据库引擎

    2024年02月03日
    浏览(37)
  • 统一观测|借助 Prometheus 监控 ClickHouse 数据库

    ClickHouse 作为用于联机分析(OLAP)的列式数据库管理系统(DBMS), 最核心的特点是极致压缩率和极速查询性能。同时,ClickHouse 支持 SQL 查询,在基于大宽表的聚合分析查询场景下展现出优异的性能。因此,获得了广泛的应用。本文旨在分享阿里云可观测监控 Prometheus 版对开源 Cli

    2024年02月14日
    浏览(32)
  • Spring Boot集成JPA和ClickHouse数据库

    Spring Boot是一个用于创建独立的、基于Spring的应用程序的框架。它具有快速开发特性,可以大大减少开发人员的工作量。JPA(Java Persistence API)是Java中处理关系型数据库持久化的标准规范,而ClickHouse是一个高性能、分布式的列式数据库。 本文将介绍如何在Spring Boot项目中集成

    2024年02月09日
    浏览(43)
  • 分布式数据库(DorisDB、Clickhouse、TiDB)调研

    B站视频:DorisDB VS ClickHouse OLAP PK 1.1 DorisDB 场量:线上数据应用 访问官方网站 DorisDB企业版文档 单表/多表查询,DorisDB总体时间最短 单表查询:DorisDB最快次数最多,ClickHouse次之 多表查询:DorisDB所有执行均最快 DorisDB多表关联效率好 支持各种主流分布式Join,不仅支持大宽表模

    2024年02月06日
    浏览(35)
  • clickhouse数据库 使用http 方式交付查询sql

    今天使用clickhouse 的HTTP 方式进行查询语句 clickhouse  服务  搭建在192.168.0.111 上面 那么我们如何快速的去查询呢   如下 我们可以使用curl 功能 或者直接在浏览器上输入对应的查询命令  如下: 说明: 前面的IP 是我们clickhouse所在的服务器IP底子 端口      8123     默认的H

    2024年01月25日
    浏览(35)
  • [1180]clickhouse查看数据库和表的容量大小

    在mysql中information_schema这个数据库中保存了mysql服务器所有数据库的信息, 而在clickhouse,我们可以通过system.parts查看clickhouse数据库和表的容量大小、行数、压缩率以及分区信息。 在此通过测试数据库来说明。 结果为:这种结果显示的大小size是字节,我们如何转换为常见的

    2024年02月05日
    浏览(45)
  • Python 连接clickhouse数据库以及新建表结构,csv导入数据

    目录 一、Python 连接clickhouse数据库 ◼ clickhouse对外的接口协议通常有两种形式: ◼ 代码实现部分: 二、使用客户端工具DBeaver连接clickhouse ◼ 新建clickhouse表 三、DBeaver 连接clickhouse 用csv文件导入数据 ◼ 导入方式: 方法一:使用DBeaver自带导入数据功能; 方法二:具体方式如

    2024年02月08日
    浏览(81)
  • (三十六)大数据实战——ClickHouse数据库的部署安装实现

    ClickHouse是俄罗斯的Yandex于2016年开源的列式存储数据库 DBMS ),使用C语言编写,主要用于在线分析处理查询( OLAP ),能够使用SQL查询实时生成分析数据报告。 列式存储 :数据按列进行存储,这使得 ClickHouse 能够高效地处理聚合查询和分析操作; 高性能 :ClickHouse 被设计用

    2024年02月19日
    浏览(32)
  • OLAP型数据库 ClickHouse的简介 应用场景 优势 不足

    ClickHouse 是一个开源的分布式列式数据库管理系统 (DBMS),专门用于在线分析处理 (OLAP)。它最初由 Yandex 开发,并且在处理大规模数据分析和实时查询方面表现出色。以下是关于 ClickHouse 的简介、应用场景、优势和不足的概述: ClickHouse 是一个高性能的列式数据库管理系统,专

    2024年02月02日
    浏览(42)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包