当前位置: 首页 > 工具软件 > Vertica > 使用案例 >

Vertica常用语法

锺离马鲁
2023-12-01

转载请注明出处,谢谢~

一、建表

1.创建普通表

CREATE TABLE INCOME.ORDER_FEE_XMYCX_201910
(
CODE_DESC varchar(50),
CHARGE_PAY_FEE_RELATION numeric(15,0),
CREATE_DATE timestamp
) DIRECT
ORDER BY CODE_DESC
SEGMENTED BY HASH(CHARGE_PAY_FEE_RELATION) ALL NODES KSAFE 1;

2.创建分区表

CREATE TABLE INCOME.OWE_B30_B34_H
(
HANDLE_MONTH char(6),
LATN_ID varchar(10) NOT NULL,
SERV_CODE varchar(30),
ACCT_ITEM_TYPE_CODE varchar(30),
ACCT_PERIOD varchar(30),
ACCT_MONTH varchar(30),
OWE_AMOUNT numeric(20,0),
UNCONFIRM_OWE numeric(20,0),
CONFIRM_OWE numeric(20,0),
PAY_TYPE varchar(10)
) DIRECT
ORDER BY SERV_CODE
SEGMENTED BY HASH(SERV_CODE) ALL NODES KSAFE 1
PARTITION BY (LATN_ID);

注意:分区字段一定要加上NOT NULL

3.修改普通表为分区表
首先需要确保分区字段不允许为空

ALTER TABLE TEST.FCT_FOURNET_WLANAP_EQUP_A ALTER DAY_ID SET NOT NULL;
ALTER TABLE TEST.FCT_FOURNET_WLANAP_EQUP_A PARTITION BY DAY_ID;

4.创建临时表(连接关闭后表会自动删掉)

CREATE LOCAL TEMPORARY TABLE ORG_TREE_TMP
ON COMMIT PRESERVE ROWS AS
SELECT * FROM INCOME.ORG_TREE_INFO_NEW;
 
CREATE LOCAL TEMPORARY TABLE input(id,name)
ON COMMIT PRESERVE ROWS AS
SELECT 42,'Arthur Dent'
UNION ALL SELECT 43,'Ford Prefect'
UNION ALL SELECT 44,'Tricia McMillan'
KSAFE 0;

Vertica临时表一些关键字说明:

  1. CREATE TEMPORARY TABLE :表示建的为SESSION级别的临时表;
  2. ON COMMIT PRESERVE ROWS :表示在SESSION断开前临时表一直保存数据;
  3. KSAFE 0 :该语句表示,数据存储时的副本数(防止节点故障数据丢失的),对于临时表时指定0即可,表示不创建副本。
    注意:如果使用CREATE LOCAL TEMPORARY TABLE tablename + 字段类型语句创建临时表,需要将ON COMMIT PRESERVE ROWS关键词放到字段类型后面。如果使用CREATE LOCAL TEMPORARY TABLE tablename AS 语句创建临时表,则不需要调整。

5.vertica建表的一些规则
在建表时如何选择order by 的列:
1、order by 后表中插入的数据是有序的,所以order by 的列就源自于你在查询语句时使用的where 字句的内容。例如,如果字句查询中有where x=1 and y=2,那么建立projection时order by (x, y)查询的时候就会迅速定位到符合条件的数据
2、group by 后面的字段,出现在order by 中也可以优化查询。
3、order by 不要建立在LONG VARBINARY and LONG VARCHAR的列
Segmentation
1、Segmentation by hash()就是按照某一列,打散数据,把数据均匀的分布在各个节点上,对于大表,要记得使用。所以 hash里的列是主键最好,也就是说该列数据不重复的值越多,越适合做hash.
2、Segmentation by 的列不要用LONG VARBINARY and LONG VARCHAR columns.

二、插入数据

跟Oracle一样,eg:

INSET INTO STORE.LW_TEST VALUES('LW','001',20,20);
INSERT INTO INCOME.SUB_CODE_ARPU
SELECT * FROM APP_Z.SUB_CODE_ARPU_19 WHERE BIL_MONTH = '201910';
INSERT INTO PUBLIC.WEIBOUSER ( 字段1,字段2,字段3,...) ( SELECT UID,'',字段3,... FROM PUBLIC.WEIBOFRIEND );

三、查看表结构

导出表结构(能查看建表语句和注释,推荐)

SELECT EXPORT_OBJECTS('','APP_X.TEST');

查询表的列对应的注释(查询太慢,不推荐)

SELECT t3.anchor_table_name AS Table_name,
SUBSTR (t1.object_name, INSTR (t1.object_name, '.', 1) + 1) AS Column_name,
t1.comment AS comment
FROM comments t1, projections t3
WHERE SUBSTR (t1.object_name, 1, INSTR (t1.object_name, '.', 1) - 1) =
t3.projection_name
AND t1.object_type = 'COLUMN'
AND t3.anchor_table_name = 'LST_DFC_NEW_MON_C'
ORDER BY t3.anchor_table_name;

四、删除字段

