-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroup9_JoinPerformance.sql
More file actions
83 lines (67 loc) · 2.83 KB
/
Copy pathGroup9_JoinPerformance.sql
File metadata and controls
83 lines (67 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
------------------------------------------------------------------
-- PART 3: JOIN TECHNIQUES AND QUERY OPTIMIZATION
------------------------------------------------------------------
-- 1. Nested Loop Join
-- =====================================================================
-- Forces PostgreSQL to use nested loop join for testing
SET enable_hashjoin = OFF;
SET enable_mergejoin = OFF;
SET enable_mergejoin = ON;
EXPLAIN ANALYZE
SELECT f.transaction_id, c.customer_id, m.merchant_city, f.amount
FROM dw.fact_transactions f
JOIN dw.dim_customer c ON f.customer_key = c.customer_key
JOIN dw.dim_merchant m ON f.merchant_key = m.merchant_key
WHERE f.amount > 500;
-- 2. Sort-Merge Join
-- =====================================================================
-- Forces PostgreSQL to use merge join for testing
SET enable_hashjoin = OFF;
SET enable_nestloop = OFF;
SET enable_mergejoin = ON;
EXPLAIN ANALYZE
SELECT f.transaction_id, cd.card_brand, f.amount
FROM dw.fact_transactions f
JOIN dw.dim_card cd ON f.card_key = cd.card_key
WHERE f.fraud_label = TRUE
ORDER BY cd.card_brand, f.amount;
-- 3. Hash Join
-- =====================================================================
-- Forces PostgreSQL to use hash join
SET enable_mergejoin = OFF;
SET enable_nestloop = OFF;
SET enable_hashjoin = ON;
EXPLAIN ANALYZE
SELECT
m.merchant_state,
SUM(f.amount) AS total_amount,
COUNT(*) AS num_transactions
FROM dw.fact_transactions f
JOIN dw.dim_merchant m
ON f.merchant_key = m.merchant_key
GROUP BY m.merchant_state
ORDER BY total_amount DESC;
------------------------------------------------------------------
-- DSS vs OLTP query comparison
------------------------------------------------------------------
-- DSS Query (Decision Support System)
-- Long-running aggregation example: monthly revenue by state
EXPLAIN ANALYZE
SELECT d.year, d.month, m.merchant_state, SUM(f.amount) AS monthly_total
FROM dw.fact_transactions f
JOIN dw.dim_date d ON f.date_key = d.date_key
JOIN dw.dim_merchant m ON f.merchant_key = m.merchant_key
GROUP BY d.year, d.month, m.merchant_state
ORDER BY d.year, d.month, m.merchant_state;
-- OLTP Query (Online Transaction Processing)
-- Simple lookup: find one transaction by ID
EXPLAIN ANALYZE
SELECT f.transaction_id, c.customer_id, f.amount, f.fraud_label
FROM dw.fact_transactions f
JOIN dw.dim_customer c ON f.customer_key = c.customer_key
WHERE f.transaction_id = 123456;
-- Observations:
-- 1. DSS queries have higher cost, scan more rows, may trigger full table scans.
-- 2. OLTP queries are faster due to indexes (primary keys, foreign keys), small row access.
-- 3. DSS often benefits from pre-aggregated tables (MOLAP) or proper indexes on join keys.
-- 4. OLTP queries are optimized for quick reads/writes with indexed lookups.