OPTIMIZE
功能描述
对目标表的指定数据进行重写。
目前仅Iceberg表支持该语法。
注意事项
- 如果用户指定了排序键,数据重写后,文件内数据有序,而无法保证全局有序。
- 建议选择重写条件时,按照分区维度编辑重写条件。
语法格式
OPTIMIZE table_name REWRITE DATA
[ WITH OPTIONS (option_key = option_value [, ...]) ]
[ WHERE condition ]
[ ORDER BY target_list [ ASC | DESC ] ] 参数说明
- table_name:目标表的名字。取值范围:已存在的表名。
- condition:一个返回boolean值的表达式,用于判断哪些文件需要被重写。
- target_list:数据重写的排序键。
- option_key、option_value:数据重写的配置选项。
| 参数 | 取值 | 说明 | 默认值 |
|---|---|---|---|
| target-file-size-bytes | 0~Max(uint64) | 重写后的文件大小。 | 与表参数write.target-file-size-bytes保持一致 |
| min-file-size-bytes | 0~Max(uint64) | 原始文件大小小于该阈值的文件会被当做目标文件。 | 75% * target-file-size-bytes |
| max-file-size-bytes | 0~Max(uint64) | 原始文件大小大于该阈值的文件会被当做目标文件。 | 180% * target-file-size-bytes |
| min-input-files | 0~Max(uint32) | 当单个分区内目标文件个数大于该值时,将会对该分区进行数据重写。 | 5 |
| delete-file-threshold | 0~Max(uint32) | 当单个分区内delete文件个数大于该值时,将会对该分区进行数据重写。 | INT_MAX |
| rewrite-all | true、false | 忽视以上所有条件,对所有满足用户条件的文件进行重写。 | false |
| max-file-group-size-bytes | 0~Max(uint64) | 分区内目标文件总大小大于该阈值时,将该分区的目标文件拆分为多个task进行处理。 | 100GB |
| output-spec-id | 0~Max(int32) | 指定数据重写时使用的分区键定义ID | 若不指定,使用当前最新表定义对应的分区键ID。 |
| use-starting-sequence-number | true、false | 使用数据重写开始时快照的序列号。 | true |
| remove_dangling_deletes | true、false | 在重写后删除悬空删除文件。如果删除文件不适用于任何活跃数据文件,则该删除文件被视为悬空。启用此功能将生成一个额外的提交。 | false |
示例
CREATE TABLE reason_t2
(
TABLE_SK INTEGER ,
TABLE_ID VARCHAR(20) ,
TABLE_NA VARCHAR(20)
) PARTITION BY (TABLE_SC VARCHAR(20)) STORE AS iceberg;
INSERT INTO reason_t2 VALUES (1, 'S01', 'StudentA', 'Xiangbei'),(2, 'T01', 'TeacherA', 'Lingnan'),(3, 'T02', 'TeacherB', 'Hainan');
INSERT INTO reason_t2 VALUES (4, 'S02', 'StudentX', 'Xiangbei'),(5, 'T03', 'TeacherX', 'Lingnan'),(6, 'T04', 'TeacherY', 'Hainan'); OPTIMIZE reason_t2 REWRITE DATA WITH OPTIONS ('rewrite-all' = 'true') WHERE TABLE_SC = 'Hainan'; 使用ORDER BY语句查看TABLE_SC = 'Lingnan'分区数据重写计划:
EXPLAIN VERBOSE OPTIMIZE reason_t2 REWRITE DATA WITH OPTIONS ('rewrite-all' = 'true') WHERE TABLE_SC = 'Lingnan' ORDER BY 1;
QUERY EXEC INFO
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
id | operation | E-rows | E-distinct | E-width | E-costs
----+-------------------------------------------------------------------------+--------+------------+---------+---------
1 | -> Row Adapter | 1000 | | 178 | 73.68
2 | -> Vector Insert on reason_t2 | 1000 | | 178 | 73.68
3 | -> Vector Sort | 1000 | | 178 | 53.68
4 | -> Partitioned Vector Foreign Scan on reason_t2 | 1000 | | 178 | 1.35
Predicate Information (identified by plan id)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
4 --Partitioned Vector Foreign Scan on reason_t2
Server Type: lf
Pruning results: (Manifests total: 3, Manifests left: 2, Files total: 4, Files dynamic prune 0, Files dynamic RF prune: 0, Files left after pruning: 2)
DN read from: direct
replication: 2 files 0.00 MB 设置min-file-size-bytes以重写全部目标文件:
EXPLAIN VERBOSE OPTIMIZE reason_t2 REWRITE DATA WITH OPTIONS ('min-file-size-bytes' = '0') ORDER BY 1; 通过设置output-spec-id来指定写入分区的示例如下:
ALTER TABLE reason_t2 ADD PARTITION COLUMN bucket(5, table_id) ;
select * from iceberg_partition_specs('reason_t2');
spec_id | field_id | name | transform | source_id
---------+----------+-------------------+-----------+-----------
0 | 1000 | table_sc | identity | 4
1 | 1000 | table_sc | identity | 4
1 | 1001 | table_id_bucket_5 | bucket[5] | 2
(3 rows)
OPTIMIZE reason_t2 REWRITE DATA WITH OPTIONS ('output-spec-id'='0') ORDER BY 1;