-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06-performance-solutions.sql
More file actions
62 lines (54 loc) · 1.04 KB
/
Copy path06-performance-solutions.sql
File metadata and controls
62 lines (54 loc) · 1.04 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
-- Solutions 06: Performance and Query Plans
-- 1.
EXPLAIN
SELECT *
FROM superstore
WHERE region = 'West';
-- 2.
EXPLAIN ANALYZE
SELECT
sub_category,
SUM(sales) AS total_sales
FROM superstore
GROUP BY sub_category
ORDER BY total_sales DESC;
-- 3.
CREATE INDEX IF NOT EXISTS idx_superstore_region
ON superstore(region);
EXPLAIN
SELECT *
FROM superstore
WHERE region = 'West';
-- 4.
EXPLAIN
SELECT
p.year_actual,
COUNT(*) AS total_orders,
SUM(s.sales) AS total_sales
FROM superstore s
JOIN periode p
ON s.order_date = p.date_actual
GROUP BY p.year_actual
ORDER BY p.year_actual;
-- 5.
EXPLAIN ANALYZE
SELECT
customer_id,
customer_name,
SUM(profit) AS total_profit
FROM superstore
GROUP BY customer_id, customer_name
ORDER BY total_profit DESC
LIMIT 5;
-- 6.
CREATE INDEX IF NOT EXISTS idx_superstore_customer_id_perf
ON superstore(customer_id);
EXPLAIN ANALYZE
SELECT
customer_id,
customer_name,
SUM(profit) AS total_profit
FROM superstore
GROUP BY customer_id, customer_name
ORDER BY total_profit DESC
LIMIT 5;