clickhouse学习笔记(二)数据类型

目录

一、基础数据类型

1、整数类型

2、浮点数类型

3、布尔类型

4、字符串和固定字符串

5、日期和时间类型

6、创建基础数据类型的建表语句及插入语句

二、复合数据类型

1、数组

2、元组

3、枚举类型

4、嵌套数据结构

5、Map

三、特殊数据类型

1、低基数编码(LowCardinality)

2、Nullable

3、Decimal

4、创建包含LowCardinality,Nullable,Decimal类型的建表语句及插入语句

​编辑

解释

注意

四、性能优化的数据类型


clickhouse的数据类型分为:基础类型,复合类型,特殊类型

一、基础数据类型

1、整数类型

ClickHouse支持多种整数类型,包括有符号和无符号整数。这些类型包括:

  • UInt8UInt16UInt32UInt64:无符号整数。
  • Int8Int16Int32Int64:有符号整数。
名称      字节 范围     类型对应
Int8     1  -128 ~ 127  byte
Int16     2 -32768 ~ 32767  short
Int32     4 -2147483648 ~ 2147483647  int
Int64     8  -9223372036854775808 ~ 9223372036854775807  bigint
UInt8     1 0 ~ 255    Unsigned byte
UInt16     2  0 ~ 65535   Unsigned short
UInt32     4  0 ~ 4294967295 Unsigned int
UInt64     8  0 ~ 18446744073709551615  Unsigned bigint

整数类型的选择取决于数据的范围和存储需求。

2、浮点数类型

ClickHouse提供了两种浮点数类型:

  • Float32:单精度浮点数。
  • Float64:双精度浮点数。
名称 字节 小数点有效位数 类型对应
Float32 4 7 float
Float64 8 16 double

这些类型适用于存储浮点数数据。

3、布尔类型

  • Bool:布尔类型,可以存储0(假)或1(真)。

4、字符串和固定字符串

  • String:可变长度的字符串。
  • FixedString(N):固定长度的字符串,N是字符串的长度,长度超过N,会报错;长度不足N, 用null字节填充末尾
  • UUID:唯一值,32位,格式位8-4-4-4-12,默认都是0填充。例如:e50157b5-c809-411f-bb8d-3870ffa883bb

固定字符串类型可以提高查询性能,因为它们在内存中占用固定空间。

5、日期和时间类型

  • Date:日期,格式为YYYY-MM-DD
  • DateTime:日期和时间,格式为YYYY-MM-DD hh:mm:ss
  • DateTime64:具有时间精度的日期和时间。

DateTime类型支持时区,而DateTime64则支持更细粒度的时间精度。

6、创建基础数据类型的建表语句及插入语句

CREATE TABLE mixed_data_types (
    id UInt64,
    float_value Float64,
    is_active Bool,
    description String,
    code FixedString(10),
    created_date Date,
    updated_time DateTime
) ENGINE = MergeTree()
ORDER BY id;

#插入语句
INSERT INTO mixed_data_types (id, float_value, is_active, description, code, created_date, updated_time)
VALUES
(1, 123.456, true, 'Sample description', 'ABC123456', '2023-01-01', '2023-01-01 12:00:00');

解释各字段类型:
id UInt64 - 这是一个64位无符号整数类型,用于存储唯一的标识符或ID。

float_value Float64 - 这是一个64位浮点数类型,用于存储需要高精度的小数点数值。

is_active Bool - 这是一个布尔类型字段,用于存储逻辑值(真或假)。在ClickHouse中,布尔值实际上是以8位整数形式存储的,其中0表示False,非零值表示True。

description String - 这是一个变长字符串类型,用于存储描述性的文本数据,如备注或说明。

code FixedString(10) - 这是一个固定长度字符串类型,长度为10个字符。这种类型适合存储固定长度的代码或标识符,如产品代码或邮编。

created_date Date - 这是一个日期类型,用于存储日期信息,格式为YYYY-MM-DD。

updated_time DateTime - 这是一个日期时间类型,用于存储日期和时间信息,格式为YYYY-MM-DD HH:MM:SS。

二、复合数据类型

1、数组

ClickHouse支持数组类型,格式为Array(T),其中T是数组中的元素类型。

