查詢 Microsoft Fabric HorizonDB

使用 PostgreSQL 查詢 Fabric HorizonDB 中的作業資料。 本文使用 horizon_sample sample-data 快速啟動所建立的結構。

Important

Fabric HorizonDB 目前處於公開預覽階段。 功能、限制與行為可能會在正式推出前發生變化。 如需更多資訊,請參閱 Microsoft Fabric 預覽功能。

Prerequisites

  • 一個 HorizonDB 資料庫。
  • 資料來自 Load 範例資料,或您自己類似的資料表。
  • 以資料庫建立者身分登入,這是目前預覽版中唯一可連線至 PostgreSQL 的身分。

檢查資料列

從有界且有序的結果開始:

SELECT
    order_reference,
    order_date,
    status,
    total
FROM horizon_sample.orders
ORDER BY order_date DESC, order_reference
LIMIT 100;

PostgreSQL 在沒有 ORDER BY 的情況下,不保證資料列順序。

聯結客戶與訂單

SELECT
    order_row.order_reference,
    customer.customer_name,
    customer.segment,
    order_row.order_date,
    order_row.status,
    order_row.total
FROM horizon_sample.orders AS order_row
JOIN horizon_sample.customers AS customer
  ON customer.customer_id = order_row.customer_id
ORDER BY order_row.order_reference;

模式限定使目標明確無歧義,並避免依賴 search_path。

以參數篩選

應用程式應將值作為驅動程式參數傳遞。 在互動式查詢中,您可以使用預先準備的語句:

PREPARE orders_by_status (text) AS
SELECT
    order_reference,
    order_date,
    total
FROM horizon_sample.orders
WHERE status = $1
ORDER BY order_date DESC;

EXECUTE orders_by_status ('open');

DEALLOCATE orders_by_status;

預備陳述式的存續期與工作階段綁定。 不要以為它能在暫停或中斷性轉換後存活。

彙總資料

SELECT
    customer.segment,
    count(order_row.order_id) AS order_count,
    coalesce(sum(order_row.total), 0) AS order_total,
    coalesce(avg(order_row.total), 0) AS average_order_total
FROM horizon_sample.customers AS customer
LEFT JOIN horizon_sample.orders AS order_row
  ON order_row.customer_id = customer.customer_id
GROUP BY customer.segment
ORDER BY customer.segment;

左連接即使客戶沒有訂單,也會在結果中保留一個區段。

分析查詢計畫

使用 EXPLAIN 來檢查計畫的操作,而無需執行查詢:

EXPLAIN (COSTS, VERBOSE)
SELECT
    order_reference,
    order_date,
    total
FROM horizon_sample.orders
WHERE customer_id = 1
ORDER BY order_date DESC;

請謹慎使用 EXPLAIN (ANALYZE, BUFFERS) ,因為它能執行該語句。 除非你是刻意想要這項變更,並將其包在適當的事務中,否則不要在資料變更陳述式上使用 ANALYZE。

在事務中更新資料

BEGIN;

UPDATE horizon_sample.orders
SET status = 'processing'
WHERE order_reference = 'ORD-1001'
  AND status = 'open'
RETURNING order_reference, status;

COMMIT;

狀態謂詞使轉換成為有條件的。 如果指令未回傳列,表示可能已有其他工作階段或先前的執行已更改了該訂單。

對於可重試的應用程式工作流程,請使用穩定的業務金鑰與冪等狀態轉換。 在提交過程中失去連線後,不要盲目重新執行交易。

查詢中繼資料

尋找客戶表:

SELECT
    schemaname AS schema_name,
    tablename AS table_name,
    tableowner AS table_owner
FROM pg_catalog.pg_tables
WHERE schemaname NOT IN ('pg_catalog', 'information_schema')
ORDER BY schemaname, tablename;

找出最大的客戶資料表:

SELECT
    statistics.schemaname AS schema_name,
    statistics.relname AS table_name,
    pg_total_relation_size(statistics.relid) AS total_bytes,
    pg_size_pretty(pg_total_relation_size(statistics.relid)) AS total_size
FROM pg_catalog.pg_statio_user_tables AS statistics
ORDER BY total_bytes DESC
LIMIT 20;

排解查詢結果

徵兆 支票
沒有排隊 確認篩選條件、交易可見性,以及範例資料是否已提交。
關聯不存在 確認資料庫、架構、名稱及引號識別碼大小寫。
權限遭到拒絕 確認您是資料庫建立者,檢查物件所有權,並確認該作業是否受支援。
查詢被封鎖 檢查pg_stat_activity和pg_blocking_pids(),若允許的話。
資料與 SQL 分析端點不同 鏡像是非同步的;比較時間戳記與鏡像狀態。
閒置時間後連線失敗 在資料庫恢復時以有限制重試方式重新連線。

下一步

透過 Python 連線。