Lesson 4 of 13 Amazon Redshift

Querying Lake Data from Redshift with Redshift Spectrum

This lesson explains how Redshift Spectrum queries data in Amazon S3 directly, so recent data can live in Redshift and years of history in the lake, joined in one query.

Learning outcomes

After completing this lesson, you will be able to:

  • Create external schemas and tables for Spectrum.
  • Keep Spectrum fast and cheap with data layout.
  • Apply hot and cold designs with Spectrum.

How Spectrum works

Redshift Spectrum lets SQL in Redshift query data that stays in Amazon S3. An external schema in Redshift points to a database in the AWS Glue Data Catalog, and its tables, called external tables, describe files in S3. They can be queried, and joined with local Redshift tables, in ordinary SQL.

When a query touches an external table, Redshift sends that part of the work to a separate, shared fleet of Spectrum nodes, which scan the files in S3, apply filters and aggregations, and return the results to the warehouse for the rest of the query. Because this fleet scales independently, scanning large amounts of lake data does not consume the warehouse's own compute. On provisioned clusters, Spectrum is charged per terabyte of data scanned; on Redshift Serverless, it is included in the RPU usage.

The following diagram shows a view that combines recent orders in Redshift with older orders in S3, and the two places its rows come from.

The all_orders view over hot and cold data. The last 13 months of orders stay in a Redshift table. Older orders were unloaded to S3 as Parquet partitioned by date and are read through an external schema by the Spectrum fleet, which scans and filters in S3. A late-binding view combines both with UNION ALL.
The view must be late-binding because it references an external table.

Keeping Spectrum fast and cheap

Spectrum's speed and cost depend on how much data it must read from S3, so the same layout practices that help Athena help Spectrum. Data should be stored in Parquet or ORC, so that only the columns a query uses are read, and partitioned by the columns most often used in filters, usually dates, so that whole folders are skipped.

File size matters too: files of 128 MB or more are read efficiently, while many small files add overhead for each one opened. Finally, queries should filter and aggregate on external tables as early as possible, because Spectrum performs those steps in its own fleet and returns only the reduced result to the warehouse, whereas returning raw rows moves far more data.

Select all that apply

Faster Spectrum queries

Exam skills 2.1.5 · 2.4.5

Identify practices that make Spectrum queries faster and cheaper.

Log in to try this activity Interactive activities are free with an account.

Typical uses

Redshift Spectrum queries data in S3 through external tables, and three designs use it. In a hot and cold design, recent data that is queried often is kept in Redshift tables for speed, older history stays cheaply in S3, and a view combines the two so users query one object. Spectrum also lets Redshift query the lake without loading it, through external tables over curated Parquet or Iceberg data. Because those tables are defined in the Glue Data Catalog, the same data can be queried by Redshift, Athena and EMR without copies. The following table summarises the designs.

UseDesign
Hot and cold dataRecent data in Redshift tables, history in S3, combined with a view
Querying the lake without loadingExternal tables over curated Parquet or Iceberg data
Shared dataThe same S3 tables queried by Redshift, Athena and EMR through the Data Catalog

Views that reference external tables must be late-binding views (WITH NO SCHEMA BINDING). Lake Formation permissions apply to Spectrum queries.

Scenario questions

Using Spectrum

Exam skills 2.1.5

Choose the Spectrum design for each requirement.

Log in to try this activity Interactive activities are free with an account.
Worked example

Seven years of history without loading it

A warehouse keeps the last 13 months of orders in Redshift. Auditors occasionally need seven years. Older data is unloaded to S3 as partitioned Parquet and exposed through Spectrum, and a late-binding view combines both, as follows.

CREATE EXTERNAL SCHEMA lake FROM DATA CATALOG DATABASE 'curated'
IAM_ROLE 'arn:aws:iam::111111111111:role/RedshiftSpectrum';

CREATE VIEW all_orders AS
SELECT order_id, order_date, amount FROM public.orders
UNION ALL
SELECT order_id, order_date, amount FROM lake.orders_history
WITH NO SCHEMA BINDING;

Queries on recent data never touch S3, and audit queries filtering on order_date read only the relevant partitions. Storage for six years of history moves from the warehouse to S3 at a fraction of the cost.

Check yourself

What is needed to query S3 data from Redshift with Spectrum?

Show answer

An external schema pointing to the data's tables in the Data Catalog, and an IAM role that can read the data.

Exam Tips

Remember for the exam

  • To query S3 data without loading it, use Redshift Spectrum with an external schema.
  • Keep hot data in Redshift and history in S3, joined through Spectrum.
  • Partitioned Parquet keeps Spectrum queries fast and cheap.
  • Views over external tables must be late-binding.
  • Spectrum reads table definitions from the Glue Data Catalog.