数据建模性能优化
数据建模入门数据建模BI 与可视化
问题
BI查询性能不佳时,数据建模层面有哪些优化手段?
答题要点
性能优化从模型设计、索引、物化、分区多方面入手:
-- 1. 宽表反范式化(减少JOIN)
CREATE TABLE dm_customer_order_wide AS
SELECT
c.customer_id,
c.customer_name,
c.region,
c.segment,
o.order_id,
o.order_date,
o.sales_amount,
p.product_name,
p.category
FROM dim_customer c
JOIN fact_sales o ON c.customer_id = o.customer_id
JOIN dim_product p ON o.product_id = p.product_id;
-- 2. 预聚合表(减少实时计算)
CREATE TABLE agg_daily_sales AS
SELECT
order_date,
region,
category,
SUM(sales_amount) AS total_sales,
COUNT(DISTINCT customer_id) AS customer_count,
COUNT(order_id) AS order_count
FROM dm_customer_order_wide
GROUP BY order_date, region, category;
-- 3. 分区表(减少扫描范围)
CREATE TABLE fact_sales_partitioned (
order_id BIGINT,
order_date DATE,
sales_amount DECIMAL(12,2)
)
PARTITION BY RANGE (order_date) (
PARTITION p2023 VALUES LESS THAN ('2024-01-01'),
PARTITION p2024_q1 VALUES LESS THAN ('2024-04-01'),
PARTITION p2024_q2 VALUES LESS THAN ('2024-07-01')
);
-- 4. 覆盖索引(避免回表)
CREATE INDEX idx_sales_covering ON fact_sales(customer_id, order_date)
INCLUDE (sales_amount, quantity); -- PostgreSQL 11+
-- 5. 列式存储物化视图(PostgreSQL + cstore_fdw)
-- 或使用ClickHouse、Druid等列式数据库
完整解析在题库里看
核心知识点、易错点和面试官想听到什么;练过的题会进入记录,答错的进错题本。
去练这道题