Clickhouse常用整理& linux操作clickhouse命令


進入click(不加上-m的話,進入之后只能一次寫一行,不能建表)

clickhouse client -m

查看數據庫

show databases;  

創建一個數據庫

create database db_doit; 

刪除數據庫

drop database  db_doit;   

查看表

show tables:  

查看當前使用的數據庫

select currentDatabase();

創建一個表(建表的時候指定數據類型,建表的時候一定要指定表引擎)

create table tb_user(
	uid Int32,
	name String,
	age UInt32,
	gender String
)engine = TinyLog;

查看表的結構

desc tb_user;

插入語句

insert into tb_user values(1,'hello',23,'M');
insert into tb_user values(2,'上海',33,'F');

查看表

select * from  tb_user;

DateTime(以下三種都可以)

create table tb_date1(
 timestamp  DateTime
)engine = TinyLog;

create table tb_date2(
	date timestamp
)engine = TinyLog;

create table tb_date3(
	datetime date
)engine = TinyLog;

插入時間

insert into tb_date values('2020-08-24 21:06:00');

Enum 枚舉(定義常量)

create table tb_enum(
	m Enum('hello'=1,'world'=2)
)engine = TinyLog;

insert into tb_enum values('hello'),('world'),('hello');

select cast(m,'Int8') from tb_enum;

將hello ,world 轉為Int8類型

數組(Array(數據類型))
create table tb_array(
	name String,
	hobby Array(String)
	)engine = TinyLog;

**插入**

insert into tb_array values('你好',['h','l','hello']);

**數組有角標,然后是從1開始**

select name ,hobby[2] from tb_array;

MegerTree建表

需要主鍵,排序字段 ( primary key , order by) 兩個一致

create table tb_megertree(
	uid Int32,
	name String,
	age UInt8,
	birthday Date,
	gender String)
engine=MergeTree()
order by uid;

插入數據

insert into tb_megertree values(2,'李白',60,'123324435','M');
insert into tb_megertree values(24,'杜甫',59,1234567,'M'), (3,'李清照',55,1234323,'F');

insert into tb_megertree values(6,'徐志摩',50,'333324435','M');

partition : 分區
create table tb_partition(
	uid Int8,
	address String
)
engine=MergeTree()
order by uid
partition by address;

insert into tb_partition values(3,'北京'),(5,'北京'),(1,'上海'),(7,'北京'),(30,'北京'),(11,'上海');

再插入

insert into tb_partition values(33,'上海'),(53,'北京'),(13,'上海');

再合並

optimize table tb_partition;
optimize table tb_partition;

一次合並一個分區,執行兩次分區全部合並ReplacingMergeTree刪除區內主鍵相同的數據 保留指定的字段中的最大版本


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM