Databricks DE ยท Topic 3.4

Deep vs Shallow CLONE & Predictive Optimization

Deep clone = independent copy (data + metadata), incremental on re-run. Shallow clone = instant pointer to the source's files. Predictive Optimization auto-runs maintenance for you. This deep dive walks each with the code.

DEEP CLONESHALLOW CLONEincrementalPredictive Optimization

The one-liner

DEEP CLONE copies data + metadata โ†’ independent table (re-runs sync only the delta). SHALLOW CLONE copies only metadata and references the source's files โ†’ instant & cheap but dependent. Predictive Optimization auto-runs OPTIMIZE/VACUUM/ANALYZE on UC managed tables.
๐Ÿ“š Analogy: deep clone = photocopying a whole book (your own copy); shallow clone = sticky-notes referencing the original (instant, but useless if the original is thrown out); PO = a robot librarian that reshelves & declutters automatically.

Clone types โ€” and the VACUUM trap

โ–ธ pick a clone type, then run VACUUM on the source

DEEP CLONE
SHALLOW CLONE
Source table
data files: present
Clone
owns its own data

1 ยท The two clone types

Both copy schema, partitioning, invariants, nullability, and TBLPROPERTIES (deep also copies stream/COPY INTO metadata). Neither copies history, description, or UC tags. CLONE defaults to deep.

SQL โ€” deep, shallow, versioned

CREATE TABLE main.dr.orders_backup DEEP CLONE main.sales.orders;     -- independent copy
CREATE TABLE main.dev.orders_dev SHALLOW CLONE main.sales.orders;     -- instant, points at source
CREATE TABLE main.dev.orders_v5 SHALLOW CLONE main.sales.orders VERSION AS OF 5;

PySpark โ€” CLONE is SQL DDL, run via spark.sql

spark.sql("CREATE OR REPLACE TABLE main.dr.orders_backup DEEP CLONE main.sales.orders")

2 ยท Deep clone is INCREMENTAL (the backup superpower)

Re-running a deep clone into an existing target commits only the new/changed data since the last clone โ€” cost scales with the change, not table size. That's why it's the go-to for scheduled backups, DR, and cross-catalog/region migration.

-- Day 1: full copy. Day 2..N: same command re-syncs ONLY the delta since last run.
CREATE OR REPLACE TABLE main.dr.orders_backup DEEP CLONE main.sales.orders;

3 ยท Shallow clone & the VACUUM trap

Shallow clone borrows the source's data files, so it's instant โ€” but two consequences interviewers probe:

CREATE TABLE main.dev.orders_dev SHALLOW CLONE main.sales.orders;
-- ...later, on the SOURCE:
VACUUM main.sales.orders;     -- โš ๏ธ removes files orders_dev references โ†’ clone breaks
๐Ÿ’ก For a durable copy that survives source maintenance, use DEEP CLONE. Also note: streaming tables & materialized views can't be a clone source or target.

4 ยท Predictive Optimization โ€” click each

What it doesโ–ธ
Automatically runs maintenance โ€” OPTIMIZE (incl. incremental clustering), VACUUM, and ANALYZE โ€” on Unity Catalog managed tables based on usage. No manual scheduling.
Why it mattersโ–ธ
Removes the "did you schedule OPTIMIZE/VACUUM?" toil and avoids both under- and over-maintenance. Modern answer to "how do you keep tables fast?"
Scope & defaultโ–ธ
Applies to UC managed tables (external you maintain yourself). Enabled by default for accounts created on/after Nov 11, 2024; older accounts via gradual rollout.

Enable at catalog or schema level (inherits down)

ALTER CATALOG main ENABLE PREDICTIVE OPTIMIZATION;
ALTER SCHEMA  main.sales ENABLE PREDICTIVE OPTIMIZATION;   -- or DISABLE / INHERIT

Deep vs shallow โ€” at a glance

Deep cloneShallow clone
Copies data filesYesNo (points at source)
Speed / costSlower / more storageInstant / cheap
Independent of sourceYesNo (VACUUM on source breaks it)
Incremental re-syncYesn/a
Good forBackup, archive, DR, migrationTest/dev, sharing

Why it matters

๐Ÿงช Real-world use case: before a risky migration, SHALLOW CLONE prod.orders to a sandbox in seconds, experiment, then drop it โ€” zero data copied. For DR, a nightly DEEP CLONE re-syncs only the day's changes.

Uses, edge cases & limitations

Common gotchas