-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_pruning.sql
More file actions
28 lines (25 loc) · 886 Bytes
/
query_pruning.sql
File metadata and controls
28 lines (25 loc) · 886 Bytes
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
-- First execution: This will use compute credits
SELECT
region,
SUM(amount) as total_sales
FROM sales
GROUP BY region
ORDER BY total_sales DESC;
-- Bad: Full scan reads everything
SELECT COUNT(*), SUM(amount) FROM sales;
-- Good: Filtered query only reads what it needs
SELECT COUNT(*), SUM(amount) FROM sales
WHERE order_date >= '2024-01-01'
AND order_date < '2024-07-01';
-- Check recent queries to see the difference in data scanned and execution time
SELECT
LEFT(query_text, 50) as query,
ROUND(bytes_scanned / 1024 / 1024, 2) as mb_scanned,
ROUND(total_elapsed_time / 1000, 2) as seconds
FROM TABLE(INFORMATION_SCHEMA.QUERY_HISTORY())
WHERE query_text LIKE '%FROM sales%'
AND query_text NOT LIKE '%QUERY_HISTORY%'
AND query_text NOT LIKE '%sales_by_region%'
AND execution_status = 'SUCCESS'
ORDER BY start_time DESC
LIMIT 5;