当前位置:网站首页>Clickhouse - data type
Clickhouse - data type
2022-04-23 17:06:00 【Magic Flute love】
data type
integer
Fixed length integers , Including signed or unsigned integers .
Integer range (-2 Of n-1 Second party to 2 Of n-1 Power -1):
Int8 - [-128 : 127] , Corresponding java Of byte
Int16 - [-32768 : 32767], Corresponding java Of short
Int32 - [-2147483648 : 2147483647], Corresponding java Of int
Int64 - [-9223372036854775808 : 9223372036854775807], Corresponding java Of long
Unsigned integer range (0~2 Of n Power -1):
UInt8 - [0 : 255]
UInt16 - [0 : 65535]
UInt32 - [0 : 4294967295]
UInt64 - [0 : 18446744073709551615]
Use scenarios : Number 、 Number 、 It can also be storage type id.
floating-point
Float32 - float
Float64 – double
It is recommended to store data in integer form as much as possible . for example , Convert fixed precision numbers to integer values , For example, time is expressed in milliseconds , Because floating-point calculation may cause rounding error .
Use scenarios : Generally, the data value is relatively small , It does not involve a large number of statistical calculations , When the accuracy requirement is not high . For example, keep the weight of goods .
Boolean type
There is no separate type for storing Boolean values . have access to UInt8 type , The value is limited to 0 or 1.
Decimal type
Signed floating point numbers , You can add 、 To maintain precision in subtraction and multiplication . For division , The least significant number will be discarded ( No rounding ). Floating point numbers are saved as Decimal type , Like money .
There are three kinds of statements :
- Decimal32(s), amount to Decimal(9-s,s), The number of significant digits is 1~9, for example Decimal32(5) Indicates that the integer part has 4 position , The fractional part has 5 position , And whether the truncation is rounded , Direct truncation
- Decimal64(s), amount to Decimal(18-s,s), The number of significant digits is 1~18
- Decimal128(s), amount to Decimal(38-s,s), The number of significant digits is 1~38
s Identify decimal places
Use scenarios : General amount field 、 exchange rate 、 Interest rate and other fields to ensure decimal point accuracy , All use Decimal For storage .
character string
1)String
Strings can be of any length . It can contain any set of bytes , Contains empty bytes .
2)FixedString(N)
Fixed length N String ,N It must be strictly positive natural numbers . When the server read length is less than N When the string of , By at the end of the string Add empty bytes In order to achieve N bytes . When the server read length is greater than N When the string of , Will return an error message .
And String comparison , Rarely used FixedString, Because it's not very convenient to use .
Use scenarios : name 、 A word description 、 Character encoding . Fixed length can save some fixed length content , For example, some coding , Gender, etc., but considering a certain risk of change , The benefits are not obvious enough , Therefore, the use of fixed length strings is of limited significance .
Enumeration type
Include Enum8 and Enum16 type .Enum preservation ‘string’= integer Correspondence of .
Enum8 use ‘String’= Int8 Description of .
Enum16 use ‘String’= Int16 Description of .
Usage demonstration
Create one with an enumeration Enum8(‘hello’ = 1, ‘world’ = 2) Column of type
CREATE TABLE t_enum
(
x Enum8('hello' = 1, 'world' = 2)
)
ENGINE = TinyLog;
This x Columns can only store values listed in the type definition :‘hello’ or ’world’
hadoop113 :) INSERT INTO t_enum VALUES ('hello'), ('world'), ('hello');
And then query
hadoop113 :) select * from t_enum;
SELECT *
FROM t_enum
Query id: 99231044-3186-4734-9b11-26c81083e054
┌─x─────┐
│ hello │
│ world │
│ hello │
└───────┘
3 rows in set. Elapsed: 0.006 sec.
If you try to save any other values ,ClickHouse Throw an exception
hadoop113 :) insert into t_enum values('a')
...
Code: 36. DB::Exception: Unknown element 'a' for enum: data for INSERT was parsed from query
...
If you need to see the value of the corresponding line , You have to Enum Convert value to integer type
hadoop113 :) SELECT CAST(x, 'Int8') FROM t_enum;
┌─CAST(x, 'Int8')─┐
│ 1 │
│ 2 │
│ 1 │
└─────────────────┘
Use scenarios : For some states 、 Type of field is a kind of spatial optimization , It's also a kind of data constraint . However, in actual use, the maintenance cost is often increased due to the change of some data content , Even data loss . So use... With caution .
Time type
at present ClickHouse There are three types of time
* Date Accept years - month - Japan String, such as ‘2019-12-16’
* Datetime Accept years - month - Japan when : branch : second String, such as ‘2019-12-16 20:50:10’
* Datetime64 Accept years - month - Japan when : branch : second . Subsecond String, such as ‘2019-12-16 20:50:10.66’
The date type , Store in two bytes , From 1970-01-01 ( Unsigned ) To the current date value .
Array
Array(T): from T An array of type elements .
T It can be any type , Contains array types . but Multidimensional arrays are not recommended ,ClickHouse Limited support for multidimensional arrays . for example , Can't be in MergeTree Tables store multidimensional arrays .
/* How to create an array 1, Use array function array(T), among toTypeName Is to get the type of column to */
hadoop102 :) SELECT array(1, 2) AS x, toTypeName(x);
┌─x─────┬─toTypeName(array(1, 2))─┐
│ [1,2] │ Array(UInt8) │
└───────┴─────────────────────────┘
/* How to create an array 2: Use square brackets [] */
hadoop102 :) SELECT [1, 2] AS x, toTypeName(x);
┌─x─────┬─toTypeName(array(1, 2))─┐
│ [1,2] │ Array(UInt8) │
└───────┴─────────────────────────┘
Null value Nullable
It is not recommended to use Nullable, Because it almost always has a negative impact on performance , Therefore, if you have to save a null value when designing the database , It is recommended to convert to a special value of the corresponding type , for example Int Type of -1 etc. , Can not use Nulltable You don't have to .
There are also many data structures , Please refer to the official documents :https://clickhouse.yandex/docs/zh/data_types/
版权声明
本文为[Magic Flute love]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231704450717.html
边栏推荐
- [PROJECT] small hat takeout (8)
- 手写事件发布订阅框架
- Detailed explanation of Niuke - Gloves
- [problem solving] [show2012] random tree
- VLAN advanced technology, VLAN aggregation, super VLAN, sub VLAN
- Linux MySQL data timing dump
- 1-3 components and modules
- 正则过滤内网地址和网段
- Document operation II (5000 word summary)
- STM32__ 03 - beginner timer
猜你喜欢

