Sub topic 3
Working with Results, History, and Automation
Assume you know basic SQL and can use the Databricks workspace. This chapter focuses on turning query results into reusable analysis, then finding and automating that analysis.
Results and visualizations
After a query finishes, the result table shows returned rows and columns. Use the result controls to sort, filter, download, or create a visualization. A visualization maps result columns to fields such as an x-axis, y-axis, grouping, or series.
For example, a grouped query produces data that works well in a bar chart:
SELECT
pickup_zip,
COUNT(*) AS trips,
ROUND(AVG(fare_amount), 2) AS avg_fare
FROM samples.nyctaxi.trips
GROUP BY pickup_zip
ORDER BY trips DESC
LIMIT 20;A visualization is based on the current result, so change the query when you need different rows or measures.
Saving and sharing queries
Save a query with a descriptive name and organize it in a workspace folder. Sharing a query does not automatically grant access to its tables or warehouse. Users need the required permissions for both. Share the smallest access level that meets the team’s needs.
Query history and profile
Query history records executed statements and their status, duration, warehouse, and user. Open a query profile to inspect stages such as scanning, joining, aggregating, and writing results. Look for stages that read much more data or take much longer than others.
Performance, cost, and monitoring
Caching can make repeated queries faster, but it should not hide inefficient SQL. Reduce scanned data with selective filters, appropriate joins, and only the columns you need. Monitor warehouse utilization, query duration, concurrency, and running costs. Auto stop limits idle compute cost, while larger or more clusters improve concurrency at higher cost.
Dashboards, alerts, and programmatic access
Add visualizations to dashboards to share recurring metrics. Alerts can notify people when a query result crosses a configured threshold. For applications or scheduled jobs, submit SQL through the Statement Execution API:
curl -X POST "$DATABRICKS_HOST/api/2.0/sql/statements" \
-H "Authorization: Bearer $DATABRICKS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"warehouse_id":"'"$WAREHOUSE_ID"'","statement":"SELECT COUNT(*) FROM samples.nyctaxi.trips","wait_timeout":"30s"}'Exercise
A query is slow, and its result feeds a dashboard. What should you inspect first?
Show answer
Open query history and the query profile. Identify the slowest stage and check whether it scans unnecessary data or waits for warehouse capacity.