仓库事件系统表引用

重要

此功能目前以公共预览版提供。

本文介绍如何使用仓库事件系统表监视和管理工作区中的 SQL 仓库。 每次仓库启动、停止、运行、纵向扩展和纵向缩减时,此表都会记录一行。 你可以将本文中的示例查询与警报搭配使用,以便及时了解仓库的变化情况。

仓库事件系统表位于 system.compute.warehouse_events

记录的仓库事件类型

此系统表会记录以下类型的事件:

  • SCALED_UP:一个新的群集已添加到仓库。
  • SCALED_DOWN:一个群集已从仓库中移除。
  • STOPPING:仓库正在停止。
  • RUNNING:仓库正在积极运行。
  • STARTING:仓库正在启动。
  • STOPPED:仓库已完全停止运行。

仓库事件架构

warehouse_events 系统表使用以下架构:

列名称 数据类型 说明 示例
account_id 字符串 Azure Databricks 帐户的 ID。 7af234db-66d7-4db3-bbf0-956098224879
workspace_id string 部署仓库的工作区的 ID。 123456789012345
warehouse_id string 事件相关的 SQL 仓库的 ID。 123456789012345
event_type string 仓库事件的类型。 可能值为 SCALED_UPSCALED_DOWNSTOPPINGRUNNINGSTARTING 以及 STOPPED SCALED_UP
cluster_count integer 正在积极运行的群集数。 2
event_time timestamp 事件发生时的时间戳。 2023-07-20T19:13:09.504Z

示例查询

以下示例查询是模板。 插入任何对组织有意义的值。 还可以向这些查询添加警报,以便及时了解仓库的变化情况。 请参阅创建警报

使用以下示例查询来深入了解仓库行为:

哪些仓库正在积极运行?运行时间有多长?

此查询将确定当前处于活动状态的仓库及其运行时间(以小时为单位)。

USE CATALOG `system`;

SELECT
we.warehouse_id,
we.event_time,
TIMESTAMPDIFF(MINUTE, we.event_time, CURRENT_TIMESTAMP()) / 60.0 AS running_hours,
we.cluster_count
FROM
compute.warehouse_events we
WHERE
we.event_type = 'RUNNING'
AND NOT EXISTS (
SELECT 1
FROM compute.warehouse_events we2
WHERE we2.warehouse_id = we.warehouse_id
AND we2.event_time > we.event_time
)

警报机会:作为工作区管理员,你可能需要在仓库运行时间超过预期时获得警报。 例如,你可以使用查询结果来设置将在运行时间超过特定阈值时触发的警报条件。

确定纵向扩展时间超出预期的仓库

此查询将确定当前处于活动状态的仓库及其运行时间(以小时为单位)。

use catalog `system`;

SELECT
   we.warehouse_id,
   we.event_time,
   TIMESTAMPDIFF(MINUTE, we.event_time, CURRENT_TIMESTAMP()) / 60.0 AS upscaled_hours,
   we.cluster_count
FROM
   compute.warehouse_events we
WHERE
   we.event_type = 'SCALED_UP'
   AND we.cluster_count >= 2
   AND NOT EXISTS (
       SELECT 1
       FROM compute.warehouse_events we2
       WHERE we2.warehouse_id = we.warehouse_id
       AND (
           (we2.event_type = 'SCALED_DOWN') OR
           (we2.event_type = 'SCALED_UP' AND we2.cluster_count < 2)
       )
       AND we2.event_time > we.event_time
   )

警报机会:在此条件下发出警报有助于监视资源和成本。 你可以设置一个在纵向扩展小时数超出特定限制时发出的警报。

首次启动的仓库

此查询会告知你首次启动的新仓库。

use catalog `system`;

SELECT
   we.warehouse_id,
   we.event_time,
   we.cluster_count
FROM
   compute.warehouse_events we
WHERE
   (we.event_type = 'STARTING' OR we.event_type = 'RUNNING')
   AND NOT EXISTS (
       SELECT 1
       FROM compute.warehouse_events we2
       WHERE we2.warehouse_id = we.warehouse_id
       AND we2.event_time < we.event_time
   )

警报机会:针对新仓库发出警报可帮助组织跟踪资源分配。 例如,可以设置每次新仓库启动时触发的警报。

调查计费费用

如果想要具体了解产生计费费用时仓库的行为,此查询可以用于了解仓库纵向扩展或缩减以及启动或停止的确切日期和时间。

use catalog `system`;

SELECT
    we.warehouse_id AS warehouse_id,
    we.event_type AS event,
    we.event_time AS event_time,
    we.cluster_count AS cluster_count
FROM
    compute.warehouse_events AS we
WHERE
    we.event_type IN (
        'STARTING', 'RUNNING', 'STOPPING', 'STOPPED',
        'SCALING_UP', 'SCALED_UP', 'SCALING_DOWN', 'SCALED_DOWN'
    )
    AND MONTH(we.event_time) = 7
    AND YEAR(we.event_time) = YEAR(CURRENT_DATE())
    AND we.warehouse_id = '19c9d68652189278'
