Data Warehouse: Synapse Dedicated Pools & CTAS
Enterprise data warehousing with Synapse dedicated pools, CTAS patterns, and star schema design
Data Warehouse Architecture
CTAS Pattern Examples
-- Create fact table with CTAS
CREATE TABLE [dbo].[FactSales]
WITH
(
DISTRIBUTION = HASH(date_key),
CLUSTERED COLUMNSTORE INDEX,
PARTITION = (date_key RANGE RIGHT FOR VALUES
(20240101, 20240201, 20240301, 20240401,
20240501, 20240601, 20240701, 20240801,
20240901, 20241001, 20241101, 20241201))
)
AS
SELECT
s.sale_id,
d.date_key,
c.customer_key,
p.product_key,
s.quantity,
s.unit_price,
s.quantity * s.unit_price AS total_amount,
s.region,
GETDATE() AS load_date
FROM [staging].[Sales] s
INNER JOIN [dbo].[DimDates] d
ON s.sale_date = d.full_date
INNER JOIN [dbo].[DimCustomers] c
ON s.customer_id = c.customer_id
INNER JOIN [dbo].[DimProducts] p
ON s.product_id = p.product_id;
-- Create dimension table with replicated distribution
CREATE TABLE [dbo].[DimCustomers]
WITH
(
DISTRIBUTION = REPLICATE,
CLUSTERED INDEX (customer_key)
)
AS
SELECT
customer_id AS customer_key,
customer_name,
email,
segment,
region,
GETDATE() AS load_date
FROM [staging].[Customers];
-- Create aggregate table
CREATE TABLE [dbo].[AggDailySalesByRegion]
WITH
(
DISTRIBUTION = HASH(region),
CLUSTERED COLUMNSTORE INDEX
)
AS
SELECT
date_key,
region,
SUM(total_amount) AS daily_revenue,
COUNT(*) AS transaction_count,
COUNT(DISTINCT customer_key) AS unique_customers
FROM [dbo].[FactSales]
GROUP BY date_key, region;
Distribution Strategy Guide
| Table Type | Recommended Distribution | Reason |
|---|---|---|
| Large fact tables (>1GB) | HASH (join column) | Even distribution, efficient joins |
| Small dimension tables (<2GB) | REPLICATE | Avoid shuffling for joins |
| Staging tables | ROUND ROBIN | Fast load, no skew |
| Aggregate tables | HASH (group by column) | Even distribution |
âšī¸
Pro Tip: Use DBCC PDW_SHOWSPACEUSED('table_name') to check distribution skew. If any distribution is significantly larger than others, consider changing the distribution key.
Interview Questions
Q1: How do you choose between Hash and Round Robin distribution? A: Hash for tables that are frequently joined (even distribution on join key). Round Robin for staging tables or when no clear join key exists (fastest load, even distribution).
Q2: What is the benefit of Clustered Columnstore Indexes in Synapse? A: CCI provides optimal compression (up to 10x), columnar storage for analytics, and batch-mode execution. It's the default and recommended index type for fact tables.
Q3: How do you handle slowly changing dimensions in Synapse? A: Use MERGE statement for SCD Type 1 (overwrite). For SCD Type 2, create history tables with effective dates and status flags. Use Synapse Serverless for staging changes before merging.
Performance Optimization Techniques
Index Optimization
| Index Type | Best For | Compression | Maintenance |
|---|---|---|---|
| Clustered Columnstore | Fact tables, large scans | 10-20x | Automatic |
| Clustered Index | Point lookups, small tables | 2-5x | Rebuild/Organize |
| Non-Clustered Columnstore | Secondary queries | 5-10x | Rebuild |
| Non-Clustered Index | Filtered lookups | None | Rebuild |
Query Optimization
-- Bad: scans entire table, no predicate pushdown
SELECT * FROM FactSales WHERE year = 2024;
-- Good: selective predicate, column selection, partition elimination
SELECT region, SUM(total_amount) AS revenue
FROM FactSales
WHERE date_key BETWEEN 20240101 AND 20241231
GROUP BY region;
Partitioning Strategy
| Partition Range | Use Case | File Count |
|---|---|---|
| Daily | Real-time analytics | 365+/year |
| Monthly | Standard BI | 12/year |
| Yearly | Archival, compliance | 1/year |
Monitoring and Alerting
# Query performance monitoring
from azure.monitor.query import MetricsQueryClient
from azure.identity import DefaultAzureCredential
credential = DefaultAzureCredential()
metrics_client = MetricsQueryClient(credential)
# Track key metrics
metrics_to_monitor = [
"DWUConsumptionPercentage",
"QueryWaitTime",
"TempdbUsage",
"CacheHitPercentage",
"RowsRead",
"RowsWritten"
]
Real-World Case Study
E-commerce Data Warehouse: A mid-size retailer migrated from on-prem SQL Server to Synapse. They:
- Used HASH distribution on
customer_keyfor the fact table (2TB, 500M rows) - Replicated 15 small dimension tables (<500MB each)
- Partitioned fact table monthly for 3 years
- Achieved 15x query performance improvement
- Reduced cost by 40% using auto-pause during non-business hours
Best Practices Checklist
- Choose distribution key based on join patterns
- Keep replicated tables under 2GB
- Use Clustered Columnstore for fact tables >1GB
- Partition by the most common filter column
- Create statistics on filtered columns
- Monitor distribution skew monthly
- Use result set caching for repeated dashboards
- Implement resource classes for concurrency management A: Use MERGE statement for SCD Type 1 (overwrite). For SCD Type 2, create history tables with effective dates and status flags. Use Synapse Serverless for staging changes before merging.
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type â Always start by categorizing the problem
- Incorrect formula application â Verify the formula applies to your specific case
- Unit errors â Always check units are consistent throughout
- Sign errors â Pay careful attention to positive and negative values
- Rounding too early â Keep full precision until the final answer
- Forgetting constraints â Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic â Build on the foundation laid here
- Next Topic â See how this concept extends to more advanced material
- Practice Problems â Additional exercises with solutions
- Video Explanation â Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions