Lakehouse Table Cloning and Fast Backup Guide

Overview

In data warehouse operations, you often need to quickly create table copies for testing, backup, or data recovery. Singdata Lakehouse provides the CREATE TABLE ... CLONE syntax, supporting zero-copy cloning that instantly creates table replicas without copying the actual data. This guide categorizes by business scenario to help you quickly master efficient table cloning methods.


SQL Commands Covered

CommandPurposeUse Case
CREATE TABLE ... CLONEZero-copy table cloningFast backup, test environment setup
CREATE TABLE ... CLONE ... ATPoint-in-time cloneRestore to a historical state
CREATE TABLE ... LIKEClone table structure onlyCreate an empty table for data loading

Prerequisites

The following examples use a simulated orders table orders_clone:

-- Create source table
CREATE TABLE IF NOT EXISTS orders_clone (
    order_id INT,
    customer_id INT,
    amount DOUBLE,
    order_date DATE
);

-- Insert test data
INSERT INTO orders_clone VALUES
(1, 101, 500, '2024-06-01'),
(2, 102, 300, '2024-06-02'),
(3, 103, 800, '2024-06-03');

Full Table Clone

Use CREATE TABLE ... CLONE to instantly create a replica of the table. The cloned table shares underlying data files with the source, consuming no additional storage space (until you perform write operations on the cloned table).

-- Clone the table
CREATE TABLE orders_clone_backup CLONE orders_clone;

Result Verification:

SELECT * FROM orders_clone_backup ORDER BY order_id;
order_idcustomer_idamountorder_date
11015002024-06-01
21023002024-06-02
31038002024-06-03

Point-in-time Clone

Use the AT clause to clone the table state at a specific point in time, commonly used to restore to a historical version.

-- Clone the table state from 1 minute ago
CREATE TABLE orders_clone_history CLONE orders_clone 
TIMESTAMP AS OF (CURRENT_TIMESTAMP() - INTERVAL '1' MINUTE);

Use Cases:

  • Restore to a previous state after erroneous operations
  • Audit historical data
  • Compare data changes

Clone Structure Only

Use the LIKE syntax to clone the table structure without copying data, suitable for creating empty tables for subsequent data loading.

-- Clone table structure only
CREATE TABLE orders_clone_empty LIKE orders_clone;

Result Verification:

SELECT COUNT(*) FROM orders_clone_empty;
COUNT(*)
0

Post-clone Verification

After cloning, it is recommended to verify consistency between the cloned table and the source table.

-- Compare row counts
SELECT 
    (SELECT COUNT(*) FROM orders_clone) as source_count,
    (SELECT COUNT(*) FROM orders_clone_backup) as clone_count;

Result Explanation:

source_countclone_count
33

Clone and Independent Modification

Once created, the cloned table is independent -- modifications to the cloned table will not affect the source table.

-- Insert new data into the cloned table
INSERT INTO orders_clone_backup VALUES (4, 104, 600, '2024-06-04');

-- Compare source and cloned tables
SELECT 'source' as tbl, COUNT(*) as cnt FROM orders_clone
UNION ALL
SELECT 'clone' as tbl, COUNT(*) as cnt FROM orders_clone_backup;

Result Explanation:

tblcnt
source3
clone4

Clean Up Test Data

After completing clone verification, it is recommended to clean up test tables:

-- Drop test tables
DROP TABLE IF EXISTS orders_clone;
DROP TABLE IF EXISTS orders_clone_backup;
DROP TABLE IF EXISTS orders_clone_history;
DROP TABLE IF EXISTS orders_clone_empty;

Notes

  1. Zero-copy Property: Cloned tables share data files with the source, consuming no additional storage space (until new data is written).
  2. Time Travel Dependency: Point-in-time cloning depends on Time Travel functionality and must be within the retention period.
  3. Permission Inheritance: Cloned tables do not automatically inherit source table permissions; separate configuration is required.
  4. Dynamic Table Cloning: Dynamic Tables can also be cloned, but the clone becomes a regular table and no longer auto-refreshes.
  5. Materialized View Cloning: Materialized views also become regular tables after cloning.