ORDER BY
    event_time DESC

过去 30 天内未使用哪些仓库?

此查询可帮助你确定未使用的资源,从而提供成本优化的机会。

use catalog `system`;

SELECT
    we.warehouse_id,
    we.event_time,
    we.event_type,
    we.cluster_count
FROM
    compute.warehouse_events AS we
WHERE
    we.warehouse_id IN (
        SELECT DISTINCT
            warehouse_id
        FROM
            compute.warehouse_events
        WHERE
            MONTH(event_time) = 6
            AND YEAR(event_time) = YEAR(CURRENT_DATE())
    )
    AND we.warehouse_id NOT IN (
        SELECT DISTINCT
            warehouse_id
        FROM
            compute.warehouse_events
        WHERE
            MONTH(event_time) = 7
            AND YEAR(event_time) = YEAR(CURRENT_DATE())
    )
ORDER BY
    event_time DESC

警报机会:就未使用的资源接收警报可帮助组织优化成本。 例如,可以设置在查询检测到未使用的仓库时触发的警报。

一个月内运行时间最多的仓库

此查询显示在特定月份使用最多的仓库。 此查询以 7 月为例。

use catalog `system`;

SELECT
   warehouse_id,
   SUM(TIMESTAMPDIFF(MINUTE, start_time, end_time)) / 60.0 AS uptime_hours
FROM (
   SELECT
      starting.warehouse_id,
      starting.event_time AS start_time,
      (
         SELECT
            MIN(stopping.event_time)
         FROM
            compute.warehouse_events AS stopping
         WHERE
            stopping.warehouse_id = starting.warehouse_id
            AND stopping.event_type = 'STOPPED'
            AND stopping.event_time > starting.event_time
      ) AS end_time
   FROM
      compute.warehouse_events AS starting
   WHERE
      starting.event_type = 'STARTING'
      AND MONTH(starting.event_time) = 7
      AND YEAR(starting.event_time) = YEAR(CURRENT_DATE())
) AS warehouse_uptime
WHERE
   end_time IS NOT NULL
GROUP BY
   warehouse_id
ORDER BY
   uptime_hours DESC

警报机会:你可能需要跟踪高利用率仓库。 例如,可以设置在仓库的运行时间超过特定阈值时触发的警报。

一个月内花费最多时间进行纵向扩展的仓库

此查询会展示一个月内大量时间处于纵向扩展状态的仓库。 此查询以 7 月为例。

use catalog `system`;

SELECT
   warehouse_id,
   SUM(TIMESTAMPDIFF(MINUTE, upscaled_time, downscaled_time)) / 60.0 AS upscaled_hours
FROM (
   SELECT
      upscaled.warehouse_id,
      upscaled.event_time AS upscaled_time,
      (
         SELECT
            MIN(downscaled.event_time)
         FROM
            compute.warehouse_events AS downscaled
         WHERE
            downscaled.warehouse_id = upscaled.warehouse_id
            AND (downscaled.event_type = 'SCALED_DOWN' OR downscaled.event_type = 'STOPPED')
            AND downscaled.event_time > upscaled.event_time
      ) AS downscaled_time
   FROM
      compute.warehouse_events AS upscaled
   WHERE
      upscaled.event_type = 'SCALED_UP'
      AND upscaled.cluster_count >= 2
      AND MONTH(upscaled.event_time) = 7
      AND YEAR(upscaled.event_time) = YEAR(CURRENT_DATE())
) AS warehouse_upscaled
WHERE
   downscaled_time IS NOT NULL
GROUP BY
   warehouse_id
ORDER BY
   upscaled_hours DESC limit 0;

警报机会:你可能需要跟踪高利用率仓库。 例如,可以设置在仓库的运行时间超过特定阈值时触发的警报。

仓库运行时间(按日期)

此查询会展示仓库运行的总分钟数(按日期排列)。 此查询可用作预测的基础。

use catalog `system`;

SELECT
   warehouse_id,
   SUM(TIMESTAMPDIFF(MINUTE, upscaled_time, downscaled_time)) / 60.0 AS upscaled_hours
FROM (
   SELECT
      upscaled.warehouse_id,
      upscaled.event_time AS upscaled_time,
      (
         SELECT
            MIN(downscaled.event_time)
         FROM
            compute.warehouse_events AS downscaled
         WHERE
            downscaled.warehouse_id = upscaled.warehouse_id
            AND (downscaled.event_type = 'SCALED_DOWN' OR downscaled.event_type = 'STOPPED')
            AND downscaled.event_time > upscaled.event_time
      ) AS downscaled_time
   FROM
      compute.warehouse_events AS upscaled
   WHERE
      upscaled.event_type = 'SCALED_UP'
      AND upscaled.cluster_count >= 2
      AND MONTH(upscaled.event_time) = 7
      AND YEAR(upscaled.event_time) = YEAR(CURRENT_DATE())
) AS warehouse_upscaled
WHERE
   downscaled_time IS NOT NULL
GROUP BY
   warehouse_id
ORDER BY
   upscaled_hours DESC limit 0;