在這個教學中,你會在 TPC-H 資料集上建立銷售分析指標視圖。 最後,你會得到一個度量視角:
- 利用雪花架構將訂單和客戶跨多個表格串連。
- 定義時間、地理和秩序屬性的欄位(也稱為維度)。
- 計算簡單與複雜的指標,包括比率、過濾聚合及視窗測量。
- 利用可組合性從較簡單的指標建立複雜的指標。
- 定義一個參數,以便在查詢時套用折現率,並在步驟 9 中將其移除。
- 包含供儀表板和 AI 工具使用的代理程式中繼資料。
- 在您於步驟 9 移除參數後,使用實體化來加速查詢。
如果你是度量視圖新手,可以從 「建立度量視圖 」開始學習基礎。 這篇教程在基礎上進一步擴展,涵蓋實際應用中的複雜性。
要求
若要完成本教學課程,您必須具備:
- 已啟用 Unity Catalog 的工作區。
- 運行 Databricks Runtime 17.3 或以上版本的 SQL 倉庫或運算資源。
- 已啟用無伺服器運算的工作區,以完成第 9 步。 您必須是該計量檢視的擁有者。
如需建立度量視圖所需的完整權限清單,請參閱 前置條件。
Note
Databricks Runtime 16.4 及以上版本支援建立度量檢視。 本教學使用的功能需要 Databricks 執行時 17.3 或以上,且部分步驟需支援較晚的執行環境。 關於每個功能的最小執行時間,請參見 「指標檢視功能可用性」。
資料模型
TPC-H 資料集模擬了一條批發供應鏈。 這個教學使用三個以雪花結構連接的表格:
-
orders與customer在o_custkey = c_custkey上聯結 -
customer加入至nation,在c_nationkey = n_nationkey上
| 資料表 | Role | 關鍵資料行 |
|---|---|---|
orders |
事實表(訂單交易) |
o_orderkey、、 o_custkey、 o_totalprice、 o_orderdate、 o_orderstatus |
customer |
維度表(客戶資料) |
c_custkey、c_name、c_mktsegment、c_nationkey |
nation |
尺寸表(國家或地區參考) |
n_nationkey、n_name、n_regionkey |
步驟 1:建立指標檢視並開啟編輯器
你可以在目錄總管介面中建立這個度量檢視,使用 Genie Code 產生,或直接撰寫完整的 YAML 定義。 這三種方法都解析為單一的 YAML 定義,以建模度量視圖。 在接下來的每個步驟中,選擇目錄 總管介面 或 YAML 編輯器 標籤,依照您偏好的方法操作。 如果你使用 YAML 編輯器,每個步驟中的範例程式碼都是 YAML 定義中對應於你在該步驟中建立內容的那一部分。
Note
本教學中的 YAML 範例使用 fields 關鍵字。 當你在低程式碼編輯器中建立度量視圖時,所產生的 YAML 會改為使用對應的 dimensions 關鍵字。 參見菲爾茲。
如果你不熟悉建立度量視圖的介面,請參閱 「建立度量視圖」。
若要建立度量檢視,請在 Catalog Explorer 中:
- 搜尋
samples.tpch.orders。 - 按一下表格名稱。
- 點選 「建立指標>檢視 」並命名檢視。
關於詳細的建立步驟,請參見 建立指標視圖。 編輯器開啟時,使用 介面 標籤進行互動式建置,或點擊 <> 按鈕直接編輯 YAML 定義。
步驟 2:設定指標檢視
為度量視圖設定版本和描述。
version 會決定 YAML 規範版本,而 comment 會說明度量檢視的用途,其內容會顯示於 Catalog Explorer 中。 Azure Databricks 會幫你管理這個版本。
目錄檔案總管使用者介面
這個版本已為你定義好。 儲存指標檢視後,若要新增或編輯描述:
- 在目錄總管中,搜尋該度量檢視並按一下其名稱。
- 點選 描述,然後輸入度量檢視的描述。 你可以使用 YAML編輯器 標籤中所示的範例描述。
此文字對應 comment 於 YAML 定義中的欄位。 如需更多編輯度量視圖的方法,請參見 「編輯度量視圖」。
YAML 編輯器
version: 1.1
comment: |-
Sales analytics metric view for order performance analysis.
Joins orders with customers and geography.
Owner: Analytics Team
Last updated: 2025-01-15
步驟 3:定義來源與聯結
定義主要來源資料表並連接相關資料表:
-
source將事實表(訂單)設定為粒度。 -
joins透過多對一關係導入客戶資料。 - 巢狀
nation連接展現了雪花式結構模式,透過連接customer來到達地理資料,其中國家是客戶的子維度。
目錄檔案總管使用者介面
此範例新增兩個 多對一 連接,用來建立雪花式綱要。
若要加入 customer 連接:
- 在編輯器中,點擊右上角的 「加入 」即可開啟 「新增加入 」對話框。
- 搜尋
samples.tpch.customer,按一下資料表名稱,然後按一下 新增。 - 將連接條件設為
o_custkey = c_custkey。 - 在 聯結基數下,選擇 多對一。 關於選擇基數的指引,請參見 「加入基數」。
然後加入巢狀 nation 連接。 重複執行從 customer 聯結開始的步驟,在 samples.tpch.nation 上聯結 c_nationkey = n_nationkey。 將聯結巢狀置於 customer 之下,會把 nation 建模為 customer 的子維度。
完整的連接對話步驟,請參見 步驟 2:新增連接。
YAML 編輯器
source: SELECT * FROM samples.tpch.orders
joins:
- name: customer
source: samples.tpch.customer
'on': o_custkey = c_custkey
joins:
- name: nation
source: samples.tpch.nation
'on': c_nationkey = n_nationkey
步驟 4:定義篩選器
A filter 限制來源資料,且適用於所有度量視圖上的查詢。 本教學將度量視角限制於近期資料。
目錄檔案總管使用者介面
定義濾波器:
- 在編輯器中,點擊
右上角的濾鏡。
- 使用下拉選單將 欄位 設為
o_orderdate,運算 子 設為>=, 值設為1995-01-01。
欲了解更多關於濾波器的資訊,請參見 步驟 3:定義濾波器。
YAML 編輯器
filter: o_orderdate >= '1995-01-01'
步驟 5:定義欄位
欄位是使用者群組和篩選的屬性。 欄位可以是類別欄位(如區域或狀態),或是使用者在查詢時彙整的未彙總數字欄位(如年齡或數量)。
代理程式中繼資料
本教學中的每個欄位和度量都包含代理程式中繼資料屬性,可改善你的指標檢視與儀表板及 AI 工具的搭配運作:
-
display_name: 可讀標籤,在視覺化中取代技術欄位名稱。 -
synonyms: 替代名稱,幫助 Genie 等 AI 工具透過自然語言查詢發現欄位與度量。 -
format: 數值在儀表板、筆記本及 SQL 查詢結果等下游介面中如何顯示,例如貨幣、數字或百分比。
這些屬性為可選,但建議使用。 以下步驟中的欄位和度量定義會以內嵌方式包含在其中。
欄位定義
這個教學新增:
-
時間欄位:
order_date、order_month以及order_year,以多種時間粒度滿足不同的分析需求。 -
轉換後的欄位:
order_status以及order_priority,這些欄位使用CASE與SPLIT將原始碼轉換為可讀標籤。 -
連接欄位:
customer_name、、market_segment及customer_nation,皆使用連接名稱來參考連接資料表。 巢狀連接欄位使用鏈點符號,例如customer.nation.n_name,來遍歷雪花結構。
目錄檔案總管使用者介面
編輯器會自動將所有來源欄位加入至 Fields 分頁。 編輯、重新命名、移除並新增欄位,讓度量檢視能定義以下內容。 對於每個欄位,按一下其名稱即可編輯,或按一下
新增 以建立欄位,然後在 建立器 或 自訂 模式中設定運算式。 如圖所示,為每個欄位設定 顯示名稱 和 同義詞 。
order_date:在 建構 者模式中,選取該
o_orderdate欄位。 將顯示名稱設為Order Date。order_month:在 自訂 模式中,輸入
DATE_TRUNC('MONTH', order_date)。 將顯示名稱設為Order Month。order_year:在 自訂 模式中輸入
YEAR(order_date)。 將顯示名稱設為Order Year。order_status:在 自訂 模式中,輸入以下表達式。 將顯示名稱設為
Order Status,同義詞設為status,fulfillment status。CASE o_orderstatus WHEN 'O' THEN 'Open' WHEN 'P' THEN 'Processing' WHEN 'F' THEN 'Fulfilled' ENDorder_priority:在 自訂 模式中輸入
SPLIT(o_orderpriority, '-')[0]。 將顯示名稱設為Priority。customer_name:在建立器模式中,從已連接的
c_name資料表中選取customer欄位。 將顯示名稱設為Customer Name。market_segment:在建構器模式中,從連接的
c_mktsegment資料表中選取customer欄位。 將顯示名稱設為Market Segment,同義詞設為segment,industry。customer_nation:在 自訂 模式中,請輸入
customer.nation.n_name以參照巢狀nation聯結。 將顯示名稱設為Country,同義詞設為nation,country。
完整欄位步驟請參見 步驟4:新增欄位。
YAML 編輯器
fields:
- name: order_date
expr: o_orderdate
display_name: Order Date
- name: order_month
expr: "DATE_TRUNC('MONTH', order_date)"
display_name: Order Month
- name: order_year
expr: YEAR(order_date)
display_name: Order Year
- name: order_status
expr: |-
CASE o_orderstatus
WHEN 'O' THEN 'Open'
WHEN 'P' THEN 'Processing'
WHEN 'F' THEN 'Fulfilled'
END
display_name: Order Status
synonyms:
- status
- fulfillment status
- name: order_priority
expr: "SPLIT(o_orderpriority, '-')[0]"
display_name: Priority
- name: customer_name
expr: customer.c_name
display_name: Customer Name
- name: market_segment
expr: customer.c_mktsegment
display_name: Market Segment
synonyms:
- segment
- industry
- name: customer_nation
expr: customer.nation.n_name
display_name: Country
synonyms:
- nation
- country
步驟 6:定義參數
參數讓你在查詢指標時能將值傳遞到度量視圖,因此單一定義可以服務多種查詢變體。 本教學加入了一個 discount 參數,供後續的量值用來計算折現營收。 該參數預設為 0,因此未傳遞值的查詢會返回未折扣的收益。 欲了解更多參數,請參見 「使用參數與度量視圖」。
目錄檔案總管使用者介面
在編輯器標題中,點選 「新增參數」。 輸入 discount 名稱,然後輸入預設值, 0 並選擇資料型別 double 。
YAML 編輯器
parameters:
- name: discount
data_type: double
default: 0
步驟 7:定義量值
衡量指標是使用者想要分析的計算。 先定義原子度量,然後利用可組合特性建立複雜度量,這些度量會參考先前由MEASURE()函數定義的度量。 如 display_name所述,為每個量值設定 format、synonyms 和 。 這個教學新增:
-
原子度量:
order_count、total_revenue和unique_customers,這些簡單的彙總構成了基本組件。 -
組合測度:
avg_order_value以及revenue_per_customer,其中使用MEASURE()參照先前定義的測度,而非重複聚合邏輯。 若total_revenue有變動,這些指標會自動使用更新後的定義。 請參閱 組合性。 -
已篩選的量值:
open_order_revenue和fulfilled_order_revenue,其使用FILTER (WHERE ...)來建立無須另外建立欄位的條件式指標。 -
參數化的 measure:
discounted_revenue,引用discount參數,以套用折現率。 請參見 「使用參數與度量視圖」。 -
視窗度量:
t7d_customers,計算 7 天滾動期間內的不重複客戶數。 更多窗戶度量模式請參見 窗戶度量 。
目錄檔案總管使用者介面
編輯器會自動加入範例 COUNT(*) 測度。 編輯或移除該項目,並新增測量,讓度量檢視明確定義為以下內容。 對每個度量點選
新增,然後在 建構 器或 自訂 模式中設定表達式。 請依圖中設定 顯示名稱、 格式與 同義 詞。 貨幣格式用小數點2位,數字用0位小數點。
-
order_count:在 建構 器模式中,選擇
o_orderkey的「相異計數」彙總。 將顯示名稱設為Order Count,格式改為 數字。 -
total_revenue:在建構器模式中,選擇 的
o_totalprice彙總。 將顯示名稱設為Total Revenue,格式為貨幣(USD),同義詞設為revenue。sales -
discounted_revenue:在 自訂 模式中,輸入
SUM(o_totalprice * (1 - discount))。 將顯示名稱設為Discounted Revenue,格式為貨幣(USD)。 -
unique_customers:在 Builder 模式中,在
o_custkey上選擇「相異計數」彙總。 將顯示名稱設為Unique Customers,格式改為 數字。 -
avg_order_value:在 自訂 模式中,輸入
MEASURE(total_revenue) / MEASURE(order_count)。 將顯示名稱設為Avg Order Value,格式為 貨幣(美元),同義詞設為AOV。 -
revenue_per_customer:在 自訂 模式中,輸入
MEASURE(total_revenue) / MEASURE(unique_customers)。 將顯示名稱設為Revenue per Customer,格式為貨幣(USD)。 -
open_order_revenue:在 自訂 模式中輸入
SUM(o_totalprice) FILTER (WHERE o_orderstatus = 'O')。 將顯示名稱設為Open Order Revenue,格式為 貨幣(美元),同義詞設為backlog。 -
fulfilled_order_revenue:在 自訂 模式中,輸入
SUM(o_totalprice) FILTER (WHERE o_orderstatus = 'F')。 將顯示名稱設為Fulfilled Revenue,格式為貨幣(USD)。 -
t7d_customers:在 自訂 模式中輸入
COUNT(DISTINCT o_custkey)。 然後按一下 + 視窗,並設定一個依order_date排序、範圍為trailing 7 day,且採用last半加法彙總的視窗。 將顯示名稱設為7-Day Rolling Customers,格式改為 數字。
完整測量步驟請參見 步驟5:新增測量。
YAML 編輯器
measures:
- name: order_count
expr: COUNT(DISTINCT o_orderkey)
display_name: Order Count
format:
type: number
decimal_places:
type: exact
places: 0
abbreviation: compact
- name: total_revenue
expr: SUM(o_totalprice)
display_name: Total Revenue
format:
type: currency
currency_code: USD
decimal_places:
type: exact
places: 2
abbreviation: compact
synonyms:
- revenue
- sales
- name: discounted_revenue
expr: SUM(o_totalprice * (1 - discount))
display_name: Discounted Revenue
format:
type: currency
currency_code: USD
decimal_places:
type: exact
places: 2
abbreviation: compact
- name: unique_customers
expr: COUNT(DISTINCT o_custkey)
display_name: Unique Customers
format:
type: number
decimal_places:
type: exact
places: 0
abbreviation: compact
- name: avg_order_value
expr: MEASURE(total_revenue) / MEASURE(order_count)
display_name: Avg Order Value
format:
type: currency
currency_code: USD
decimal_places:
type: exact
places: 2
abbreviation: compact
synonyms:
- AOV
- name: revenue_per_customer
expr: MEASURE(total_revenue) / MEASURE(unique_customers)
display_name: Revenue per Customer
format:
type: currency
currency_code: USD
decimal_places:
type: exact
places: 2
abbreviation: compact
- name: open_order_revenue
expr: SUM(o_totalprice) FILTER (WHERE o_orderstatus = 'O')
display_name: Open Order Revenue
format:
type: currency
currency_code: USD
decimal_places:
type: exact
places: 2
abbreviation: compact
synonyms:
- backlog
- name: fulfilled_order_revenue
expr: SUM(o_totalprice) FILTER (WHERE o_orderstatus = 'F')
display_name: Fulfilled Revenue
format:
type: currency
currency_code: USD
decimal_places:
type: exact
places: 2
abbreviation: compact
- name: t7d_customers
expr: COUNT(DISTINCT o_custkey)
window:
- order: order_date
range: trailing 7 day
display_name: 7-Day Rolling Customers
format:
type: number
decimal_places:
type: exact
places: 0
檢視完整定義
完成上述步驟後,你的度量視角會有以下完整定義:
查看完整的 YAML 定義
version: 1.1
parameters:
- name: discount
data_type: double
default: 0
source: SELECT * FROM samples.tpch.orders
joins:
- name: customer
source: samples.tpch.customer
'on': o_custkey = c_custkey
joins:
- name: nation
source: samples.tpch.nation
'on': c_nationkey = n_nationkey
filter: o_orderdate >= '1995-01-01'
comment: |-
Sales analytics metric view for order performance analysis.
Joins orders with customers and geography.
Owner: Analytics Team
Last updated: 2025-01-15
fields:
- name: order_date
expr: o_orderdate
display_name: Order Date
- name: order_month
expr: "DATE_TRUNC('MONTH', order_date)"
display_name: Order Month
- name: order_year
expr: YEAR(order_date)
display_name: Order Year
- name: order_status
expr: |-
CASE o_orderstatus
WHEN 'O' THEN 'Open'
WHEN 'P' THEN 'Processing'
WHEN 'F' THEN 'Fulfilled'
END
display_name: Order Status
synonyms:
- status
- fulfillment status
- name: order_priority
expr: "SPLIT(o_orderpriority, '-')[0]"
display_name: Priority
- name: customer_name
expr: customer.c_name
display_name: Customer Name
- name: market_segment
expr: customer.c_mktsegment
display_name: Market Segment
synonyms:
- segment
- industry
- name: customer_nation
expr: customer.nation.n_name
display_name: Country
synonyms:
- nation
- country
measures:
- name: order_count
expr: COUNT(DISTINCT o_orderkey)
display_name: Order Count
format:
type: number
decimal_places:
type: exact
places: 0
abbreviation: compact
- name: total_revenue
expr: SUM(o_totalprice)
display_name: Total Revenue
format:
type: currency
currency_code: USD
decimal_places:
type: exact
places: 2
abbreviation: compact
synonyms:
- revenue
- sales
- name: discounted_revenue
expr: SUM(o_totalprice * (1 - discount))
display_name: Discounted Revenue
format:
type: currency
currency_code: USD
decimal_places:
type: exact
places: 2
abbreviation: compact
- name: unique_customers
expr: COUNT(DISTINCT o_custkey)
display_name: Unique Customers
format:
type: number
decimal_places:
type: exact
places: 0
abbreviation: compact
- name: avg_order_value
expr: MEASURE(total_revenue) / MEASURE(order_count)
display_name: Avg Order Value
format:
type: currency
currency_code: USD
decimal_places:
type: exact
places: 2
abbreviation: compact
synonyms:
- AOV
- name: revenue_per_customer
expr: MEASURE(total_revenue) / MEASURE(unique_customers)
display_name: Revenue per Customer
format:
type: currency
currency_code: USD
decimal_places:
type: exact
places: 2
abbreviation: compact
- name: open_order_revenue
expr: SUM(o_totalprice) FILTER (WHERE o_orderstatus = 'O')
display_name: Open Order Revenue
format:
type: currency
currency_code: USD
decimal_places:
type: exact
places: 2
abbreviation: compact
synonyms:
- backlog
- name: fulfilled_order_revenue
expr: SUM(o_totalprice) FILTER (WHERE o_orderstatus = 'F')
display_name: Fulfilled Revenue
format:
type: currency
currency_code: USD
decimal_places:
type: exact
places: 2
abbreviation: compact
- name: t7d_customers
expr: COUNT(DISTINCT o_custkey)
window:
- order: order_date
range: trailing 7 day
display_name: 7-Day Rolling Customers
format:
type: number
decimal_places:
type: exact
places: 0
使用 SQL 建立指標檢視
如果你是在目錄檔案總管之外建立這個定義,請執行以下 SQL 來建立度量檢視:
CREATE OR REPLACE VIEW catalog.schema.tpch_sales_analytics
WITH METRICS
LANGUAGE YAML
AS $$
version: 1.1
parameters:
- name: discount
data_type: double
default: 0
source: SELECT * FROM samples.tpch.orders
joins:
- name: customer
source: samples.tpch.customer
'on': o_custkey = c_custkey
joins:
- name: nation
source: samples.tpch.nation
'on': c_nationkey = n_nationkey
filter: o_orderdate >= '1995-01-01'
comment: |-
Sales analytics metric view for order performance analysis.
Joins orders with customers and geography.
Owner: Analytics Team
Last updated: 2025-01-15
fields:
- name: order_date
expr: o_orderdate
display_name: Order Date
- name: order_month
expr: "DATE_TRUNC('MONTH', order_date)"
display_name: Order Month
- name: order_year
expr: YEAR(order_date)
display_name: Order Year
- name: order_status
expr: |-
CASE o_orderstatus
WHEN 'O' THEN 'Open'
WHEN 'P' THEN 'Processing'
WHEN 'F' THEN 'Fulfilled'
END
display_name: Order Status
synonyms:
- status
- fulfillment status
- name: order_priority
expr: "SPLIT(o_orderpriority, '-')[0]"
display_name: Priority
- name: customer_name
expr: customer.c_name
display_name: Customer Name
- name: market_segment
expr: customer.c_mktsegment
display_name: Market Segment
synonyms:
- segment
- industry
- name: customer_nation
expr: customer.nation.n_name
display_name: Country
synonyms:
- nation
- country
measures:
- name: order_count
expr: COUNT(DISTINCT o_orderkey)
display_name: Order Count
format:
type: number
decimal_places:
type: exact
places: 0
abbreviation: compact
- name: total_revenue
expr: SUM(o_totalprice)
display_name: Total Revenue
format:
type: currency
currency_code: USD
decimal_places:
type: exact
places: 2
abbreviation: compact
synonyms:
- revenue
- sales
- name: discounted_revenue
expr: SUM(o_totalprice * (1 - discount))
display_name: Discounted Revenue
format:
type: currency
currency_code: USD
decimal_places:
type: exact
places: 2
abbreviation: compact
- name: unique_customers
expr: COUNT(DISTINCT o_custkey)
display_name: Unique Customers
format:
type: number
decimal_places:
type: exact
places: 0
abbreviation: compact
- name: avg_order_value
expr: MEASURE(total_revenue) / MEASURE(order_count)
display_name: Avg Order Value
format:
type: currency
currency_code: USD
decimal_places:
type: exact
places: 2
abbreviation: compact
synonyms:
- AOV
- name: revenue_per_customer
expr: MEASURE(total_revenue) / MEASURE(unique_customers)
display_name: Revenue per Customer
format:
type: currency
currency_code: USD
decimal_places:
type: exact
places: 2
abbreviation: compact
- name: open_order_revenue
expr: SUM(o_totalprice) FILTER (WHERE o_orderstatus = 'O')
display_name: Open Order Revenue
format:
type: currency
currency_code: USD
decimal_places:
type: exact
places: 2
abbreviation: compact
synonyms:
- backlog
- name: fulfilled_order_revenue
expr: SUM(o_totalprice) FILTER (WHERE o_orderstatus = 'F')
display_name: Fulfilled Revenue
format:
type: currency
currency_code: USD
decimal_places:
type: exact
places: 2
abbreviation: compact
- name: t7d_customers
expr: COUNT(DISTINCT o_custkey)
window:
- order: order_date
range: trailing 7 day
display_name: 7-Day Rolling Customers
format:
type: number
decimal_places:
type: exact
places: 0
$$;
關於建立其他度量視圖的方法,請參見 建立度量視圖。
步驟 8:查詢您的指標檢視
使用商業友善的語法查詢度量視圖。 這個 MEASURE() 函數會依據你所選欄位的粒度彙總量值。
按維度彙總量值
此範例彙整了跨多個領域的測量。 它回傳總營收、訂單數及按客戶國家和市場區隔區劃分的平均訂單金額,並依營收最高排序:
SELECT
customer_nation,
market_segment,
MEASURE(total_revenue) AS total_revenue,
MEASURE(order_count) AS order_count,
MEASURE(avg_order_value) AS avg_order_value
FROM catalog.schema.tpch_sales_analytics
GROUP BY customer_nation, market_segment
ORDER BY total_revenue DESC;
分析每月趨勢
此範例結合時間域與衡量趨勢。 它會依月回傳總營收與未結訂單收入(待訂單)及訂單狀態:
SELECT
order_month,
order_status,
MEASURE(total_revenue) AS total_revenue,
MEASURE(open_order_revenue) AS open_order_revenue
FROM catalog.schema.tpch_sales_analytics
GROUP BY order_month, order_status
ORDER BY order_month;
傳入參數值
因為度量視圖定義了一個參數,你可以把它叫做一個以表格為值的函式,並在查詢時傳遞一個值。 以下查詢可享有10% 折扣。 由於 discount 預設值為 0,省略參數的查詢則返回未貼現的收入:
SELECT
customer_nation,
MEASURE(total_revenue) AS total_revenue,
MEASURE(discounted_revenue) AS discounted_revenue
FROM catalog.schema.tpch_sales_analytics(discount => 0.1)
GROUP BY customer_nation
ORDER BY discounted_revenue DESC;
步驟 9:新增物化以加快查詢速度
實體化會預先計算您的度量檢視中的部分內容,作為實體化檢視。 當您查詢指標檢視時,查詢優化器會自動重寫查詢,改為從匹配的實體化讀取,而非來源資料表。 你不會改變你的查詢。
此步驟會新增兩個實體化:
-
baseline(未聚合):將合併及篩選後的資料模型實體化,讓查詢每次執行時都不必重新合併orders、customer和nation。 任何在度量檢視上的查詢都可以從中讀取。 -
monthly_status_revenue(彙總): 預先計算依order_month和order_status分組的total_revenue與open_order_revenue,這與步驟 8 的 每月趨勢查詢 相符。 這兩種指標都是SUM聚合,因此這種具體化也能用於較粗略的查詢,例如total_revenue單獨分order_month組。
實體化需要一個啟用無伺服器運算的工作區。 有關所有需求,請參閱 「指標視圖的具體化」。
移除參數
你無法實現一個定義參數的度量視圖。 在新增實體化之前,請編輯度量檢視以移除 discount 該參數及其 discounted_revenue 參考的量度。 關於編輯步驟,請參閱「編輯度量檢視」。
移除參數後,傳遞參數值中的查詢將不再執行。 沒有其他指標參考 discounted_revenue,因此第 8 步中的其他查詢運作不變。
定義具體化
目錄檔案總管使用者介面
- 在目錄總管中,找到指標檢視,然後點擊 編輯 以開啟編輯器。
- 點選 「實體化」 標籤。
- 新增未彙總的具體化:
- 點擊
「新增」。 - 在「 實體化名稱」欄位輸入
baseline。 - 在「類型」中,選擇「未彙總」。
- 點擊
- 新增彙總的具體化:
- 點擊
「新增」。 - 在「 實體化名稱」欄位輸入
monthly_status_revenue。 - 在「類型」中,選擇「彙總」。
- 從 「欄位 」下拉選單中選取
order_month和order_status。 - 從 「度量」 下拉式選單中,選取
total_revenue和open_order_revenue。
- 點擊
- 點選 「排程」,將刷新間隔設定為每 6 小時一次,然後在對話框中點選 「儲存 」。
- 在頁面上按一下 「儲存」。
YAML 編輯器
刪除該 parameters: 區塊與該 discounted_revenue 測量,然後將以下頂層 materialization 區塊新增至指標檢視定義中。 它列出了下列的 dimensions欄位。
materialization:
schedule: every 6 hours
mode: relaxed
materialized_views:
- name: baseline
type: unaggregated
- name: monthly_status_revenue
type: aggregated
dimensions:
- order_month
- order_status
measures:
- total_revenue
- open_order_revenue
關於所有實體化欄位,請參閱Materialization。
當您首次新增實體化後儲存指標檢視時,Azure Databricks 會建立受管理管線並開始首次重新整理。 查詢重寫僅在該重新整理完成後才使用這些具體化結果。 在此之前,查詢會從來源資料表讀取。 如需按需重新整理,請參閱 「手動重新整理」。
monthly_status_revenue 不包含 order_count、 unique_customers或 t7d_customers。 這些度量會使用 COUNT(DISTINCT) 或一個視窗,因此無法從其他實體化中重新聚合。 對它們的查詢會讀取 baseline。 若要從彙總實體化中提供這些資料,請將它們包含在內,並與查詢的 GROUP BY 完全相符。
確認查詢是否使用實體化
第一次刷新完成後,請使用 EXPLAIN EXTENDED 執行每月趨勢查詢:
EXPLAIN EXTENDED
SELECT
order_month,
order_status,
MEASURE(total_revenue) AS total_revenue,
MEASURE(open_order_revenue) AS open_order_revenue
FROM catalog.schema.tpch_sales_analytics
GROUP BY order_month, order_status;
如果查詢使用具體化,計畫的葉節點包含 __materialization_mat_ 以及具體化的名稱 monthly_status_revenue。
依維度彙總量值中的查詢會按 monthly_status_revenue 未包含的欄位分組,因此會改為從 baseline 讀取。 如需其他檢查方式,請參閱 「驗證查詢是否使用物化檢視」。
檢視更新後的定義
完成步驟 9 後,您的指標檢視沒有參數、沒有 discounted_revenue 測量值,且有一個 materialization 區塊:
檢視更新後的 YAML 定義
version: 1.1
source: SELECT * FROM samples.tpch.orders
joins:
- name: customer
source: samples.tpch.customer
'on': o_custkey = c_custkey
joins:
- name: nation
source: samples.tpch.nation
'on': c_nationkey = n_nationkey
filter: o_orderdate >= '1995-01-01'
comment: |-
Sales analytics metric view for order performance analysis.
Joins orders with customers and geography.
Owner: Analytics Team
Last updated: 2025-01-15
fields:
- name: order_date
expr: o_orderdate
display_name: Order Date
- name: order_month
expr: "DATE_TRUNC('MONTH', order_date)"
display_name: Order Month
- name: order_year
expr: YEAR(order_date)
display_name: Order Year
- name: order_status
expr: |-
CASE o_orderstatus
WHEN 'O' THEN 'Open'
WHEN 'P' THEN 'Processing'
WHEN 'F' THEN 'Fulfilled'
END
display_name: Order Status
synonyms:
- status
- fulfillment status
- name: order_priority
expr: "SPLIT(o_orderpriority, '-')[0]"
display_name: Priority
- name: customer_name
expr: customer.c_name
display_name: Customer Name
- name: market_segment
expr: customer.c_mktsegment
display_name: Market Segment
synonyms:
- segment
- industry
- name: customer_nation
expr: customer.nation.n_name
display_name: Country
synonyms:
- nation
- country
measures:
- name: order_count
expr: COUNT(DISTINCT o_orderkey)
display_name: Order Count
format:
type: number
decimal_places:
type: exact
places: 0
abbreviation: compact
- name: total_revenue
expr: SUM(o_totalprice)
display_name: Total Revenue
format:
type: currency
currency_code: USD
decimal_places:
type: exact
places: 2
abbreviation: compact
synonyms:
- revenue
- sales
- name: unique_customers
expr: COUNT(DISTINCT o_custkey)
display_name: Unique Customers
format:
type: number
decimal_places:
type: exact
places: 0
abbreviation: compact
- name: avg_order_value
expr: MEASURE(total_revenue) / MEASURE(order_count)
display_name: Avg Order Value
format:
type: currency
currency_code: USD
decimal_places:
type: exact
places: 2
abbreviation: compact
synonyms:
- AOV
- name: revenue_per_customer
expr: MEASURE(total_revenue) / MEASURE(unique_customers)
display_name: Revenue per Customer
format:
type: currency
currency_code: USD
decimal_places:
type: exact
places: 2
abbreviation: compact
- name: open_order_revenue
expr: SUM(o_totalprice) FILTER (WHERE o_orderstatus = 'O')
display_name: Open Order Revenue
format:
type: currency
currency_code: USD
decimal_places:
type: exact
places: 2
abbreviation: compact
synonyms:
- backlog
- name: fulfilled_order_revenue
expr: SUM(o_totalprice) FILTER (WHERE o_orderstatus = 'F')
display_name: Fulfilled Revenue
format:
type: currency
currency_code: USD
decimal_places:
type: exact
places: 2
abbreviation: compact
- name: t7d_customers
expr: COUNT(DISTINCT o_custkey)
window:
- order: order_date
range: trailing 7 day
display_name: 7-Day Rolling Customers
format:
type: number
decimal_places:
type: exact
places: 0
materialization:
schedule: every 6 hours
mode: relaxed
materialized_views:
- name: baseline
type: unaggregated
- name: monthly_status_revenue
type: aggregated
dimensions:
- order_month
- order_status
measures:
- total_revenue
- open_order_revenue
你學到的
你建立了一個度量視圖,展示了:
| Feature | 範例 |
|---|---|
| Snowflake 結構聯結 | 從客戶到國家的訂單(嵌套的多對一關聯) |
| 時間欄位 | 日期、月份、年份的細緻度 |
| 轉換後的欄位 |
CASE 陳述式、 SPLIT 函數 |
| 簡單度量 |
COUNT,SUM |
| 可組合性 |
avg_order_value 並參考 revenue_per_customer 先前定義的量度,並使用以下方式 MEASURE() |
| 已篩選度量 |
篩選FILTER (WHERE ...)條件彙總 |
| 視窗量值 | 使用 7 天滾動客戶計數 trailing 7 day |
| 參數 |
discount 套用在 discounted_revenue 量測中的參數(在步驟 9 中移除) |
| 代理程式元資料 |
display_name, format, synonyms 關於欄位和量值 |
| 具體化 | 未彙總 baseline 與彙總 monthly_status_revenue 具體化檢視表,並具自動查詢重寫 |
其他資源
- 視窗度量 工具用於計算滾動平均值及年初至今總計。
- 為指標檢視實現實體化 ,以提升大型資料集的查詢效能。
- 為度量檢視選擇一種物化類型 ,以決定在彙總與非彙總物化之間做出決定。
- 使用指標檢視 來在 AI/BI 儀表板中使用你的指標檢視。