Sub topic 12
Choosing between COPY INTO and Auto Loader
This chapter assumes you can write a basic COPY INTO statement, know that COPY INTO skips files it has already loaded, and understand Delta tables and Unity Catalog volumes. Here you will compare COPY INTO with Auto Loader and learn how to pick one for a workload.
Two tools for the same job
COPY INTO and Auto Loader both load new files from cloud storage into a Delta table. They differ in how they find new files and how they run.
- COPY INTO is a SQL command. Each run lists the source location, compares the files with the ones it has already loaded, and loads the new ones. When the command finishes, nothing keeps running.
- Auto Loader is a Structured Streaming source called
cloudFiles. It records discovered files in a checkpoint and can discover them through file notifications instead of listing the directory each time. It can run continuously, or as a scheduled job that processes what is available and stops.
How they differ
| Aspect | COPY INTO | Auto Loader |
|---|---|---|
| Interface | SQL statement | Python or Scala streaming code, or SQL streaming tables |
| File discovery | Lists the source path on every run | Directory listing or file notifications |
| Tracking state | Stored with the target table | Stored in a checkpoint |
| Best file volume | Thousands of files per run | Millions of files and more |
| Schema handling | `inferSchema` and `mergeSchema` | Schema inference, schema location, and rescued data column |
| Latency | Minutes to hours, depending on your schedule | Seconds to minutes |
Both tools skip files they have already processed. The difference is where they record that, and how much work they do to find new files.
Choosing
Use COPY INTO when:
- The source receives thousands of files, not millions.
- A scheduled batch is fast enough.
- You want one SQL statement with no streaming code or checkpoint to manage.
- You want to preview the load with
VALIDATEbefore you write any data.
Use Auto Loader when:
- Files arrive in very large numbers, so listing the whole directory on every run becomes slow.
- You need continuous or near-real-time ingestion.
- The source schema changes often and you want a rescued data column to capture values that do not fit.
- You expect the volume to grow and want to avoid a migration later.
The same load in both tools
This COPY INTO statement loads CSV files from a volume.
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
FORMAT_OPTIONS ('header' = 'true');This Auto Loader version in Python does the same job as a job that stops when it has caught up.
(spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "csv")
.option("header", "true")
.option("cloudFiles.schemaLocation", "/Volumes/main/sales/landing/_schemas/orders")
.load("/Volumes/main/sales/landing/orders/")
.writeStream
.option("checkpointLocation", "/Volumes/main/sales/landing/_checkpoints/orders")
.trigger(availableNow=True)
.toTable("main.sales.orders"))The Auto Loader version needs two extra storage locations, one for the schema and one for the checkpoint. That is the cost of its added capabilities.
Common mistakes
- Choosing Auto Loader for a small, daily batch because it sounds more modern. The extra checkpoint and schema paths add work with no benefit.
- Staying with COPY INTO as file counts climb. When runs get slower because of listing time, that is the sign to move.
- Expecting COPY INTO to react to new files. It only runs when you start it, so a schedule or a job trigger must do that.
Exercise
A team receives about 200 CSV files each night. A report that reads the table runs at 7 a.m. The team knows SQL well and has no streaming experience. Which tool fits, and what would make you change the answer?
Show answer
COPY INTO fits. The volume is small, a nightly batch meets the 7 a.m. deadline, and it needs only a SQL statement on a schedule. Change the answer if the file count grows to a very large number so that listing slows each run, if the report needs data within minutes of arrival, or if the schema changes often and you need a rescued data column. In those cases, move to Auto Loader.