数组的定义
[1,2,3,4.5]
array('a','b','c')
[1,2,3,'hello'] --错误,数据类型不一致

create table test_array(
id Int8,
hobby Array(String)
)engine=Memory;
insert into test_array values(1,['eat','drink','la','sa']),(2,array('sleep','play'));
select * from test_array;
┌─id─┬─hobby─────────────────────┐
│  1 │ ['eat','drink','la','sa'] │
│  2 │ ['sleep','play']          │
└────┴───────────────────────────┘

select id,hobby,toTypeName(hobby) from test_array;
┌─id─┬─hobby─────────────────────┬─toTypeName(hobby)─┐
│  1 │ ['eat','drink','la','sa'] │ Array(String)     │
│  2 │ ['sleep','play']          │ Array(String)     │
└────┴───────────────────────────┴───────────────────┘

select id,hobby[1],toTypeName(hobby) from test_array;   --数组的取值 [index] index范围:1-based
┌─id─┬─arrayElement(hobby, 1)─┬─toTypeName(hobby)─┐
│  1 │ eat                    │ Array(String)     │
│  2 │ sleep                  │ Array(String)     │
└────┴────────────────────────┴───────────────────┘

2、元组

元组类型允许将多个不同类型的列组合在一起,格式为Tuple(T1, T2, ..., Tn)

select (1,2,'s',5.6) as x ,toTypeName(x);
┌─x─────────────┬─toTypeName((1, 2, 's', 5.6))─────────┐
│ (1,2,'s',5.6) │ Tuple(UInt8, UInt8, String, Float64) │
└───────────────┴──────────────────────────────────────┘

create table test_tuple(
c1 Tuple(UInt8,String,Float64)
)engine=Memory;

insert into test_tuple values((12,'ads',12.132)),(tuple(34,'fre',34.2)); 
select * from test_tuple;
┌─c1────────────────┐
│ (12,'ads',12.132) │
│ (34,'fre',34.2)   │
└───────────────────┘

3、枚举类型

枚举类型用于存储有限数量的值,格式为Enum('value1' = num1, 'value2' = num2, ...)

create table test_enum(
id Int8,
color Enum('RED'=1,'GREEN'=2,'BLUE'=3)
)engine=Memory;

insert into test_enum values(1,'RED');
insert into test_enum values(2,2);
select * from test_enum;
┌─id─┬─color─┐
│  1 │ RED   │
└────┴───────┘
┌─id─┬─color─┐
│  2 │ GREEN │
└────┴───────┘

4、嵌套数据结构

嵌套数据结构允许创建复杂的数据结构,格式为Nested(Name Type, Name Type, ...)

create table test_nested(
id Int8,
name String,
scores Nested(    
times UInt8,    
maths Float64,    
chinese Float64,    
english Float64    )
)engine=Memory;
desc test_nested;
┌─name───────────┬─type───────────┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ id             │ Int8           │              │                    │         │                  │                │
│ name           │ String         │              │                    │         │                  │                │
│ scores.times   │ Array(UInt8)   │              │                    │         │                  │                │
│ scores.maths   │ Array(Float64) │              │                    │         │                  │                │
│ scores.chinese │ Array(Float64) │              │                    │         │                  │                │
│ scores.english │ Array(Float64) │              │                    │         │                  │                │
└────────────────┴────────────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘

insert into test_nested values(1,'wyb',[1,2,3],[11,12,13],[14,15,16],[17,18,19]);
insert into test_nested values(2,'lyf',[1,2,],[21,22],[23,16],[25,19]);
select * from test_nested;
┌─id─┬─name─┬─scores.times─┬─scores.maths─┬─scores.chinese─┬─scores.english─┐
│  1 │ wyb  │ [1,2,3]      │ [11,12,13]   │ [14,15,16]     │ [17,18,19]     │
└────┴──────┴──────────────┴──────────────┴────────────────┴────────────────┘
┌─id─┬─name─┬─scores.times─┬─scores.maths─┬─scores.chinese─┬─scores.english─┐
│  2 │ lyf  │ [1,2]        │ [21,22]      │ [23,16]        │ [25,19]        │
└────┴──────┴──────────────┴──────────────┴────────────────┴────────────────┘