org. apache. parquet. schema. InvalidSchemaException: A group type can not be empty. Parquet does not su

On lambda powertools typescript

Grpc gateway based on Ocelot

网络安全之渗透靶场实战详解

. net cross platform principle (Part I)

How vscode compares the similarities and differences between two files

1-1 NodeJS

SQL database

VLAN advanced technology, VLAN aggregation, super VLAN, sub VLAN

ASP. Net core dependency injection service life cycle
随机推荐
Input file upload
Idea of batch manufacturing test data, with source code
Path environment variable
文件操作《二》(5000字总结篇)
Net standard
Generate random numbers with high quality and Gaussian distribution
Conversion between hexadecimal numbers
oracle 中快速获取表的列名列表
JS to find the character that appears three times in the string
VLAN高级技术,VLAN聚合,超级Super VLAN ,Sub VLAN
Sub database and sub table & shardingsphere
织梦DEDECMS安全设置指南
ClickHouse-数据类型
SQL: How to parse Microsoft Transact-SQL Statements in C# and to match the column aliases of a view
Signalr can actively send data from the server to the client
Smart doc + Torna generate interface document
New keyword learning and summary
[PROJECT] small hat takeout (8)
Shell脚本——Shell编程规范及变量
Baidu Map 3D rotation and tilt angle adjustment