PG表数据truncate

43 阅读4分钟

一、背景

清空以下表数据:
aoi_daypart_flow_info_202303
aoi_daypart_flow_info_202304
aoi_daypart_flow_info_202307
aoi_daypart_flow_info_202309
aoi_daypart_flow_info_202310
aoi_daypart_flow_info_202311
aoi_daypart_flow_info_202312

aoi_daypart_order_kfc_202301
aoi_daypart_order_kfc_202302
aoi_daypart_order_kfc_202303
aoi_daypart_order_kfc_202304
aoi_daypart_order_kfc_202305
aoi_daypart_order_kfc_202306
aoi_daypart_order_kfc_202307
aoi_daypart_order_kfc_202308
aoi_daypart_order_kfc_202309
aoi_daypart_order_kfc_202310

aoi_daypart_order_ph_202301
aoi_daypart_order_ph_202302
aoi_daypart_order_ph_202303
aoi_daypart_order_ph_202304
aoi_daypart_order_ph_202305
aoi_daypart_order_ph_202306
aoi_daypart_order_ph_202307
aoi_daypart_order_ph_202308
aoi_daypart_order_ph_202309
aoi_daypart_order_ph_202310

aoi_daypart_order_yumc_202301
aoi_daypart_order_yumc_202302
aoi_daypart_order_yumc_202303
aoi_daypart_order_yumc_202304
aoi_daypart_order_yumc_202305

对以上表进行truncate操作




二、操作

truncate table aoi_daypart_flow_info_202303;
truncate table aoi_daypart_flow_info_202304;
truncate table aoi_daypart_flow_info_202307;
truncate table aoi_daypart_flow_info_202309;
truncate table aoi_daypart_flow_info_202310;
truncate table aoi_daypart_flow_info_202311;
truncate table aoi_daypart_flow_info_202312;


truncate table aoi_daypart_order_kfc_202301;
truncate table aoi_daypart_order_kfc_202302;
truncate table aoi_daypart_order_kfc_202303;
truncate table aoi_daypart_order_kfc_202304;
truncate table aoi_daypart_order_kfc_202305;
truncate table aoi_daypart_order_kfc_202306;
truncate table aoi_daypart_order_kfc_202307;
truncate table aoi_daypart_order_kfc_202308;
truncate table aoi_daypart_order_kfc_202309;
truncate table aoi_daypart_order_kfc_202310;

2. ## 表

# 
alter table aoi_daypart_order_ph_202301 DISABLE TRIGGER ALL;
alter table aoi_daypart_order_ph_202302 DISABLE TRIGGER ALL;
alter table aoi_daypart_order_ph_202303 DISABLE TRIGGER ALL;
alter table aoi_daypart_order_ph_202304 DISABLE TRIGGER ALL;
alter table aoi_daypart_order_ph_202305 DISABLE TRIGGER ALL;
alter table aoi_daypart_order_ph_202306 DISABLE TRIGGER ALL;
alter table aoi_daypart_order_ph_202307 DISABLE TRIGGER ALL;
alter table aoi_daypart_order_ph_202308 DISABLE TRIGGER ALL;
alter table aoi_daypart_order_ph_202309 DISABLE TRIGGER ALL;
alter table aoi_daypart_order_ph_202310 DISABLE TRIGGER ALL;

# 
TRUNCATE TABLE aoi_daypart_order_ph_202301;
TRUNCATE TABLE aoi_daypart_order_ph_202302;
TRUNCATE TABLE aoi_daypart_order_ph_202303;
TRUNCATE TABLE aoi_daypart_order_ph_202304;
TRUNCATE TABLE aoi_daypart_order_ph_202305;
TRUNCATE TABLE aoi_daypart_order_ph_202306;
TRUNCATE TABLE aoi_daypart_order_ph_202307;
TRUNCATE TABLE aoi_daypart_order_ph_202308;
TRUNCATE TABLE aoi_daypart_order_ph_202309;
TRUNCATE TABLE aoi_daypart_order_ph_202310;

# 
alter table aoi_daypart_order_ph_202301 ENABLE TRIGGER ALL;
alter table aoi_daypart_order_ph_202302 ENABLE TRIGGER ALL;
alter table aoi_daypart_order_ph_202303 ENABLE TRIGGER ALL;
alter table aoi_daypart_order_ph_202304 ENABLE TRIGGER ALL;
alter table aoi_daypart_order_ph_202305 ENABLE TRIGGER ALL;
alter table aoi_daypart_order_ph_202306 ENABLE TRIGGER ALL;
alter table aoi_daypart_order_ph_202307 ENABLE TRIGGER ALL;
alter table aoi_daypart_order_ph_202308 ENABLE TRIGGER ALL;
alter table aoi_daypart_order_ph_202309 ENABLE TRIGGER ALL;
alter table aoi_daypart_order_ph_202310 ENABLE TRIGGER ALL;

3. ## 表

alter table aoi_daypart_order_yumc_202301 DISABLE TRIGGER ALL;
alter table aoi_daypart_order_yumc_202302 DISABLE TRIGGER ALL;
alter table aoi_daypart_order_yumc_202303 DISABLE TRIGGER ALL;
alter table aoi_daypart_order_yumc_202304 DISABLE TRIGGER ALL;
alter table aoi_daypart_order_yumc_202305 DISABLE TRIGGER ALL;


truncate table aoi_daypart_order_yumc_202301;
truncate table aoi_daypart_order_yumc_202302;
truncate table aoi_daypart_order_yumc_202303;
truncate table aoi_daypart_order_yumc_202304;
truncate table aoi_daypart_order_yumc_202305;


alter table aoi_daypart_order_yumc_202301 ENABLE TRIGGER ALL;
alter table aoi_daypart_order_yumc_202302 ENABLE TRIGGER ALL;
alter table aoi_daypart_order_yumc_202303 ENABLE TRIGGER ALL;
alter table aoi_daypart_order_yumc_202304 ENABLE TRIGGER ALL;
alter table aoi_daypart_order_yumc_202305 ENABLE TRIGGER ALL;

4. ## 表(1100w+行的数据)


-- 禁用外键约束
ALTER TABLE aoi_daypart_order_kfc_202308 DISABLE TRIGGER ALL;

-- 清空表数据
TRUNCATE TABLE aoi_daypart_order_kfc_202308;

-- 启用外键约束
ALTER TABLE aoi_daypart_order_kfc_202308 ENABLE TRIGGER ALL;


select count(1) from aoi_daypart_order_kfc_202308;



三、补充

-- 禁用外键约束
ALTER TABLE aoi_daypart_order_kfc_202310 DISABLE TRIGGER ALL;

-- 清空表数据
TRUNCATE TABLE aoi_daypart_order_kfc_202310;

-- 启用外键约束
ALTER TABLE aoi_daypart_order_kfc_202310 ENABLE TRIGGER ALL;