How Do I Use an Engine to Maintain Tables?
Suggestion 1: Periodically Compressing Data Files
Reason: Too many small files will result in high metadata overhead and file opening costs, affecting query performance.
Method:
-- Default binpack policy (combining small files)
CALL spark_catalog.system.rewrite_data_files('db.table');
-- Sorting policy (sorting by specified columns)
CALL spark_catalog.system.rewrite_data_files(
table => 'db.table',
strategy => 'sort',
sort_order => 'id DESC NULLS LAST, name ASC NULLS FIRST'
);
-- Z-Order optimization (multi-column interleaved sorting to improve multi-column query performance)
CALL spark_catalog.system.rewrite_data_files(
table => 'db.table',
strategy => 'sort',
sort_order => 'zorder(c1,c2,c3)'
);
-- Enable partial progress and clean up dangling deletes.
CALL spark_catalog.system.rewrite_data_files(
table => 'db.table',
options => map(
'min-input-files', '2',
'remove-dangling-deletes', 'true',
'partial-progress.enabled', 'true'
)
); Key parameters:
- target-file-size-bytes: the target file size (512 MB by default)
- min-input-files: the minimum number of files that trigger rewriting (5 by default)
- rewrite-job-order: the sorting policy (bytes-asc/desc, files-asc/desc)
Aura implementation:
In Aura, the OPTIMIZE syntax is used to rewrite specified data in a target table.
OPTIMIZE table_name REWRITE DATA
[ WITH OPTIONS (option_key = option_value [, ...]) ]
[ WHERE condition ]
[ ORDER BY target_list [ ASC | DESC ] ] Parameters:
- table_name: the name of the target table. Enter an existing table name.
- condition: an expression that returns a boolean value, used to determine which files need to be rewritten.
- target_list: sort keys for data rewriting.
- option_key, option_value: configuration options for data rewriting.
Suggestion 2: Periodically Rewriting Position Delete Files
Reason: Compress small position delete files and clear dangling deletes (delete records that point to non-existent data files).
Method:
-- Compress and clear dangling deletes.
CALL spark_catalog.system.rewrite_position_delete_files('db.table');
-- Forcibly rewrite all files.
CALL spark_catalog.system.rewrite_position_delete_files(
table => 'db.table',
options => map('rewrite-all', 'true')
); Suggestion 3: Periodically Clearing Expired Snapshots
Reason: Each write, update, deletion, upsert, or compression operation generates a new snapshot. Failure to clear these snapshots will cause continuous storage consumption.
Method:
-- Delete snapshots that have been stored for more than seven days and retain the latest 100 snapshots. CALL spark_catalog.system.expire_snapshots( table => 'db.table', older_than => CURRENT_TIMESTAMP - INTERVAL '7' DAYS, retain_last => 100, stream_results => true -- Prevent driver OOM caused by a large amount of data. -- Delete specific snapshots. CALL spark_catalog.system.expire_snapshots( table => 'db.table', snapshot_ids => ARRAY(123) ); -- Delete unused metadata during the cleanup. CALL spark_catalog.system.expire_snapshots( table => 'db.table', older_than => TIMESTAMP '2024-01-01 00:00:00', clean_expired_metadata => true );
Notes:
- By default, branches and tags never expire. To configure their validity periods, set the table property history.expire.max-ref-age-ms.
- Set stream_results to true to prevent driver OOM when there is a large amount of data.
Aura implementation: Refer to the iceberg_expire_snapshots table service. For details about the syntax, see Iceberg Table Service Functions.
Suggestion 4: Periodically Deleting Orphaned Files
Reason: Delete orphaned files that are no longer referenced by any metadata file to free up storage space.
Method:
-- Check the files to be cleared in the trial run. (You are advised to perform this operation in the production environment first.) CALL spark_catalog.system.remove_orphan_files( table => 'db.table', dry_run => true ); -- Perform the actual cleanup (delete orphaned files that have been stored for more than seven days). CALL spark_catalog.system.remove_orphan_files( table => 'db.table', older_than => CURRENT_TIMESTAMP - INTERVAL '7' DAYS ); -- Delete orphaned files from a specific directory. CALL spark_catalog.system.remove_orphan_files( table => 'db.table', location => 'table_location/data' );
Aura implementation: Refer to the iceberg_remove_orphanfiles table service. For details about the syntax, see Iceberg Table Service Functions.
Suggestion 5: Periodically Rewriting Manifest Files
Reason: Sort data files in the manifest by partition spec fields to optimize the query planning performance.
Method:
-- Rewrite the manifest.
CALL spark_catalog.system.rewrite_manifests('db.table');
-- Specify the partition spec.
CALL spark_catalog.system.rewrite_manifests(
table => 'db.table',
spec_id => 1
); Aura implementation: Refer to the iceberg_rewrite_manifests table service. For details about the syntax, see Iceberg Table Service Functions.
Suggestion 6: Creating Tags for Important Versions
Reason: Facilitate quick rollback to a specific version to avoid irrecoverable issues caused by misoperations.
Method:
-- Switch to a specified snapshot.
CALL spark_catalog.system.set_current_snapshot(
table => 'db.table',
snapshot_id => 123
);
-- Switch to a specified tag.
CALL spark_catalog.system.set_current_snapshot(
table => 'db.table',
ref => 'my_tag'
);
-- Roll back to a specified snapshot.
CALL spark_catalog.system.rollback_to_snapshot('db.table', 1);
-- Roll back by time.
CALL spark_catalog.system.rollback_to_timestamp(
'db.table',
TIMESTAMP '2024-06-30 00:00:00.000'
); Aura implementation: Refer to the iceberg_set_current_snapshot, iceberg_rollback_to_snapshot and iceberg_rollback_to_timestamp table services. For details about the syntax, see Iceberg Table Service Functions.
Suggestion 7: Using a Snapshot Table For Testing Before Migration
Reason: Creating a lightweight temporary copy for testing does not affect the source table and helps avoid migration risks.
Method:
-- Create a snapshot table.
CALL spark_catalog.system.snapshot('db.source_table', 'db.snapshot_table'); Note: The snapshot table is not the sole owner of data files and cannot physically delete files using expire_snapshots.
Suggestion 8: Performing Formal Migration Using migrate
Reason: Replace existing Hive/Spark tables with Iceberg tables and retain the original table as a backup.
Method:
-- Migrate and add properties.
CALL spark_catalog.system.migrate(
'db.old_table',
map('foo', 'bar')
);
-- Do not retain backups.
CALL spark_catalog.system.migrate(
table => 'db.old_table',
drop_backup => true
); Suggestion 9: Calculating Table Statistics
Reason: Collect the NDV statistics of columns to optimize the query plan.
Method:
-- Calculate statistics of the entire table.
CALL spark_catalog.system.compute_table_stats('db.table');
-- Specify the snapshot and column.
CALL spark_catalog.system.compute_table_stats(
table => 'db.table',
snapshot_id => 'snap1',
columns => array('col1', 'col2')
); Suggestion 10: Calculating Partition Statistics
Reason: Incrementally collect partition statistics to improve the partition pruning efficiency.
Method:
-- Calculate partition statistics.
CALL spark_catalog.system.compute_partition_stats('db.table');
-- Specify a snapshot.
CALL spark_catalog.system.compute_partition_stats(
table => 'db.table',
snapshot_id => 'snap1'
); What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot