Efficient management of Snowflake resources is crucial for optimizing performance and cost. One of the key aspects is monitoring warehouse usage. In this blog post, we’ll explore a use case where we automate the reporting of warehouse usage and send detailed weekly summaries via email. Our Organization is a data-driven organization relying heavily on Snowflake for its data warehousing needs. The company uses multiple warehouses for different environments such as development, testing, and production. To ensure optimal usage and cost management, the IT team needs to monitor the credit usage of these warehouses on a weekly basis.
Objective: Automate the generation and distribution of a weekly report that details the credit usage of specified warehouses, compares it with the previous week, and highlights any significant changes.
Solution: We’ll create a Snowflake stored procedure that:
- Queries the warehouse metering history to get the weekly credit usage for the past 28 days.
- Constructs an HTML email report summarizing the warehouse usage and changes.
- Sends the report to the IT team’s email address
Technical implementation:
Step 1: Create the Stored Procedure
We’ll start by creating a stored procedure in Snowflake. This procedure will execute a query to gather the necessary data, format it into an HTML table, and send it via email.
Step 2: Schedule the Stored Procedure
To automate this process, we’ll use Snowflake Tasks to schedule the stored procedure to run weekly.
CREATE OR REPLACE TASK SEND_WH_INFO_MAIL_TASK
WAREHOUSE = my_warehouse
SCHEDULE = 'USING CRON 0 0 * * 0 UTC' -- Every Sunday at midnight UTC
AS
CALL SEND_WH_INFO_MAIL();
Output:
Benefits
- Automation: The entire process is automated, ensuring regular and timely reporting without manual intervention.
- Efficiency: IT teams can quickly identify usage patterns, trends, and anomalies, enabling proactive management of resources.
- Cost Management: By monitoring usage, the team can make informed decisions on scaling and resource allocation, potentially reducing costs.