select name,scores.maths from test_nested where id=1;
┌─name─┬─scores.maths─┐
│ wyb  │ [11,12,13]   │
└──────┴──────────────┘

5、Map

Map类型的一般语法如下:

Map(K, V)Map(K,V)

其中:

  • K 是键的类型。
  • V 是值的类型。

键和值可以是ClickHouse支持的任何数据类型。

create table test_map (
a Map(String,Int8)
)engine=Memory;


insert into test_map values(map('key1',1,'key2',10,'key3',2,'key4',20));
select a['key2'] from test_map;
┌─arrayElement(a,'key2')──┐
│                      10 │
└─────────────────────────┘

三、特殊数据类型

1、低基数编码(LowCardinality)

LowCardinality(T)类型允许存储大量重复值的列,而只使用少量的内存。

2、Nullable

Nullable(T)类型允许存储NULL值,其中T是基本数据类型。

3、Decimal

Decimal(P, S)类型用于存储固定精度的小数,其中P是精度,S是小数位数。

4、创建包含LowCardinality,Nullable,Decimal类型的建表语句及插入语句

CREATE TABLE product_sales (
    product_id UInt64,
    category LowCardinality(String),
    price Decimal(10, 2),
    supplier Nullable(String),
    sale_date Date
) ENGINE = MergeTree()
ORDER BY (product_id, sale_date);



INSERT INTO product_sales (product_id, category, price, supplier, sale_date)
VALUES
(1, 'Electronics', 123.45, 'Tech Supplier Inc.', '2023-01-01'),
(2, 'Clothing', 99.99, NULL, '2023-01-02'),
(3, 'Books', 15.99, 'Bookstore Ltd.', '2023-01-03');

select * from product_sales

解释


category LowCardinality(String):LowCardinality类型用于优化那些具有相对较少不同值的列(即低基数),它可以显著减少存储空间和加快查询速度。这里的category字段用于存储产品类别。

price Decimal(10, 2):Decimal类型用于存储固定精度的小数,这里Decimal(10, 2)表示总位数为10位,其中小数点后有2位。适合存储商品价格这类需要精确到几分钱的数值。

supplier Nullable(String):Nullable类型允许字段值为NULL,这对于处理可能缺失的数据非常有用。在这里,supplier字段可能没有值,因此使用了Nullable类型。

注意
  • 当使用Nullable类型插入数据时,如果字段值未知或不适用,可以使用NULL来表示。

  • Decimal类型的值在插入时应准确匹配定义的精度,否则可能会导致错误。

四、性能优化的数据类型

ClickHouse的数据类型设计考虑了性能优化,例如:

  • 使用UInt类型代替Int类型,因为正数比负数更常见,这可以减少存储需求。
  • 使用LowCardinality类型来减少重复值的存储。
  • 使用Nullable类型来存储可能包含NULL值的数据。

相关推荐

  1. Python学习笔记数据类型及相关函数

    2024-06-09 06:46:04       39 阅读
  2. ResutBuilder 学习笔记:增加新的输入数据类型

    2024-06-09 06:46:04       20 阅读
  3. ClickHouse简介+数据类型

    2024-06-09 06:46:04       9 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-09 06:46:04       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-09 06:46:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-09 06:46:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-09 06:46:04       18 阅读

热门阅读

  1. 【深度学习基础】模型文件介绍

    2024-06-09 06:46:04       9 阅读
  2. 用旧安卓手机当 linux 开发机

    2024-06-09 06:46:04       13 阅读
  3. LeetCode题练习与总结:三角形最小路径和--120

    2024-06-09 06:46:04       9 阅读
  4. Sony前端连接功放:深度解析与实用指南

    2024-06-09 06:46:04       12 阅读
  5. Linux服务器配置一个简单的DNS

    2024-06-09 06:46:04       7 阅读
  6. 【 k8s 标签与选择器 】

    2024-06-09 06:46:04       11 阅读
  7. adb 删除文件或文件夹

    2024-06-09 06:46:04       7 阅读
  8. 字母异位词分组(charyw)

    2024-06-09 06:46:04       12 阅读