Lesser Known aspects of Amazon Redshift

Abdul Rafee Wahab
4 min readApr 28, 2023
Image Source: Datanyze

Background

Redshift is a powerful cloud-based data warehousing solution provided by AWS.

It is designed to handle large amounts of data (petabytes), and support complex analytical queries.

While many of its features are well-known, there are several aspects of Redshift that are not widely understood.

In this segment, I will explore these lesser-known aspects of Redshift, and illustrate how they work.

Query Optimizer

Redshift uses a query optimizer to analyze and optimize SQL queries.

It has several optimization techniques, such as:

  • Predicate pushdown
  • JOIN reordering
  • Projection pushdown

This allows queries to run faster and more efficiently.

However, it is important to note that the query optimizer is not perfect and may not always choose the best execution plan. It is recommended to always review the execution plan and tune the queries if necessary.

Example: Suppose we have a table named sales with columns date, product, and sales_amount. We want to find the total sales amount for each product in the last week.

The following query can be used:

SELECT product, SUM(sales_amount)
FROM sales
WHERE date >= DATEADD('day', -7, GETDATE())
GROUP BY product;

The query optimizer may choose to use an index scan on the date column and a hash aggregate on the product column. However, if the data distribution is skewed, this may result in poor performance. To address this, we can use a sort key on the date column to improve query performance:

CREATE TABLE sales (
date DATE,
product VARCHAR(50),
sales_amount FLOAT
)
DISTKEY (product)
SORTKEY (date);

SELECT product, SUM(sales_amount)
FROM sales
WHERE date >= DATEADD('day', -7, GETDATE())
GROUP BY product;

Automatic Compression

Redshift automatically compresses data to save storage space and improve query performance.

It uses multiple compression algorithms, such as:

  • Run-length encoding
  • Dictionary encoding
  • Delta encoding

However, not all data types can be compressed. It is important to choose the appropriate data types and configure compression settings to optimize storage and performance.

Here is an example of creating a table with automatic compression:

CREATE TABLE sales (
date DATE,
amount DECIMAL(18,2) ENCODE lzo
);

In this example, the amount column is configured to use the LZO compression algorithm.

Concurrency Scaling

Redshift provides a feature called concurrency scaling, which automatically adds and removes compute resources based on the workload.

This allows queries to run faster and more efficiently, even during peak usage.

However, it is important to note that concurrency scaling can incur additional costs, and it is recommended to monitor and adjust the settings accordingly.

Example: Suppose we have a table named clicks with columns timestamp, user_id, and page_id. We want to find the number of clicks for each page in the last hour.

The following query can be used:

SELECT page_id, COUNT(*)
FROM clicks
WHERE timestamp >= DATEADD('hour', -1, GETDATE())
GROUP BY page_id;

If the workload increases, we can enable concurrency scaling to automatically add more compute nodes:

ALTER CLUSTER mycluster
SET enable_concurrency_scaling = true,
min_concurrency_scaling_cluster_size = 2,
max_concurrency_scaling_cluster_size = 10;

COPY Command

Redshift provides a COPY command for loading data into the data warehouse.

It supports several file formats, such as CSV, JSON, and Parquet. Additionally, it can perform data transformations and validations, such as data type conversions and data cleaning.

It is recommended to use the COPY command for loading large amounts of data into Redshift, as it is more efficient than using SQL inserts.

Here is an example of using the COPY command to load data from a CSV file:

COPY sales
FROM 's3://my-bucket/sales.csv'
CREDENTIALS 'aws_iam_role=arn:aws:iam::1234567890:role/MyRedshiftRole'
CSV
IGNOREHEADER 1;

Data Retention

Redshift allows users to configure the retention period for backups and snapshots.

However, it is important to note that the retention period applies to all backups and snapshots, including manual and automated ones.

Additionally, deleting a cluster also deletes all associated backups and snapshots. It is recommended to carefully configure and manage the data retention settings to ensure data availability and compliance.

Here is an example of configuring the data retention period:

ALTER CLUSTER my-cluster
MODIFY RETENTION PERIOD 7;

In this example, the retention period is set to 7 days.

Closing thoughts 👏

In summary, Amazon Redshift is a fully managed data warehouse service provided by Amazon Web Services (AWS) designed to handle large amounts of data and support complex analytical queries.

In addition to its well-known features, such as query optimizer and automatic compression, it also has lesser-known aspects such as concurrency scaling and the COPY command for data loading.

These features can significantly improve the performance and efficiency of Redshift. Understanding and applying them is crucial for optimizing queries and managing the workload.

--

--

Abdul Rafee Wahab

Tech guy. I like building cool software, & also leading others in building cool things. All views shared are my own.