top of page

Coffee and Tips Newsletter

Assine nossa newsletter para receber tutoriais Tech, reviews de dispositivos e notícias do mundo Tech no seu email

Nos vemos em breve!

Writer's pictureJP

Don't Let Your Dashboards Break: Understanding DistKey and SortKey in Practice

Entendendo DistKey e SortKey na prática

First, About AWS Redshift?


Redshift is a highly scalable cloud-based data warehouse service offered by AWS. It allows companies to quickly analyze large volumes of data using standard SQL and BI tools. Redshift's architecture is optimized for large-scale data analysis, leveraging parallelization and columnar storage for high performance.

I recommend reading my post where I dive deeper into Redshift’s architecture and its components, available at Understanding AWS Redshift and Its Components.

Why Use DistKey and SortKey?

Understanding DistKey and SortKey in practice can provide several benefits, the most important being improved query performance. DistKey optimizes joins and aggregations by efficiently distributing data across nodes, while SortKey speeds up queries that filter and sort data, allowing Redshift to read only the necessary data blocks. Both help to make queries faster and improve resource efficiency.


DistKey and How It Works


DistKey (or Distribution Key) is the strategy for distributing data across the nodes of a Redshift cluster. When you define a column as a DistKey, the records sharing the same value in that column are stored on the same node, which can reduce the amount of data movement between nodes during queries.


One of the main advantages is Reducing Data Movement Between Nodes, increasing query performance and improving the utilization of Redshift’s distributed processing capabilities.


Pay Attention to Cardinal


Choosing a column with low cardinality (few distinct values) as a DistKey can result in uneven data distribution, creating "hot nodes" (nodes overloaded with data) and degrading performance.


What is Cardinality?


Cardinality refers to the number of distinct values in a column. A column with high cardinality has many distinct values, making it a good candidate for a DistKey in Amazon Redshift. High cardinality tends to distribute data more evenly across nodes, avoiding overloaded nodes and ensuring balanced query performance.

Although the idea behind DistKey is to distribute distinct values evenly across nodes, keep in mind that if data moves frequently between nodes, it will reduce the performance of complex queries. Therefore, it’s important to carefully choose the right column to define as a DistKey.


Benefits of Using DistKey


To make it clearer, here are some benefits of choosing the right DistKey strategy:


Reduced Data Movement Between Nodes:


When data sharing the same DistKey is stored on the same node, join and aggregation operations using that key can be performed locally on a single node. This significantly reduces the need to move data between nodes, which is one of the main factors affecting query performance in distributed systems.


Better Performance in Joins and Filtered Queries:


If queries frequently perform joins between tables sharing the same DistKey, keeping the data on the same node can drastically improve performance. Query response times are faster because operations don’t require data redistribution between nodes.


Suppose you have two large tables in your Redshift cluster:


  • Table A (transactions): Contains billions of customer transaction records.

  • Table B (customers): Stores customer information.


Both tables have the column client_id. If you frequently run queries joining these two tables to get transaction details by customer, defining client_id as the DistKey on both tables ensures that records for the same customer are stored on the same node.


SELECT A.transaction_id, A.amount, B.customer_name
FROM transactions A
JOIN customers B
ON A.client_id = B.client_id
WHERE B.state = 'CA';

By keeping client_id on the same node, joins can be performed locally without needing to redistribute data across different nodes in the cluster. This dramatically reduces query response times.


Without a DistKey, Redshift would need to redistribute data from both tables across nodes to execute the join, increasing the query’s execution time. With client_id as the DistKey, data is already located on the same node, allowing for much faster execution.


Storage and Processing Efficiency:


Local execution of operations on a single node, without the need for redistribution, leads to more efficient use of CPU and memory resources. This can result in better overall cluster utilization, lower costs, and higher throughput for queries.


Disadvantages of Using DistKey


Data Skew (Imbalanced Data Distribution):


One of the biggest disadvantages is the risk of creating data imbalance across nodes, known as data skew. If the column chosen as the DistKey has low cardinality or if values are not evenly distributed, some nodes may end up storing much more data than others. This can result in overloaded nodes, degrading overall performance.


Reduced Flexibility for Ad Hoc Queries:


When a DistKey is defined, it optimizes specifically for queries that use that key. However, if ad hoc queries or analytical needs change, the DistKey may no longer be suitable. Changing the DistKey requires redesigning the table and possibly redistributing the data, which can be time-consuming and disruptive.o.


Poor Performance in Non-Optimized Queries:


If queries that don’t effectively use the DistKey are executed, performance can suffer. This is particularly relevant in scenarios where queries vary widely or don’t follow predictable patterns. While the lack of data movement between nodes is beneficial for some queries, it may also limit performance for others that require access to data distributed across all nodes.


