-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinops_and_monitoring.sql
More file actions
54 lines (51 loc) · 1.76 KB
/
finops_and_monitoring.sql
File metadata and controls
54 lines (51 loc) · 1.76 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
-- -- View warehouse credit consumption
SELECT
warehouse_name,
DATE_TRUNC('DAY', start_time) as usage_date,
SUM(credits_used) as total_credits,
ROUND(SUM(credits_used) * 2, 2) as estimated_cost_usd
FROM SNOWFLAKE.ACCOUNT_USAGE.WAREHOUSE_METERING_HISTORY
WHERE start_time >= DATEADD('day', -7, CURRENT_DATE())
GROUP BY warehouse_name, DATE_TRUNC('DAY', start_time)
ORDER BY usage_date DESC, warehouse_name;
-- -- Top 10 most expensive queries
SELECT
query_id,
user_name,
LEFT(query_text, 60) as query_preview,
ROUND(total_elapsed_time / 1000, 2) as execution_seconds,
ROUND((total_elapsed_time / 1000 / 3600) * CASE warehouse_size
WHEN 'XSMALL' THEN 1
WHEN 'SMALL' THEN 2
WHEN 'MEDIUM' THEN 4
WHEN 'LARGE' THEN 8
END, 6) as estimated_credits
FROM SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY
WHERE start_time >= DATEADD('day', -1, CURRENT_DATE())
AND execution_status = 'SUCCESS'
AND warehouse_name IS NOT NULL
ORDER BY estimated_credits DESC
LIMIT 10;
-- Monthly budget tracking dashboard
WITH monthly_usage AS (
SELECT
warehouse_name,
SUM(credits_used) as credits_used
FROM SNOWFLAKE.ACCOUNT_USAGE.WAREHOUSE_METERING_HISTORY
WHERE start_time >= DATE_TRUNC('MONTH', CURRENT_DATE())
GROUP BY warehouse_name
)
SELECT
warehouse_name,
ROUND(credits_used, 4) as credits_used,
50 as monthly_budget,
ROUND((credits_used / 50) * 100, 1) as pct_consumed,
CASE
WHEN credits_used < 25 THEN 'On Track'
WHEN credits_used < 40 THEN 'Monitor Closely'
WHEN credits_used < 50 THEN 'Near Limit'
ELSE 'Over Budget'
END as status,
ROUND(50 - credits_used, 2) as credits_remaining
FROM monthly_usage
ORDER BY pct_consumed DESC;