When designing tables in Redshift, choosing the right distribution style is crucial for query performance. Distribution styles dictate how data rows are spread across the compute nodes (and their slices) in your cluster. An optimal distribution minimizes data movement between nodes during query execution, especially for joins.
- AUTO allows Redshift to manage distribution, often defaulting to EVEN or KEY based on data patterns.
- EVEN distributes rows round-robin, ensuring even data spread but potentially requiring data movement for joins.
- ALL copies the entire table to every node, beneficial for small dimension tables frequently joined with large fact tables, but consumes more storage and increases load times.
- KEY distributes rows based on the hash of values in a chosen column. This is often the most performant for large fact tables, as it colocates matching rows from joined tables on the same node, drastically reducing data transfer over the network during joins. For instance, if orders and order_items tables are both distributed by order_id, then all data for a specific order_id will reside on the same node, making joins extremely efficient.
Complementing distribution styles are sort keys, which determine the physical order in which data is stored within each slice on disk. While distribution handles where data lives across nodes, sort keys manage how it's organized once it's there. Properly chosen sort keys significantly accelerate queries that filter on a specific range, perform aggregations, or involve joins where data needs to be merged.
- COMPOUND sort keys sort data based on the order of specified columns. This is ideal when your queries frequently filter or join on the first one or two columns in the key. For example, sorting by (date, product_id) is excellent for queries filtering by date or date and product_id.
- INTERLEAVED sort keys give equal weight to all columns in the sort key, which can be beneficial when queries filter on different combinations of columns within the key, or use columns out of order. However, INTERLEAVED keys are more complex to maintain (VACUUM and ANALYZE operations take longer) and consume more disk space, so COMPOUND is generally recommended first.
The synergy between distribution styles and sort keys is key to maximizing Redshift's performance. For large fact tables, a common strategy is to use KEY distribution on a column frequently used in joins (like customer_id or order_id) to ensure related data is on the same node. Then, apply a COMPOUND sort key on columns frequently used for filtering or time-series analysis (e.g., event_timestamp, status). Always analyze your most critical queries to identify the columns that are most often filtered, joined, or grouped by. Use the EXPLAIN command to see the query plan and identify potential bottlenecks, especially data distribution warnings or large DS_BCAST_GATHER steps, which often point to sub-optimal distribution. Regularly monitoring STL_QUERY and STV_WLM_QUERY_INFO can further inform your tuning efforts.
Key Takeaways
DISTKEYdetermines how data is spread across compute nodes, crucial for efficient joins.SORTKEYdefines the physical order of data within nodes, speeding up filtering and range scans.- Use
KEYdistribution on common join columns to colocate related data and minimize network I/O. COMPOUNDsort keys are generally preferred for speeding up queries filtering on leading sort key columns.- Analyze query patterns and use
EXPLAINto identify and optimize distribution and sort key choices.
Code Example
CREATE TABLE fact_sales (
sale_id BIGINT NOT NULL,
product_id INTEGER NOT NULL,
customer_id INTEGER NOT NULL,
sale_date DATE NOT NULL,
quantity INTEGER,
price DECIMAL(10, 2)
)
DISTSTYLE KEY
DISTKEY (customer_id)
SORTKEY (sale_date, product_id);How this code works
This code establishes a fact_sales table within Redshift, meticulously structured to optimize query performance by strategically distributing and sorting data. The DISTSTYLE KEY clause, combined with DISTKEY (customer_id), is crucial here. It instructs Redshift to distribute rows across its compute nodes based on the customer_id value. This ensures that all sales records for a specific customer reside on the same node. This design choice is particularly effective for queries that filter by customer or join fact_sales with a customer dimension table, as it minimizes the need for data to travel between different nodes, significantly speeding up those operations.
Beyond distribution, SORTKEY (sale_date, product_id) dictates how the data is ordered within each node. Rows are first sorted by sale_date, and then by product_id for records sharing the same date. This physical ordering is a powerful optimization: when queries filter by date ranges or group by product_id, Redshift can quickly locate relevant data blocks and skip over irrelevant ones, reducing the amount of data it needs to scan. The other columns like sale_id, quantity, and price simply define the attributes stored for each sales transaction.