How to Create a DistKey in Practice


After selecting the best strategy based on the discussion above, creating a DistKey is straightforward. Simply add the DISTKEY keyword when creating the table.


CREATE TABLE sales (
    sale_id INT,
    client_id INT DISTKEY,
    sale_date DATE,
    amount DECIMAL(10, 2)
);

In the example above, the column client_id has been defined as the DistKey, optimizing queries that retrieve sales data by customer.


SortKey and How It Works


SortKey is the key used to determine the physical order in which data is stored in Redshift tables. Sorting data can significantly speed up queries that use filters based on the columns defined as SortKey.


Benefits of SortKey


Query Performance with Filters and Groupings:


One of the main advantages of using SortKey is improved performance for queries applying filters (WHERE), orderings (ORDER BY), or groupings (GROUP BY) on the columns defined as SortKey. Since data is physically stored on disk in the order specified by the SortKey, Redshift can read only the necessary data blocks, instead of scanning the entire table.


Reduced I/O and Increased Efficiency:


With data ordered by SortKey, Redshift minimizes I/O by accessing only the relevant data blocks for a query. This is especially useful for large tables, where reading all rows would be resource-intensive. Reduced I/O results in faster query response times.


Easier Management of Temporal Data:


SortKeys are particularly useful for date or time columns. When you use a date column as a SortKey, queries filtering by time ranges (e.g., "last 30 days" or "this year") can be executed much faster. This approach is common in scenarios where data is queried based on dates, such as transaction logs or event records.


Support for the VACUUM Command:


The VACUUM command is used to reorganize data in Redshift, removing free space and applying the order defined by the SortKey. Tables with a well-defined SortKey benefit the most from this process, as VACUUM can efficiently reorganize the data, resulting in a more compact table and even faster queries.


Disadvantages of Using SortKey


Incorrect Choice of SortKey Column:


If an inappropriate column is chosen as the SortKey, there may be no significant improvement in query performance—or worse, performance may actually degrade. For example, if the selected column is not frequently used in filters or sorting, the advantage of accessing data blocks efficiently is lost, meaning Redshift will scan more blocks, resulting in higher query latency.

An example would be defining a status column (with few distinct values) as the SortKey in a table where queries typically filter by transaction_date. This would result in little to no improvement in execution time.

Table Size and Reorganization


In very large tables, reorganizing data to maintain SortKey efficiency can be slow and resource-intensive. This can impact system availability and overall performance.

For example, when a table with billions of records needs to be reorganized due to inserts or updates that disrupt the SortKey order, the VACUUM operation can take hours or even days, depending on the table size and cluster workload.


Difficulty in Changing the SortKey


Changing the SortKey of an existing table can be complex and time-consuming, especially for large tables. This involves creating a new table, copying the data to the new table with the new SortKey, and then dropping the old table.

In other words, if you realize that the originally chosen SortKey is no longer optimizing queries as expected, changing the SortKey may require a complete data migration, which can be highly disruptive.


How to Create a SortKey in Practice


Here, sale_date was defined as the SortKey, ideal for queries that filter records based on specific dates or date ranges.

CREATE TABLE sales (
    sale_id INT,
    client_id INT ,
    sale_date DATE SORTKEY,
    amount DECIMAL(10, 2)
);

Conclusion


SortKey is highly effective for speeding up queries that filter, sort, or group data. By physically ordering the data on disk, SortKeys allow Redshift to read only the relevant data blocks, resulting in faster query response times and lower resource usage. However, choosing the wrong SortKey or failing to manage data reorganization can lead to degraded performance and increased complexity.

On the other hand, DistKey is crucial for optimizing joins and aggregations across large tables. By efficiently distributing data across cluster nodes, a well-chosen DistKey can minimize data movement between nodes, significantly improving query performance. The choice of DistKey should be based on column cardinality and query patterns to avoid issues like data imbalance or "hot nodes."

Both SortKey and DistKey require careful analysis and planning. Using them improperly can result in little or no performance improvement—or even worsen performance. Changing SortKeys or DistKeys can also be complex and disruptive in large tables.

Therefore, the key to effectively using SortKey and DistKey in Redshift is a clear understanding of data access patterns and performance needs. With proper planning and monitoring, these tools can transform the way you manage and query data in Redshift, ensuring your dashboards and reports remain fast and efficient as data volumes grow.

I hope you enjoyed this overview of Redshift’s powerful features. All points raised here are based on my team's experience in helping various areas within the organization leverage data for value delivery.

I aimed to explain the importance of thinking through strategies for DistKey and SortKey in a simple and clear manner, with real-world examples to enhance understanding. Until next time!

Recent Posts

See All

Kommentare


bottom of page