当前位置:网站首页>Clickhouse SQL operation
Clickhouse SQL operation
2022-04-23 17:06:00 【Magic Flute love】
SQL operation
Basically, traditional relational databases ( With MySQL For example ) Of SQL sentence ,ClickHouse Basically supported , But there are some differences .
Insert
Basic and standard SQL(MySQL) Almost the same
(1) standard
insert into [table_name] values(...),(....)
(2) Insert from table to table
insert into [table_name] select a,b,c from [table_name_2]
Update and Delete( Not recommended for use )
ClickHouse Provides Delete and Update The ability of , This kind of operation is called Mutation Inquire about , It can be seen as Alter A kind of .
Although it can be modified and deleted , But as usual OLTP The database is different ,Mutation A statement is a very “ heavy ” The operation of , And it doesn't support transactions .
“ heavy ” The main reason is that each modification or deletion will result in abandoning the original partition of the target data , Rebuild new partition . So try to make batch changes , Do not operate on small data frequently .
(1) Delete operation
alter table t_order_smt delete where sku_id ='sku_001';
(2) Modify the operating
alter table t_order_smt update total_amount=toDecimal32(2000.00,2) where id =102;
Because the operation is relatively “ heavy ”, therefore Mutation The statement is executed in two steps , In fact, the part of synchronous execution is only to add data, add partitions, and mark the old partitions with logical invalidation marks . Until the partition merge is triggered , Will delete old data to free up disk space , Generally, such functions will not be opened to users , Done by the administrator .
Elegant operation :
update operation : When updating, insert the same data as before except for an updated department (order by The fields must be the same ), And use ReplacingMergeTree engine , Add a logo version Field of , When querying, only query version The largest field is the latest ; Because the engine will perform de duplication operation when merging , Therefore, the old data will be deleted .
Delete operation : When deleting, identify the deleted field as delete , When querying, only the fields that have not been deleted are queried .
Query operation
ClickHouse Basically the same as the standard SQL Not much difference
* Support subquery
* Support CTE(Common Table Expression Common table expression with Clause )
* Support a wide variety of JOIN, however JOIN The operation cannot use the cache , So even if it's the same twice JOIN sentence ,ClickHouse It will also be regarded as two new SQL
* Window function ( Officials are testing …)
* Custom functions are not supported , See the official website for system functions
* GROUP BY Operation added with rollup \ with cube \ with total Used to calculate subtotals and totals .
insert data
/* Delete old data */
alter table t_order_mt delete where 1=1;
insert into t_order_mt values
(101,'sku_001',1000.00,'2020-06-01 12:00:00'),
(101,'sku_002',2000.00,'2020-06-01 12:00:00'),
(103,'sku_004',2500.00,'2020-06-01 12:00:00'),
(104,'sku_002',2000.00,'2020-06-01 12:00:00'),
(105,'sku_003',600.00,'2020-06-02 12:00:00'),
(106,'sku_001',1000.00,'2020-06-04 12:00:00'),
(107,'sku_002',2000.00,'2020-06-04 12:00:00'),
(108,'sku_004',2500.00,'2020-06-04 12:00:00'),
(109,'sku_002',2000.00,'2020-06-04 12:00:00'),
(110,'sku_003',600.00,'2020-06-01 12:00:00');
/* The query data is as follows */
┌──id─┬─sku_id──┬─total_amount─┬─────────create_time─┐
│ 105 │ sku_003 │ 600.00 │ 2020-06-02 12:00:00 │
└─────┴─────────┴──────────────┴─────────────────────┘
┌──id─┬─sku_id──┬─total_amount─┬─────────create_time─┐
│ 101 │ sku_001 │ 1000.00 │ 2020-06-01 12:00:00 │
│ 101 │ sku_002 │ 2000.00 │ 2020-06-01 12:00:00 │
│ 103 │ sku_004 │ 2500.00 │ 2020-06-01 12:00:00 │
│ 104 │ sku_002 │ 2000.00 │ 2020-06-01 12:00:00 │
│ 110 │ sku_003 │ 600.00 │ 2020-06-01 12:00:00 │
└─────┴─────────┴──────────────┴─────────────────────┘
┌──id─┬─sku_id──┬─total_amount─┬─────────create_time─┐
│ 106 │ sku_001 │ 1000.00 │ 2020-06-04 12:00:00 │
│ 107 │ sku_002 │ 2000.00 │ 2020-06-04 12:00:00 │
│ 108 │ sku_004 │ 2500.00 │ 2020-06-04 12:00:00 │
│ 109 │ sku_002 │ 2000.00 │ 2020-06-04 12:00:00 │
└─────┴─────────┴──────────────┴─────────────────────┘
with rollup: Remove dimensions from right to left for subtotal , Scroll up
select id , sku_id, sum(total_amount) from t_order_mt group by id, sku_id with rollup;
/* Reduce dimensions from right to left */
┌──id─┬─sku_id──┬─sum(total_amount)─┐
│ 110 │ sku_003 │ 600.00 │
│ 109 │ sku_002 │ 2000.00 │
│ 107 │ sku_002 │ 2000.00 │
│ 106 │ sku_001 │ 1000.00 │
│ 104 │ sku_002 │ 2000.00 │
│ 101 │ sku_002 │ 2000.00 │
│ 103 │ sku_004 │ 2500.00 │
│ 108 │ sku_004 │ 2500.00 │
│ 105 │ sku_003 │ 600.00 │
│ 101 │ sku_001 │ 1000.00 │
└─────┴─────────┴───────────────────┘
┌──id─┬─sku_id─┬─sum(total_amount)─┐
│ 110 │ │ 600.00 │
│ 106 │ │ 1000.00 │
│ 105 │ │ 600.00 │
│ 109 │ │ 2000.00 │
│ 107 │ │ 2000.00 │
│ 104 │ │ 2000.00 │
│ 103 │ │ 2500.00 │
│ 108 │ │ 2500.00 │
│ 101 │ │ 3000.00 │
└─────┴────────┴───────────────────┘
┌─id─┬─sku_id─┬─sum(total_amount)─┐
│ 0 │ │ 16200.00 │
└────┴────────┴───────────────────┘
with cube : Remove dimensions from right to left for subtotal , Then remove the dimension from left to right for subtotal , Mostly analysis
select id , sku_id, sum(total_amount) from t_order_mt group by id, sku_id with cube;
/* Reduce dimensions from right to left */
┌──id─┬─sku_id──┬─sum(total_amount)─┐
│ 110 │ sku_003 │ 600.00 │
│ 109 │ sku_002 │ 2000.00 │
│ 107 │ sku_002 │ 2000.00 │
│ 106 │ sku_001 │ 1000.00 │
│ 104 │ sku_002 │ 2000.00 │
│ 101 │ sku_002 │ 2000.00 │
│ 103 │ sku_004 │ 2500.00 │
│ 108 │ sku_004 │ 2500.00 │
│ 105 │ sku_003 │ 600.00 │
│ 101 │ sku_001 │ 1000.00 │
└─────┴─────────┴───────────────────┘
┌──id─┬─sku_id─┬─sum(total_amount)─┐
│ 110 │ │ 600.00 │
│ 106 │ │ 1000.00 │
│ 105 │ │ 600.00 │
│ 109 │ │ 2000.00 │
│ 107 │ │ 2000.00 │
│ 104 │ │ 2000.00 │
│ 103 │ │ 2500.00 │
│ 108 │ │ 2500.00 │
│ 101 │ │ 3000.00 │
└─────┴────────┴───────────────────┘
┌─id─┬─sku_id──┬─sum(total_amount)─┐
│ 0 │ sku_003 │ 1200.00 │
│ 0 │ sku_004 │ 5000.00 │
│ 0 │ sku_001 │ 2000.00 │
│ 0 │ sku_002 │ 8000.00 │
└────┴─────────┴───────────────────┘
┌─id─┬─sku_id─┬─sum(total_amount)─┐
│ 0 │ │ 16200.00 │
└────┴────────┴───────────────────┘
with totals: Only total , A total of
select id, sku_id, sum(total_amount) from t_order_mt group by id, sku_id with totals;
/* Reduce dimensions from right to left */
┌──id─┬─sku_id──┬─sum(total_amount)─┐
│ 110 │ sku_003 │ 600.00 │
│ 109 │ sku_002 │ 2000.00 │
│ 107 │ sku_002 │ 2000.00 │
│ 106 │ sku_001 │ 1000.00 │
│ 104 │ sku_002 │ 2000.00 │
│ 101 │ sku_002 │ 2000.00 │
│ 103 │ sku_004 │ 2500.00 │
│ 108 │ sku_004 │ 2500.00 │
│ 105 │ sku_003 │ 600.00 │
│ 101 │ sku_001 │ 1000.00 │
└─────┴─────────┴───────────────────┘
Totals:
┌─id─┬─sku_id─┬─sum(total_amount)─┐
│ 0 │ │ 16200.00 │
└────┴────────┴───────────────────┘
For example, the dimension is a,b
rollup: The first is No group by; And then there was group by a; And finally group by a, b
cube: Is that all situations have to have , No group by;group by a;group by b;group by a, b
totals: No group by and group by a, b
alter operation
Same as MySQL The modified fields are basically the same
/* New fields */
alter table tableName add column newcolname String after col1;
/* Modify field type */
alter table tableName modify column newcolname String;
/* Delete field */
alter table tableName drop column newcolname;
Derived data
clickhouse-client --query "select * from t_order_mt where create_time='2020-06-01 12:00:00'" --format CSVWithNames > /opt/module/data/rs1.csv
–query amount to mysql -e, Do not enter cli Interface execution statement
More supported format references :https://clickhouse.tech/docs/en/interfaces/formats/
版权声明
本文为[Magic Flute love]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231704450625.html
边栏推荐
- Variable length parameter__ VA_ ARGS__ Macro definitions for and logging
- ClickHouse-数据类型
- Installing labellmg tutorial in Windows
- Lock锁
- Regular filtering of Intranet addresses and segments
- Document operation II (5000 word summary)
- Some problems encountered in recent programming 2021 / 9 / 8
- JS, entries(), keys(), values(), some(), object Assign() traversal array usage
- An essay on the classical "tear down the wall in thinking"
- Customize my_ Strcpy and library strcpy [analog implementation of string related functions]
猜你喜欢
Quick install mongodb
Shell script -- shell programming specification and variables
Lock锁
RTKLIB 2.4.3源码笔记
VLAN advanced technology, VLAN aggregation, super VLAN, sub VLAN
Net standard
1-1 NodeJS
扫码登录的原理你真的了解吗?
Bytevcharts visual chart library, I have everything you want
Detailed explanation of information abstract, digital signature, digital certificate, symmetric encryption and asymmetric encryption
随机推荐
Paging SQL
Detailed explanation of the penetration of network security in the shooting range
Tencent resolves the address according to the IP address
Customize my_ Strcpy and library strcpy [analog implementation of string related functions]
自定义my_strcpy与库strcpy【模拟实现字符串相关函数】
Signalr can actively send data from the server to the client
SQL database
Get the column name list of the table quickly in Oracle
Change the password after installing MySQL in Linux
1-1 NodeJS
Quick install mongodb
Detailed explanation of Niuke - Gloves
ASP. Net core dependency injection service life cycle
Freecodecamp ---- budget & category exercise
Shell-入门、变量、以及基本的语法
Basic case of Baidu map
Mock test
Production environment——
Input file upload
[C#] 彻底搞明白深拷贝