ALTER TABLE INCOME.SERV_NEW DROP COLUMN SERV_NEWID;

五、重命名表

ALTER TABLE INCOME.原表名 RENAME TO 修改后的表名;

–注意新表名前不用加表属主

六、修改列名

ALTER TABLE TEST.RP_CT_OLCS_WORK_LIST_DISPOSE_MON RENAME STATIS_DATE TO STATIS_MONTH;

七、修改表所属用户

ALTER TABLE XXXX OWNER TO MID;

八、赋权

1)一个SCHEMA上的权限赋给另一个用户:

GRANT USAGE ON SCHEMA DBNAME_DW TO DEV_TEST;

2)把对某个表的操作的权限赋给另一个用户:

GRANT ALL ON TABLE TW_RE_PM_CELL_ALL TO DEV_TEST;

单独的某个权限:

GRANT SELECT ON STG.T_XXXX TO APP;

3)从某个用户收回对某个SCHEMA的使用权限:

REVOKE ALL ON SCHEMA DBNAME_DW FROM DEV_TEST;

4)从某个用户收回对某个表的使用权限:

REVOKE ALL ON TABLE FCT_FLUX_SE_FLUX FROM DEV_TEST;

九、修改字段数据类型

ALTER TABLE INCOME.BILL_DETAIL_201902 ALTER 字段 SET DATA TYPE VARCHAR(200);

对于数值类型:types–INTEGER, INT, BIGINT, TINYINT, INT8, SMALLINT, and all NUMERIC values of scale <=18 and precision 0 之间是可以互相转化的。此外,numeric类型的精度(precision)是无法更改的,但是长度(scale)是可以修改的,(0-18)之间可以互修改,(19-37)之间可以互修改。

修改表字段允许为空:

ALTER TABLE TABLE_NAME ALTER COLUMN PROV_NAME DROP NOT NULL;

十、查询PROJECTION

SELECT PROJECTION_NAME FROM PROJECTIONS WHERE OWNER_NAME = '用户名' AND ANCHOR_TABLE_NAME = '表名称' LIMIT 1;
----此处owner_name是表的创建者而非表属主

----推荐使用下面的写法

SELECT MIN(PROJECTION_SCHEMA || '.' || PROJECTION_NAME) PROJECTION_NAME FROM PROJECTIONS WHERE UPPER(PROJECTION_SCHEMA) = 'INCOME' AND ANCHOR_TABLE_NAME = 'BILL_DETAIL_201910';
-- PROJECTION_SCHEMA即表属主,表名是确定而且唯一的,不能用模糊查询

十一、给表添加注释

COMMENT ON TABLE INCOME.BILL_DEATIL_201801 IS '收入明细(宽表)_201801';

给表字段添加注释

SELECT MIN(PROJECTION_SCHEMA || '.' || PROJECTION_NAME) PROJECTION_NAME
FROM PROJECTIONS
WHERE UPPER(PROJECTION_SCHEMA) = 'INCOME'
AND ANCHOR_TABLE_NAME = 'BILL_DETAIL_201910';

----先用上面的语句查出PROJECTION_NAME,然后执行下面的语句

COMMENT ON COLUMN INCOME.BILL_DETAIL_b0.LATN_ID IS '地市ID';

【注意】:添加注释的表必须有数据才能添加注释成功,如果没有,则失败。

十二、查看表的信息(所有者、创建时间等)

SELECT * FROM TABLES WHERE TABLE_NAME like 'OFFER%';  -- ilike不区分大小写
SELECT TABLE_SCHEMA,TABLE_NAME,OWNER_NAME,CREATE_TIME FROM TABLES WHERE OWNER_NAME='hcxiesp';

同理,查看视图只需要把TABLES改为VIEWS即可

十三、查询用户和schema信息

SELECT u.user_name, s.schema_name FROM users u LEFT OUTER JOIN schemata s ON u.user_name = s.schema_owner;

(1)查询用户:select * from v_catalog.users;
(2)查询schema:select * from schemata;

十四、查询某表有哪些用户有哪些权限

SELECT GRANTOR,GRANTEE,OBJECT_SCHEMA,OBJECT_NAME,OBJECT_TYPE,PRIVILEGES_DESCRIPTION FROM GRANTS WHERE OBJECT_NAME ILIKE '%BILL_DETAIL_201911%';

十五、分组排序去重

Vertica没有rowid,参照Oracle的ROW_NUMBER () OVER (PARTITION BY … ORDER BY …)写法
–删除ASSET_INTEG_ID重复的记录

