search
HomeDatabaseMysql Tutorial BI笔记之---增量方式处理多维数据集

本文将模拟一个数据仓库系统,其中有用户数据,产品数据以及订单数据。根据这些数据结构建立多维数据集,并且以增量更新的方式对其进行处理。之所以强调增量的方

本文将模拟一个数据仓库系统,其中有用户数据,产品数据以及订单数据。根据这些数据结构建立多维数据集,并且以增量更新的方式对其进行处理。

之所以强调增量的方式,是考虑到事实表中数据的增长,假设以后增长到几十亿,全量处理就变得很不现实,所以方案中着重演示以增量方式处理多维数据集的方案。

增量处理多维数据集的关键是要将事实数据分为两部分处理,一个是增量事实表,一个是历史事实表,多维数据集第一次处理历史事实表中的数据,以后每次周期性的处理都是处理增量表中的数据。

本文中提及的SQLServer和Visual Studio都是2008版本,2005版本同样也适用。

数据假设:一张用户表,一张产品表,一张订单表,订单里记录的是谁买了什么。多维数据集统计的需求就是根据订单统计谁买过什么。

首先,建立数据仓库,在数据引擎下新建BIDemo库。

clip_image002

接下来建立用户表,结构如下:

此外还有产品表:

以及历史订单表和建立增量订单表,它们的结构是一样的:

clip_image008

为了测试方便,香港空间,我们在用户表中加入一些测试数据:

clip_image010

然后在产品表中加入一些测试数据

clip_image012

至于事实表,手动加入测试数据就不现实了,所以这里写了一个程序利用随机数来灌测试数据:

clip_image014

这个程序的代码可以在本文中找到。生成后的数据基本如下所示:

clip_image016

到此,测试的数据结构以及数据就已经准备好了,相当于有了一个小型的数据仓库。

接下来在Visual Studio中建立BI解决方案,方案下分为一个SSIS项目和一个SSAS项目。

在SSAS项目下建立数据源和数据源视图,这里需要注意的是,事实表用历史表,而不是增量表,尽管其还没有数据。

首先建立数据源,连接刚才建立的数据库,并且在数据源视图里定义好关系,虚拟主机,如下图:

clip_image018

然后,根据此数据源视图建立多维数据集,需要注意的是,度量选择历史表,维度选择用户和产品两个表。

最后,部署多维数据集。这里只部署就可以了,不需要处理,处理任务将在以后的SSIS包中处理。

下面来看SSIS项目。在SSIS包里建立四个任务模块,类型分别如下:

clip_image020

前两个多维数据集处理模块是用来处理多维数据集的,数据流负责把增量事实表的数据导到历史事实表中,最后执行一个SQL任务把增量表中的数据删除。

两个多维数据集模块,前一个是专门处理维度,第二个是处理cube。这里之所以要把多维数据集维度处理单独拿出来放在前面,是因为在笔者经验中,对多维数据集的处理虽然是全部处理,但是新增维度的数据不会被聚合到其中,所以需要单独拿出来放在前面处理。

(题外话:对于这个地方笔者一直也不是很理解,按理说既然是全部处理那么怎么连维度都不处理呢,还需要单拿出来)

以下是设置维度处理模块,在界面中选择维度即可。

clip_image022

然后是cube处理模块,如下图。

clip_image024

然后指定增量更新,并且配置增量更新的数据表,这里指定增量表。

clip_image026

多维数据集处理完成之后就可以把增量表的数据放到历史表中了,以保证第二天加入的数据都是增量数据。

需要注意的是,在实际的运行当中,一定要保证BI的处理过程时业务系统没有发生数据,否则就会造成数据遗漏而导致不平。所以,BI的处理一般都是在凌晨。

然后是第三步的数据流模块,此部分的主要任务是将增量表的数据转移到历史表中。

clip_image028

最后的一个SQL任务是一个Delete或者Truncate table任务,把增量表里的数据清空。

最终的任务流程如下图:

clip_image030

执行包,全部成功之后应该如下图所示:

clip_image032

执行成功后,打开历史表,可以发现数据已经在里面了,而且增量表中的数据已经不存在了。

