|
| 1 | +# System tables |
| 2 | + |
| 3 | +## What Are Databricks System Tables? |
| 4 | + |
| 5 | +Located within the system catalog, Databricks system tables are a collection of |
| 6 | +metadata repositories that serve as the backbone for analyzing various aspects |
| 7 | +of your Databricks environment. |
| 8 | +They help monitor performance, track resource consumption, and analyze activity logs. |
| 9 | + |
| 10 | +Importantly, these tables offer users the ability to: |
| 11 | + |
| 12 | +- Track the utilization of Databricks services like SQL Warehouses, Unity Catalog, Notebooks, Jobs, and Delta Live Tables. |
| 13 | +- Gain detailed insights into resource consumption and billing to optimize costs effectively. |
| 14 | +- Establish traceability for data transformations at both the table and column levels. |
| 15 | +- Review operations initiated by Databricks, such as maintenance tasks. |
| 16 | + |
| 17 | +## Real-World Use Cases and Practical Examples |
| 18 | + |
| 19 | +### Auditing User Activities |
| 20 | + |
| 21 | +To maintain a secure environment, tracking user actions is essential. |
| 22 | +The `system.access.audit` table provides a detailed log of user events, |
| 23 | +such as SQL executions and data access. |
| 24 | + |
| 25 | +=== "Example Query: Identifying Resource-Intensive Queries" |
| 26 | + |
| 27 | + ```sql |
| 28 | + SELECT user_email, query_text, execution_time, rows_read |
| 29 | + FROM system.access.audit |
| 30 | + WHERE event_type = 'query' AND rows_read > 1000000 |
| 31 | + ORDER BY execution_time DESC |
| 32 | + LIMIT 10; |
| 33 | + ``` |
| 34 | + |
| 35 | +### Tracing Data Lineage |
| 36 | + |
| 37 | +Understanding data flow is crucial for compliance and integrity. |
| 38 | +The `system.access.table_lineage` and `system.access.column_lineage` tables track |
| 39 | +transformations and relationships between datasets. |
| 40 | + |
| 41 | +=== "Example Query: Tracking Table Dependencies" |
| 42 | + |
| 43 | + ```sql |
| 44 | + SELECT source_table_name, target_table_name, operation |
| 45 | + FROM system.access.table_lineage |
| 46 | + WHERE target_table_name = 'customer_analytics'; |
| 47 | + ``` |
| 48 | + |
| 49 | +### Managing Costs Effectively |
| 50 | + |
| 51 | +The `system.billing.usage` table simplifies cost analysis, helping organizations |
| 52 | +allocate budgets wisely and identify inefficiencies. |
| 53 | + |
| 54 | +=== "Example Query: Monthly Cost Analysis" |
| 55 | + |
| 56 | + ```sql |
| 57 | + WITH usage_costs AS ( |
| 58 | + SELECT |
| 59 | + u.workspace_id, |
| 60 | + u.sku_name, |
| 61 | + u.usage_date, |
| 62 | + DATE_FORMAT(u.usage_date, 'yyyy-MM') AS YearMonth, |
| 63 | + u.usage_quantity, |
| 64 | + lp.pricing.default AS list_price, |
| 65 | + lp.pricing.default * u.usage_quantity AS list_cost, |
| 66 | + COALESCE(u.usage_metadata.job_id, u.usage_metadata.dlt_pipeline_id, u.usage_metadata.warehouse_id, u.usage_metadata.notebook_id) AS resource_id |
| 67 | + FROM |
| 68 | + system.billing.usage u |
| 69 | + INNER JOIN system.billing.list_prices lp |
| 70 | + ON u.cloud = lp.cloud |
| 71 | + AND u.sku_name = lp.sku_name |
| 72 | + AND u.usage_start_time >= lp.price_start_time |
| 73 | + AND (u.usage_end_time <= lp.price_end_time OR lp.price_end_time IS NULL) |
| 74 | + WHERE u.usage_start_time >= '2024-02-01' |
| 75 | + ) |
| 76 | + SELECT usage_type, resource_id, SUM(usage_quantity) AS quantity, SUM(list_cost) AS cost |
| 77 | + FROM usage_costs |
| 78 | + GROUP BY usage_type, resource_id |
| 79 | + ORDER BY cost DESC |
| 80 | + LIMIT 20; |
| 81 | + ``` |
| 82 | + |
| 83 | +### Monitoring Compute Resources |
| 84 | + |
| 85 | +The `system.compute.clusters` table provides insights into cluster activity, |
| 86 | +enabling better resource management. |
| 87 | + |
| 88 | +=== "Example Query: Recently Terminated Clusters" |
| 89 | + |
| 90 | + ```sql |
| 91 | + SELECT cluster_name, terminated_time, termination_reason |
| 92 | + FROM system.compute.clusters |
| 93 | + WHERE terminated_time IS NOT NULL |
| 94 | + ORDER BY terminated_time DESC |
| 95 | + LIMIT 5; |
| 96 | + ``` |
| 97 | + |
| 98 | +## Conclusion |
| 99 | + |
| 100 | +Databricks system tables offer unparalleled opportunities for monitoring and |
| 101 | +optimization. |
| 102 | +By combining metadata analysis with actionable insights, these tables help users |
| 103 | +maintain efficiency, control costs, and ensure compliance. |
| 104 | +Leverage the sample queries and schemas provided to unlock the full potential |
| 105 | +of your Databricks environment. |
| 106 | + |
| 107 | +## References |
| 108 | + |
| 109 | +- [Databricks System Tables for Monitoring and Optimization](https://medium.com/towards-data-engineering/databricks-system-tables-for-monitoring-and-optimization-37267e723ede) |
0 commit comments