SELECT COUNT(*) FROM INCOME.SERV_INFO_YXZC_NEW_80;  --7694160条记录
SELECT COUNT(*) FROM (SELECT DISTINCT ASSET_INTEG_ID FROM  INCOME.SERV_INFO_YXZC_NEW_80) a;  --7694089条记录
CREATE TABLE INCOME.SERV_INFO_80_PST
AS
(
SELECT SERV_ID,
 ACC_NBR,
 AREA_ID,
 COMPLETED_DATE,
 STATE,
 STATE_DATE,
 S_CLAIM_DATE,
 CRM_SERV_NBR,
 LATN_ID,
 ASSET_INTEG_ID,
 L_CLAIM_ID,
 S_AREA_ID,
 S_SECOND_CLAIM_ID,
 LOCAL_AREA_ID,
 DUTY_AREA_ID,
 VIP_NBR
 FROM  ( SELECT SERV_ID,
 ACC_NBR,
 AREA_ID,
 COMPLETED_DATE,
 STATE,
 STATE_DATE,
 S_CLAIM_DATE,
 CRM_SERV_NBR,
 LATN_ID,
 ASSET_INTEG_ID,
 L_CLAIM_ID,
 S_AREA_ID,
 S_SECOND_CLAIM_ID,
 LOCAL_AREA_ID,
 DUTY_AREA_ID,
 VIP_NBR,
 ROW_NUMBER () OVER (PARTITION BY ASSET_INTEG_ID ORDER BY DUTY_AREA_ID DESC) = 1  AS  ROWNUMBER
           FROM INCOME.SERV_INFO_YXZC_NEW_80
       )T1
 WHERE ROWNUMBER = 1   
)
ORDER BY ASSET_INTEG_ID
SEGMENTED BY HASH (ASSET_INTEG_ID) ALL NODES KSAFE 1;
DROP TABLE INCOME.SERV_INFO_YXZC_NEW_80;
ALTER TABLE INCOME.SERV_INFO_80_PST RENAME TO SERV_INFO_YXZC_NEW_80;

十六、其他常用语法

1.关闭session
查询Vertica的所有session(普通用户只能看到自己的session)

SELECT * FROM sessions;

关闭session_id为 stress05-27944:0xc1a 的session

SELECT CLOSE_SESSION('stress05-27944:0xc1a');

关闭所有session(只有超级管理员有权限)

SELECT CLOSE_ALL_SESSIONS();

关闭当前用户的session

SELECT close_user_sessions('hcxiesp');

2.序列
(1)查询系统中的序列:select * from sequences;
(2)创建序列:
简单语法:create sequence etl_log_id increment by 1 start with 1 maxvalue 99999999;
标准语法:

CREATE SEQUENCE [[db-name.]schema.]sequence_name
[ INCREMENT [ BY ] positive_or_negative ]
[ MINVALUE minvalue | NO MINVALUE ]
[ MAXVALUE maxvalue | NO MAXVALUE ]
[ START [ WITH ] start ]
[ CACHE cache ]
[ CYCLE | NO CYCLE ]

(3)1、使用序列:

SELECT NEXTVAL('my_seq');
SELECT CURRVAL('my_seq');

注:一个新创建还没有使用过的序列,必须首先执行NEXTVAL,然后才能执行CURRVAL。
2、在INSERT语句里使用序列:

INSERT INTO customer VALUES ('Hawkins' ,'John', 072753, NEXTVAL('my_seq'));

3、在INSERT语句里把序列作为默认值:

CREATE TABLE customer2(ID INTEGER DEFAULT NEXTVAL('my_seq'),
lname VARCHAR(25),
fname VARCHAR(25),
membership_card INTEGER
);
=> INSERT INTO customer2 VALUES (default,'Carr', 'Mary', 87432);

(4)删除序列:DROP SEQUENCE seq_name;

3.Vertica创建外部表

CREATE EXTERNAL TABLE ext1 (x integer) AS COPY FROM '/tmp/ext1.dat' DELIMITER ',';
CREATE EXTERNAL TABLE ext1 (x integer) AS COPY FROM '/tmp/ext1.dat.bz2' BZIP DELIMITER ',';
CREATE EXTERNAL TABLE ext1 (x integer, y integer) AS COPY (x as '5', y) FROM '/tmp/ext1.dat.bz2' BZIP DELIMITER ',';

4.四合五入

SELECT TRIM (TO_CHAR (ROUND (3.456, 2.0), '999999999999999999.00')),
       TRIM (TO_CHAR (ROUND (3, 2.0), '999999999999999999.00')),
       TRIM (TO_CHAR (ROUND (3.00, 2.0), '999999999999999999.00')),
       TRIM (TO_CHAR (ROUND (323542.101, 2.0), '999999999999999999.00')),
       TRIM (TO_CHAR (ROUND (3.1067, 2.0), '999999999999999999.00'));

【注意】

SELECT TRIM (TO_CHAR (ROUND (34.565, 2.0), '99.00')); --99.00是格式,位数和待格式化数字的整数位一致,比如SELECT TRIM (TO_CHAR (ROUND (3.4565, 2.0), '9.00'));

5.创建表设置自增字段和默认系统时间

CREATE TABLE AA (
ID identity(1,1,1), --#或者直接identity(1)
XXX varchar(10),
ETL_AT TIMESTAMP DEFAULT NOW()
);

6.收集统计信息

SELECT ANALYZE_STATISTICS('APP_X.TEST');

十七、vertica数据库规范

https://blog.csdn.net/weixin_40197494/article/details/80568160

 类似资料: