Redshift
Amazon Redshift
AWS's data warehouse. MPP architecture.
Amazon Redshift
AWS's data warehouse. MPP architecture.
Amazon Redshift is a cloud data warehouse that AWS launched in 2012. Although it is based on PostgreSQL, it is optimized for petabyte-scale analytics through columnar storage, MPP (Massively Parallel Processing), and automatic compression. It is well suited to OLAP (Online Analytical Processing) workloads.
The architecture consists of a Leader Node and Compute Nodes. The Leader Node parses queries, builds an execution plan, and distributes it to the Compute Nodes. Each Compute Node processes data slices in parallel and aggregates the results to the Leader. It scales horizontally by increasing the number of nodes.
With Redshift Spectrum, you can query S3 data directly as external tables, which enables a lakehouse pattern in which only frequently used data is loaded into Redshift and the rest is left in S3. It is useful for cost-efficient cold data analysis.
Redshift Serverless was launched in 2022 and is used with query-based billing, without node provisioning. It is cost-efficient for intermittent analytics workloads, and RA3 instances separate compute and storage so that each can be scaled independently.
-- ํ
์ด๋ธ ์์ฑ (๋ถ์ฐ ํค, ์ ๋ ฌ ํค ์ค์ )
CREATE TABLE sales (
sale_id BIGINT IDENTITY(1,1),
product_id INT NOT NULL,
customer_id INT NOT NULL,
sale_date DATE NOT NULL,
amount DECIMAL(10,2) NOT NULL,
region VARCHAR(50)
)
DISTSTYLE KEY DISTKEY(customer_id) -- ๋ถ์ฐ ํค
SORTKEY(sale_date); -- ์ ๋ ฌ ํค
-- COPY: S3์์ ๋ฐ์ดํฐ ๋ก๋ (๊ฐ์ฅ ๋น ๋ฅธ ๋ฐฉ๋ฒ)
COPY sales
FROM 's3://my-bucket/sales-data/'
IAM_ROLE 'arn:aws:iam::123456789:role/RedshiftLoadRole'
FORMAT AS PARQUET;
-- CSV ๋ก๋ (์์ถ ํ์ผ ์ง์)
COPY sales
FROM 's3://my-bucket/sales.csv.gz'
IAM_ROLE 'arn:aws:iam::123456789:role/RedshiftLoadRole'
CSV GZIP
IGNOREHEADER 1
DATEFORMAT 'YYYY-MM-DD';
-- ๋ถ์ ์ฟผ๋ฆฌ
SELECT
DATE_TRUNC('month', sale_date) AS month,
region,
COUNT(*) AS num_sales,
SUM(amount) AS total_revenue,
AVG(amount) AS avg_order_value
FROM sales
WHERE sale_date >= '2024-01-01'
GROUP BY 1, 2
ORDER BY 1, total_revenue DESC;
-- ์๋์ฐ ํจ์
SELECT
product_id,
sale_date,
amount,
SUM(amount) OVER (PARTITION BY product_id ORDER BY sale_date
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cumulative_sales,
RANK() OVER (PARTITION BY DATE_TRUNC('month', sale_date) ORDER BY amount DESC) AS monthly_rank
FROM sales;
-- Redshift Spectrum: S3 ์ธ๋ถ ํ
์ด๋ธ
CREATE EXTERNAL SCHEMA spectrum_schema
FROM DATA CATALOG
DATABASE 'my_glue_database'
IAM_ROLE 'arn:aws:iam::123456789:role/SpectrumRole'
REGION 'us-east-1';
-- S3 ๋ฐ์ดํฐ ์ง์ ์ฟผ๋ฆฌ (Redshift ํ
์ด๋ธ๊ณผ ์กฐ์ธ ๊ฐ๋ฅ)
SELECT
s.product_id,
p.product_name,
SUM(s.amount) AS total
FROM sales s
JOIN spectrum_schema.products p ON s.product_id = p.id
GROUP BY 1, 2;
-- ์ฟผ๋ฆฌ ์ฑ๋ฅ ๋ถ์
EXPLAIN
SELECT * FROM sales WHERE region = 'Seoul';
-- ํ
์ด๋ธ ํต๊ณ ๊ฐฑ์
ANALYZE sales;
-- ๋์คํฌ ๊ณต๊ฐ ํ์ (DELETE ํ)
VACUUM sales;
-- Python์์ ์ฐ๊ฒฐ (psycopg2 ๋๋ redshift_connector)
-- import redshift_connector
-- conn = redshift_connector.connect(
-- host='cluster.xxx.region.redshift.amazonaws.com',
-- database='dev',
-- user='admin',
-- password='password'
-- )
-- cursor = conn.cursor()
-- cursor.execute("SELECT * FROM sales LIMIT 10")
Senior: "We keep recent data in Redshift tables, and data older than a year in S3, which we query with Spectrum. Storage costs drop dramatically."
Junior: "My query is slow. What should I do?"
Senior: "Check the execution plan with EXPLAIN, and verify that DISTKEY and SORTKEY match your query patterns. You should also run ANALYZE and VACUUM periodically."
Interviewer: "Please tell me about your data warehouse experience."
Candidate: "I analyzed 1TB of data in Redshift. I improved query performance 5x by setting DISTKEY on the join column and SORTKEY on the filtering column, and I shortened ETL time by loading Parquet files from S3 in parallel with the COPY command. I also reduced cold data query costs with Spectrum."
Take a mock interview on this topic โ WIA SOOM AI Interview ยท Start for free
Reviewer: "Use COPY instead of INSERT. INSERT is far too slow for bulk data loads."
Developer: "Ah, so I can store the data in S3 first and then load it with COPY."