OLAP Cube设计

数据建模入门数据建模

问题

OLAP Cube是什么?如何设计多维数据集?

答题要点

OLAP Cube预计算多维聚合:

Cube概念

3维Cube示例:
           Product
              ↑
              │
              │
        ┌─────┼─────┐
        │     │     │
   Time │  Sales   │ Region
        │   Cube   │
        │     │     │
        └─────┼─────┘
              │
              ↓

SQL Server Analysis Services (SSAS) Cube定义

<!-- Cube定义(简化XML) -->
<Cube Name="SalesCube">
  <Dimensions>
    <Dimension Name="Date">
      <Hierarchies>
        <Hierarchy Name="Calendar">
          <Levels>
            <Level Name="Year" Column="year"/>
            <Level Name="Quarter" Column="quarter"/>
            <Level Name="Month" Column="month"/>
            <Level Name="Day" Column="day"/>
          </Levels>
        </Hierarchy>
      </Hierarchies>
    </Dimension>

    <Dimension Name="Product">
      <Hierarchies>
        <Hierarchy Name="Product Category">
          <Levels>
            <Level Name="Category" Column="category"/>
            <Level Name="Subcategory" Column="subcategory"/>
            <Level Name="Product" Column="product_name"/>
          </Levels>
        </Hierarchy>
      </Hierarchies>
    </Dimension>

    <Dimension Name="Geography">
      <Hierarchies>
        <Hierarchy Name="Location">
          <Levels>
            <Level Name="Region" Column="region"/>
            <Level Name="City" Column="city"/>
            <Level Name="Store" Column="store_name"/>
          </Levels>
        </Hierarchy>
      </Hierarchies>
    </Dimension>
  </Dimensions>

  <MeasureGroups>
    <MeasureGroup Name="Sales">
      <Measures>
        <Measure Name="Sales Amount" AggregateFunction="Sum"/>
        <Measure Name="Order Count" AggregateFunction="Count"/>
        <Measure Name="Avg Order Value" AggregateFunction="Average"/>
      </Measures>
    </MeasureGroup>
  </MeasureGroups>
</Cube>

开源替代: Apache Kylin预计算Cube

-- Kylin Model定义(JSON)
{
  "name": "sales_model",
  "fact_table": "fact_sales",
  "dimensions": [
    {
      "table": "dim_date",
      "columns": ["year", "quarter", "month", "day"]
    },
    {
      "table": "dim_product",
      "columns": ["category", "subcategory", "product_id"]
    },
    {
      "table": "dim_geography",
      "columns": ["region", "city", "store_id"]
    }
  ],
  "measures": [
    {
      "name": "sales_amount",
      "function": "SUM",
      "parameter": "sales_amount"
    },
    {
      "name": "order_count",
      "function": "COUNT",
      "parameter": "order_id"
    }
  ]
}

-- Kylin自动预计算所有维度组合的聚合
-- 查询时直接从预计算结果返回,毫秒级响应
SELECT
    d.year,
    p.category,
    g.region,
    SUM(f.sales_amount) AS total_sales
FROM fact_sales f
JOIN dim_date d ON f.date_key = d.date_key
JOIN dim_product p ON f.product_key = p.product_key
JOIN dim_geography g ON f.geography_key = g.geography_key
WHERE d.year = 2024
GROUP BY d.year, p.category, g.region;
-- Kylin直接查询Cube,无需实时聚合

SQL聚合表模拟Cube

-- 预计算不同维度组合的聚合表
-- 年-产品-地区
CREATE TABLE agg_year_product_region AS
SELECT
    d.year,
    p.category,
    g.region,
    SUM(f.sales_amount) AS total_sales,
    COUNT(f.order_id) AS order_count
FROM fact_sales f
JOIN dim_date d ON f.date_key = d.date_key
JOIN dim_product p ON f.product_key = p.product_key
JOIN dim_geography g ON f.geography_key = g.geography_key
GROUP BY d.year, p.category, g.region;

-- 月-产品
CREATE TABLE agg_month_product AS
SELECT
    d.year,
    d.month,
    p.product_id,
    SUM(f.sales_amount) AS total_sales
FROM fact_sales f
JOIN dim_date d ON f.date_key = d.date_key
JOIN dim_product p ON f.product_key = p.product_key
GROUP BY d.year, d.month, p.product_id;

-- 查询改写:使用聚合表
SELECT year, category, SUM(total_sales)
FROM agg_year_product_region
WHERE year = 2024
GROUP BY year, category;
-- 直接查聚合表,无需JOIN原始事实表

完整解析在题库里看

核心知识点、易错点和面试官想听到什么;练过的题会进入记录,答错的进错题本。

去练这道题

回知识库看这一块

同一能力的其他题