Sub topic 14
Understanding cost and monitoring a warehouse
This chapter assumes you can start and stop a SQL warehouse and know the warehouse types, sizes, autoscaling, and auto stop settings from earlier chapters. You should also be able to write grouped SQL queries. Here you will learn what a warehouse bills for, where to watch it, and how to answer "what did this cost?" with a query.
What you are billed for
A SQL warehouse is billed in Databricks Units (DBUs). A DBU is a unit of processing capability, and you are charged per DBU-hour. Three facts drive almost every cost question:
- Billing follows uptime, not queries. A warehouse accrues DBUs from the moment it starts until it stops. Idle time before auto stop is billed.
- Size sets the rate. Each size step up roughly doubles the DBUs per hour. A Large warehouse costs about twice a Medium for each hour it runs.
- Clusters multiply the rate. The size describes one cluster. If autoscaling adds clusters, you pay for each running cluster.
The type changes what else appears on your bill:
| Type | DBU price covers | Cloud VM cost |
|---|---|---|
| Serverless | Compute and the Databricks service | Included in the DBU price |
| Pro | Databricks service | Billed separately by your cloud provider |
| Classic | Databricks service | Billed separately by your cloud provider |
For pro and classic warehouses, the Databricks invoice shows only part of the cost. The virtual machines appear on your cloud bill. Serverless shows one number, which makes comparisons simpler.
The exact DBU rates and dollar prices change over time and differ by region, cloud, and plan. Treat the doubling pattern as the working model and check the current pricing page for numbers.
The cost levers
Four settings control most of the spend. Most of them were covered in earlier chapters, so this table shows how each one affects cost.
| Lever | Effect on cost | Typical mistake |
|---|---|---|
| Auto stop | Limits idle billing | Leaving a long timeout on a warehouse used a few times a day |
| Size | Sets DBUs per hour per cluster | Sizing for the heaviest query that runs once a week |
| Max clusters | Caps concurrency scaling | Setting a high maximum with no review of peak usage |
| Type | Changes startup time and what the DBU price includes | Using a classic warehouse for interactive work that needs fast starts |
Monitoring a warehouse in the workspace
Three places in the workspace answer different questions.
The Monitoring tab
Open SQL Warehouses, select a warehouse, and open the Monitoring tab. It shows live and recent activity:
- Running and queued queries over time.
- Running clusters, which shows when autoscaling added or removed capacity.
- Peak queued queries, which shows whether queries are waiting for capacity.
Read the charts together. Queued queries while clusters stay at the maximum mean the warehouse is saturated. Queued queries while clusters are below the maximum may mean the warehouse is still scaling up. Long stretches with one cluster and no queries mean you are paying for idle time, so shorten auto stop.
Query history
Query history lists past statements with their user, warehouse, duration, and status. Filter by warehouse and time range to find the queries that kept a warehouse busy. Sort by duration and open the longest ones in the query profile, as covered in the previous chapter.
Warehouse events
The warehouse page also records starts, stops, scale-ups, and scale-downs. This helps explain a spike on the bill. For example, a scheduled dashboard refresh may restart a warehouse every hour and keep it running until auto stop.
Querying cost with system tables
The workspace charts show recent activity. For history and attribution, use system tables, which are read-only tables in the system catalog. Billing data is in system.billing.usage. An account admin must enable the schema and grant you SELECT on it before you can query it.
This query shows DBUs per warehouse per day for the last 30 days:
SELECT
usage_date,
usage_metadata.warehouse_id AS warehouse_id,
SUM(usage_quantity) AS dbus
FROM system.billing.usage
WHERE billing_origin_product = 'SQL'
AND usage_date >= date_sub(current_date(), 30)
GROUP BY usage_date, usage_metadata.warehouse_id
ORDER BY usage_date, dbus DESC;To turn DBUs into approximate dollars, join to system.billing.list_prices. This uses list prices, so negotiated discounts are not reflected.
SELECT
u.usage_date,
u.usage_metadata.warehouse_id AS warehouse_id,
SUM(u.usage_quantity) AS dbus,
ROUND(SUM(u.usage_quantity * p.pricing.default), 2) AS approx_list_cost
FROM system.billing.usage AS u
JOIN system.billing.list_prices AS p
ON u.sku_name = p.sku_name
AND u.usage_start_time >= p.price_start_time
AND (p.price_end_time IS NULL OR u.usage_end_time <= p.price_end_time)
WHERE u.billing_origin_product = 'SQL'
AND u.usage_date >= date_sub(current_date(), 30)
GROUP BY u.usage_date, u.usage_metadata.warehouse_id
ORDER BY u.usage_date, approx_list_cost DESC;Two limits apply. Usage rows can arrive with a delay, so today's numbers are incomplete. And this data covers Databricks charges only. For pro and classic warehouses, the cloud provider's VM cost is not in it.
Attributing cost with tags
Warehouse IDs are hard to read in a report. Add custom tags to a warehouse, such as team or project, in the warehouse settings. Tags are recorded on usage rows in the custom_tags column, so you can group cost by team:
SELECT
custom_tags['team'] AS team,
SUM(usage_quantity) AS dbus
FROM system.billing.usage
WHERE billing_origin_product = 'SQL'
AND usage_date >= date_sub(current_date(), 30)
GROUP BY custom_tags['team']
ORDER BY dbus DESC;Rows with no tag appear with a NULL team. A large NULL group means untagged warehouses and is worth fixing first. Tags also help when several teams share a workspace but not a warehouse. Sharing a warehouse makes per-team cost hard to separate, so give each team its own warehouse if you need clean attribution.
A review routine
A short, regular check catches most waste:
- Run the daily DBU query and look for warehouses with unexpected growth.
- Open the Monitoring tab for the top spenders and compare uptime to query activity.
- If a warehouse runs long with little activity, shorten auto stop.
- If queries queue while the warehouse is at maximum clusters, raise the maximum or move heavy workloads to a separate warehouse.
- If one cluster is busy and queries are slow, test the next size up and compare total cost, not only duration.
- Check that every warehouse has the tags your reports need.
Common errors
- Reading the Databricks bill as total cost. For pro and classic, add the cloud VM cost.
- Leaving a warehouse running after a test. Stop it, or confirm auto stop is set to a short value.
- Keeping scheduled refreshes against an idle warehouse. Each run restarts it and holds it up until auto stop. Group refreshes into one time window.
- Reading
system.billing.usagewithout thebilling_origin_productfilter. The table covers all Databricks products, so your totals will include jobs and other compute. - Blaming size for slowness. Use the query profile first. A skewed or poorly filtered query stays slow on a bigger warehouse and costs more.
Exercise
A pro warehouse with a Medium size, a maximum of 3 clusters, and a 60-minute auto stop is used for about 10 minutes of queries every 2 hours. The monitoring tab shows one cluster running for almost the whole day. Name two changes that would reduce cost and explain why each works. Then say what the Databricks bill alone will not show for this warehouse.
Show answer
Shorten auto stop to a few minutes, because the warehouse is billed while idle and now sits idle for about 50 of every 120 minutes before stopping. Alternatively, reduce the size, if the query profile shows the queries do not speed up on Medium, since each size step roughly doubles the DBU rate. Lowering the maximum clusters does not help here because only one cluster ran. A pro warehouse also incurs cloud provider VM charges, which appear on the cloud bill and not on the Databricks bill.
Exercise
Write a query that returns total SQL DBUs per warehouse for the last 7 days, largest first.
Show answer
SELECT usagemetadata.warehouseid AS warehouseid, SUM(usagequantity) AS dbus FROM system.billing.usage WHERE billingoriginproduct = 'SQL' AND usagedate >= datesub(currentdate(), 7) GROUP BY usagemetadata.warehouse_id ORDER BY dbus DESC;