Lesson 2 of 13 Amazon Redshift

Designing Redshift Tables with Distribution Styles and Sort Keys

This lesson explains how distribution styles, sort keys, compression and the schema decide where a table's rows are placed across nodes and how they are ordered on disk, which strongly affects query speed.

Learning outcomes

After completing this lesson, you will be able to:

  • Choose a distribution style for each table.
  • Choose sort keys and compression encodings.
  • Design star schemas for analytical queries.
  • Maintain tables with VACUUM and ANALYZE.

Distribution styles

Redshift spreads each table's rows across slices on its compute nodes, and the distribution style decides how. It matters most for joins: if matching rows of two tables are on different nodes, Redshift must move data across the network at query time. KEY distribution places rows with the same value of a chosen column on the same slice, so two large tables distributed on their join column join locally, called co-located. ALL places a full copy of the table on every node, so any join to it is local. That suits small dimension tables that change slowly, since every change is written to every node.

EVEN spreads rows round-robin, which balances storage for tables that are not joined on a particular key. AUTO, the default, lets Redshift choose and adjust the style as the table grows. The following table summarises the styles.

StyleHow rows are spreadUse for
AUTORedshift chooses and adjustsWhen unsure
EVENRound-robinTables not joined on a key
KEYSame key value on the same slice, so joins are co-locatedLarge tables joined on that column
ALLA full copy on every nodeSmall, slowly changing dimension tables

The following diagram shows where the rows of a sales schema sit on two compute nodes after the distribution styles are chosen to keep joins local.

The chosen distribution. Sales and customers are both distributed on customer_id, so each customer's sales rows sit on the same node as the customer row and the join runs locally. The small stores table uses ALL, so every node has a full copy. Before the change, the plan showed DS_BCAST_INNER and DS_DIST_BOTH steps moving data for every join.
Slices within each node are not shown.

Sort keys and encodings

A sort key determines the order in which a table's rows are stored on disk. When a query filters on the sort key column, for example a date range, the zone maps of sorted blocks have narrow, non-overlapping ranges, so Redshift skips most blocks without reading them. Sort keys are therefore usually placed on the columns used most often in range filters, typically dates. A compound sort key sorts by several columns in order and helps most when queries filter on the leading column; AUTO lets Redshift choose and adjust sort keys from the workload.

Compression encodings reduce the storage each column uses and therefore the I/O each query needs. Redshift chooses encodings automatically with ENCODE AUTO; common choices are AZ64 for numbers and dates and ZSTD or LZO for text. For semi-structured data, the SUPER data type stores JSON documents, which are queried with PartiQL, a SQL extension that navigates nested attributes and arrays, so JSON can be loaded without first flattening it into columns.

The following diagram shows how a sort key makes a Redshift table's zone maps selective.

Zone maps with and without a sort key. Each block records the minimum and maximum sale_date it holds. Unsorted, every block's range overlaps the last 30 days, so every block is read. Sorted on sale_date, the ranges are narrow and only the newest block can match, so the others are skipped.
Block ranges are illustrative.
True or false

Sort keys and encodings

Exam skills 2.4.1 · 2.4.5

Decide whether each statement about Redshift table design is true.

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

Schema design

Redshift schemas are usually dimensional, because that design suits analytical queries. In a star schema, measurable events, such as sales, are stored in a central fact table, and descriptive attributes, such as product, customer and store details, are stored in dimension tables joined to it. Analysts filter and group by dimension attributes and aggregate fact measures.

Table design follows the schema. Large fact tables are distributed on the key of their most frequent join, so that join is co-located, and sorted on the date column used in most filters. Small dimension tables use ALL distribution so that every join to them is local. Denormalising a few frequently used attributes into the fact table, such as a product category, can avoid a join entirely for the most common queries, at the cost of some duplicated storage.

Sort into groups

Fact or dimension?

Exam skills 2.4.1

Decide whether each table is a fact or a dimension in a star schema.

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

Maintenance

Tables need maintenance as data changes. Deleted and updated rows leave space that is not reused immediately, and new rows are appended unsorted, which weakens the effect of sort keys. VACUUM reclaims that space and re-sorts rows, and ANALYZE refreshes the statistics the query planner uses to choose efficient plans. Redshift runs both automatically in the background when the warehouse is not busy, so manual runs are needed mainly after very large loads or deletes.

Redshift Advisor analyses the workload and recommends improvements, such as distribution and sort keys for specific tables. The system view SVV_TABLE_INFO shows each table's distribution skew, where some slices hold much more data than others and slow every query, and the proportion of unsorted rows, which shows whether a table needs vacuuming.

Match the pairs

Table maintenance

Exam skills 2.4.5 · 3.3.4

Match each need with the Redshift maintenance tool.

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

Choosing distribution and sort keys

A warehouse has a 2-billion-row sales fact table, a 5-million-row customers table and a 500-row stores table. Most queries join sales to customers and filter on the last 30 days. The following table gives the design chosen for each table.

TableDistributionSort keyReason
salesKEY on customer_idsale_dateCo-locates rows with matching customers; date filters skip blocks
customersKEY on customer_id—Joins to sales without redistribution
storesALL—Small table copied to every node

Before the change, the query plan showed DS_BCAST_INNER and DS_DIST_BOTH steps, meaning data was being moved between nodes for every join. After it, the join runs locally on each node. AUTO distribution and sort keys would make similar choices over time; explicit keys help when access patterns are well known.

Check yourself

Which distribution style suits a small dimension table?

Show answer

ALL, which copies the table to every node so joins need no data movement.

Exam Tips

Remember for the exam

  • Large tables joined on the same column suit KEY distribution on that column.
  • Small dimension tables suit ALL distribution.
  • When unsure, use AUTO distribution and sort keys.
  • Range filters on dates call for a sort key on the date column.
  • Semi-structured JSON in Redshift calls for the SUPER type with PartiQL.