查询多维数据集,可以看到新的数据被聚合到其中。

clip_image034

通过以上透视表可以清晰的看到谁买了什么样的产品。

再次运行Rubbish往增量表里灌几条数据,然后重新运行此SSIS包,可以发现新增的数据已经被聚合到多维数据集中了,注意处理的方式是增量的。

本文提及的数据结构模型都很简单,主要介绍的是多维数据集处理的流程以及方法,重点阐述增量部分的方案,以及需要注意的问题。希望有知道更好方法的兄弟一起交流探讨。

 

本文提及的相关数据库,项目文件以及程序下载

 

FAQ:

1.增量数据是怎么来的?

笔者个人认为这个需要跟业务系统配合来做,比如加入触发器等。或者通过时间戳,到业务系统中能提取到。

2.如果有更新和删除怎么办?

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL: An Introduction to the World's Most Popular DatabaseMySQL: An Introduction to the World's Most Popular DatabaseApr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

The Importance of MySQL: Data Storage and ManagementThe Importance of MySQL: Data Storage and ManagementApr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system suitable for data storage, management, query and security. 1. It supports a variety of operating systems and is widely used in Web applications and other fields. 2. Through the client-server architecture and different storage engines, MySQL processes data efficiently. 3. Basic usage includes creating databases and tables, inserting, querying and updating data. 4. Advanced usage involves complex queries and stored procedures. 5. Common errors can be debugged through the EXPLAIN statement. 6. Performance optimization includes the rational use of indexes and optimized query statements.

Why Use MySQL? Benefits and AdvantagesWhy Use MySQL? Benefits and AdvantagesApr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Describe InnoDB locking mechanisms (shared locks, exclusive locks, intention locks, record locks, gap locks, next-key locks).Describe InnoDB locking mechanisms (shared locks, exclusive locks, intention locks, record locks, gap locks, next-key locks).Apr 12, 2025 am 12:16 AM

InnoDB's lock mechanisms include shared locks, exclusive locks, intention locks, record locks, gap locks and next key locks. 1. Shared lock allows transactions to read data without preventing other transactions from reading. 2. Exclusive lock prevents other transactions from reading and modifying data. 3. Intention lock optimizes lock efficiency. 4. Record lock lock index record. 5. Gap lock locks index recording gap. 6. The next key lock is a combination of record lock and gap lock to ensure data consistency.

What are common causes of poor MySQL query performance?What are common causes of poor MySQL query performance?Apr 12, 2025 am 12:11 AM

The main reasons for poor MySQL query performance include not using indexes, wrong execution plan selection by the query optimizer, unreasonable table design, excessive data volume and lock competition. 1. No index causes slow querying, and adding indexes can significantly improve performance. 2. Use the EXPLAIN command to analyze the query plan and find out the optimizer error. 3. Reconstructing the table structure and optimizing JOIN conditions can improve table design problems. 4. When the data volume is large, partitioning and table division strategies are adopted. 5. In a high concurrency environment, optimizing transactions and locking strategies can reduce lock competition.

When should you use a composite index versus multiple single-column indexes?When should you use a composite index versus multiple single-column indexes?Apr 11, 2025 am 12:06 AM

In database optimization, indexing strategies should be selected according to query requirements: 1. When the query involves multiple columns and the order of conditions is fixed, use composite indexes; 2. When the query involves multiple columns but the order of conditions is not fixed, use multiple single-column indexes. Composite indexes are suitable for optimizing multi-column queries, while single-column indexes are suitable for single-column queries.

How to identify and optimize slow queries in MySQL? (slow query log, performance_schema)How to identify and optimize slow queries in MySQL? (slow query log, performance_schema)Apr 10, 2025 am 09:36 AM

To optimize MySQL slow query, slowquerylog and performance_schema need to be used: 1. Enable slowquerylog and set thresholds to record slow query; 2. Use performance_schema to analyze query execution details, find out performance bottlenecks and optimize.

MySQL and SQL: Essential Skills for DevelopersMySQL and SQL: Essential Skills for DevelopersApr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use