
# OPTIMIZE
#### 功能描述
对目标表的指定数据进行重写。
![](https://support.huaweicloud.com/devg-fabric/public_sys-resources/note_3.0-zh-cn.png)
目前仅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：数据重写的配置选项。
表1option_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                                |
   
#### 示例
创建表reason_t2：
```
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');
```
使用WHERE条件对TABLE_SC = '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;
```
