一、动态分区的好处就不说了,随着时间的推移,不可能一个度量值组都放在一个分区中,处理速度非常慢,如何动态添加分区,如何动态处理分区,成为了很多新手BI工程师一个头痛的问题,废话不多说,分享一下我的经验。

二、首先讲一下大致的流程,主要是通过SSIS进行任务的处理,本文主要是按照月进行分区,当然分区的规则大家可以根据自己的需求制定。

该包用到的所有变量

三、对上面四个步骤分别讲解一下。

1、得到所有分区:

①、主要设置如下图

②、输出的结果集应该传给变量Partitions

③、SQLStatement为:(主要依据创建分区的语句中需要的参数的值)

SELECT
'RmyyHisDW' AS DataSoureID,--数据源
'RmyyMZ' AS CubeName,--分区来自哪一个cube
'RmyyMZ' AS CubeID,
'Fact Mz Visit Table' AS MeasureGroup,--指定是一个度量值组
'Fact Mz Visit Table' AS MeasureGroupID,
'Fact Mz Visit Table' + Cast(MonthInfo.YearMonth AS VARCHAR(6)) AS Partition,--分区名称=度量值组名称+年月
'SELECT [dbo].[fact_mz_visit_table].[patient_id],
[dbo].[fact_mz_visit_table].[times],
[dbo].[fact_mz_visit_table].[name],
[dbo].[fact_mz_visit_table].[age],
[dbo].[fact_mz_visit_table].[ampm],
[dbo].[fact_mz_visit_table].[charge_type],
[dbo].[fact_mz_visit_table].[clinic_type],
[dbo].[fact_mz_visit_table].[contract_code],
[dbo].[fact_mz_visit_table].[visit_dept],
[dbo].[fact_mz_visit_table].[doctor_code],
[dbo].[fact_mz_visit_table].[gh_date],
[dbo].[fact_mz_visit_table].[gh_date_time],
[dbo].[fact_mz_visit_table].[gh_opera],
[dbo].[fact_mz_visit_table].[haoming_code],
[dbo].[fact_mz_visit_table].[icd_code],
[dbo].[fact_mz_visit_table].[icd_code1],
[dbo].[fact_mz_visit_table].[icd_code2],
[dbo].[fact_mz_visit_table].[icd_code3],
[dbo].[fact_mz_visit_table].[response_type],
[dbo].[fact_mz_visit_table].[visit_date],
[dbo].[fact_mz_visit_table].[visit_date_time],
[dbo].[fact_mz_visit_table].[visit_flag]
FROM [dbo].[fact_mz_visit_table]
WHERE visit_flag <> 9 and where_clause' AS SQL,--要进行分区的SQL
cast(MinDateKey as varchar(8)) as MinDateKey,--最小datekey
cast(MaxDateKey as varchar(8)) as MaxDateKey--最大datekey
FROM
(SELECT t1.YearMonth,
(SELECT Min(datekey) FROM dim_date t2 WHERE CONVERT(VARCHAR(6), t2.Date, 112) = t1.YearMonth) AS MinDateKey,
(SELECT Max(datekey) FROM dim_date t2 WHERE CONVERT(VARCHAR(6), t2.Date, 112) = t1.YearMonth) AS MaxDateKey
FROM (SELECT DISTINCT CONVERT(VARCHAR(6), [Date], 112) AS YearMonth FROM dim_date) AS t1
) MonthInfo
WHERE
EXISTS(SELECT * FROM fact_mz_visit_table WHERE visit_date BETWEEN MonthInfo.MinDateKey AND MonthInfo.MaxDateKey)

注意:SQL字段中最后面有个where_clause ,在“判断分区脚本任务”中的C#脚本中会替换成后面的where条件,也就是将MinDateKey和MaxDateKey加入条件限制,进行分区。

④、步骤③执行的结果为

2、Foreach 循环容器(主要循环执行上面的sql语句执行的结果)相关设置如下图

注意:变量映射按照sql语句中的字段名的顺序

3、判断分区是否存在,主要是通过步骤2中传出的参数判断cube中是否有该分区,有则不创建,无则通过Anaysis Services执行DDL任务来创建。

①、具体设置如下:

②、点击编辑脚本任务

需要引用AMO

③主要代码为

/*
Microsoft SQL Server Integration Services Script Task
Write scripts using Microsoft Visual C# 2008.
The ScriptMain is the entry point class of the script.
*/
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using Microsoft.AnalysisServices;
namespace ST_f33f263fa3864817a3291fc4715774d3.csproj
{
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
/*
The execution engine calls this method when the task executes.
To access the object model, use the Dts property. Connections, variables, events,
and logging features are available as members of the Dts property as shown in the following examples.
To reference a variable, call Dts.Variables["MyCaseSensitiveVariableName"].Value;
To post a log entry, call Dts.Log("This is my log text", 999, null);
To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, true);
To use the connections collection use something like the following:
ConnectionManager cm = Dts.Connections.Add("OLEDB");
cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;";
Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
To open Help, press F1.
*/
public void Main()
{
// TODO: Add your code here
// Dts.TaskResult = (int)ScriptResults.Success;
//将参数赋给变量
String sPartition = (String)Dts.Variables["Partition"].Value;
String sCubeName = (String)Dts.Variables["CubeName"].Value;
String sMeasureGroup = (String)Dts.Variables["MeasureGroup"].Value;
String sServer = "localhost";
String sDataBaseID = (String)Dts.Variables["DatabaseID"].Value;
String sCubeID = (String)Dts.Variables["CubeID"].Value;
String sMeasureGroupID = (String)Dts.Variables["MeasureGroupID"].Value;
String sDataSoureID = (String)Dts.Variables["DataSoureID"].Value;
String sSQL = (String)Dts.Variables["SQL"].Value;
String sMaxDateKey = (String)Dts.Variables["MaxDateKey"].Value;
String sMinDateKey = (String)Dts.Variables["MinDateKey"].Value;
string aSql = sSQL.Replace("where_clause", "visit_date >=" + sMinDateKey + " and visit_date <=" + sMaxDateKey);
ConnectionManager cm = Dts.Connections.Add("MSOLAP100");
cm.ConnectionString = "Provider=MSOLAP.4;Data Source=localhost;Integrated Security=SSPI;Initial Catalog=" + sDataBaseID;
Microsoft.AnalysisServices.Server aServer = new Server();
aServer.Connect(sServer);
Microsoft.AnalysisServices.Database aDatabase = aServer.Databases.FindByName(sDataBaseID);
Microsoft.AnalysisServices.Cube aCube = aDatabase.Cubes.FindByName(sCubeName);
Microsoft.AnalysisServices.MeasureGroup aMeasureGroup = aCube.MeasureGroups.FindByName(sMeasureGroup);
//判断分区是否存在
if (aMeasureGroup.Partitions.Contains(sPartition))
{
Dts.Variables["IsNetePresent"].Value = false;
Dts.Variables["Xmla_Script"].Value = "";
Dts.TaskResult = (int)ScriptResults.Success;
}
else
{
Dts.Variables["IsNetePresent"].Value = true;
Dts.Variables["Xmla_Script"].Value =
"<Create xmlns=\"http://schemas.microsoft.com/analysisservices/2003/engine\">"
+ "<ParentObject>"
+ "<DatabaseID>" + sDataBaseID + "</DatabaseID>"
+ "<CubeID>" + sCubeID + "</CubeID>"
+ "<MeasureGroupID>" + sMeasureGroupID + "</MeasureGroupID>"
+ "</ParentObject>"
+ "<ObjectDefinition>"
+ "<Partition xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
+"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:ddl2=\"http://schemas.microsoft.com/analysisservices/2003/engine/2\" xmlns:ddl2_2=\"http://schemas.microsoft.com/analysisservices/2003/engine/2/2\" xmlns:ddl100_100=\"http://schemas.microsoft.com/analysisservices/2008/engine/100/100\" xmlns:ddl200=\"http://schemas.microsoft.com/analysisservices/2010/engine/200\" xmlns:ddl200_200=\"http://schemas.microsoft.com/analysisservices/2010/engine/200/200\">"
+ "<ID>" + sPartition + "</ID>"
+ "<Name>" + sPartition + "</Name>"
+ "<Source xsi:type=\"QueryBinding\">"
+ "<DataSourceID>" + sDataSoureID + "</DataSourceID>"
+ "<QueryDefinition>" + aSql + "</QueryDefinition>"
+ "</Source>"
+ "<StorageMode>Molap</StorageMode> <ProcessingMode>Regular</ProcessingMode>"
+ "<ProactiveCaching> <SilenceInterval>-PT1S</SilenceInterval> <Latency>-PT1S</Latency> <SilenceOverrideInterval>-PT1S</SilenceOverrideInterval> <ForceRebuildInterval>-PT1S</ForceRebuildInterval>"
+ "<Source xsi:type=\"ProactiveCachingInheritedBinding\" /> </ProactiveCaching>"
+ "</Partition>"
+ "</ObjectDefinition>"
+ "</Create>";
Dts.TaskResult = (int)ScriptResults.Success;
}
}
}
}

④、判断是否执行下一步

4、不存在创建分区(主要执行步骤3传过来的Xmla_Script),具体设置如下:

5、执行任务,查看结果:

SSAS动态添加分区 (转载)的更多相关文章

  1. SSAS动态添加分区(一)

    一.动态分区的好处就不说了,随着时间的推移,不可能一个度量值组都放在一个分区中,处理速度非常慢,如何动态添加分区,如何动态处理分区,成为了很多新手BI工程师一个头痛的问题,废话不多说,分享一下我的经验 ...

  2. 用Javascript动态添加删除HTML元素实例 (转载)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. [转载]给Jquery动态添加的元素添加事件

    原文地址:给Jquery动态添加的元素添加事件作者:小飞侠 我想很多人都会向我一样曾经 被新元素的事件绑定困惑很久也就是在页面加载完成后给元素绑定了事件,但又新增加的元素上却没有绑定任何事件. js的 ...

  4. 动态添加select的option [转载]

    动态给select标签添加option,结合前人经验以及自己经验,现在总结三种方法供大家参考,一起交流学习!首先是定义的select元素://根据ID获得select元素 var mySelect = ...

  5. ASP.NET给前端动态添加修改 CSS样式JS 标题 关键字(转载)

    原文地址:http://www.cnblogs.com/xbhp/p/6392225.html 有很多网站读者能换自己喜欢的样式,还有一些网站想多站点共享后端代码而只动前段样式,可以采用动态替换CSS ...

  6. js动态添加事件-事件委托

    作者:白狼 出处:http://www.manks.top/javascript-dynamic-event.html 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给 ...

  7. Hadoop学习笔记—13.分布式集群中节点的动态添加与下架

    开篇:在本笔记系列的第一篇中,我们介绍了如何搭建伪分布与分布模式的Hadoop集群.现在,我们来了解一下在一个Hadoop分布式集群中,如何动态(不关机且正在运行的情况下)地添加一个Hadoop节点与 ...

  8. jquery实现动态添加html代码

    先看下思导图,整体了解下,然后我们再来学习. 现在我们来看一下几段代码,然后根据这几段代码我们来学习一下如何正确的学习动态添加html. 一.html()方法 html函数的作用原理首先是移除目标元素 ...

  9. 使用runtime给类动态添加方法并调用 - class_addMethod

    上手开发 iOS 一段时间后,我发现并不能只着眼于完成需求,利用闲暇之余多研究其他的开发技巧,才能在有限时间内提升自己水平.当然,“其他开发技巧”这个命题对于任何一个开发领域都感觉不找边际,而对于我来 ...

随机推荐

  1. 建设移动端web开发会涉及到的meta标签

    <meta name="format-detection" content="telephone=no"> <meta name=" ...

  2. thinkPHP访问不同表前缀

    $Model=new Model(); $goods=$Model->Table('sdb_goods'); $param['brief']=array('like','%'.$p_title. ...

  3. URL重写无效

    在IIS7或以上版本中使用urlrewriter时会出现无效的现像,这时候需要使用以下设置

  4. springboot+dubbo

    使用springboot搭建dubbo服务,首先封装出springboot-dubbo的项目,引入在服务上加直接@Service直接使用,方便于拆封统一管理. package hello.dubbo. ...

  5. Neil·Zou 语录三

    1 人的大脑,假设100分成熟.那么每个人大脑达到100分的时间点不同.有些人因为外界因素在读书时就达到了,而有些人到工作后才达到.但很可惜,很多国人进入社会后就不学习了.所以说,工作后保持激情.好奇 ...

  6. 让javascript显原型!

    相信以下的javascript让你读起来痛苦不已,告诉你一下简单的办法,就可以让它显出原型!将第一个单词,即eval换成document.write,然后再运行一下,它立即就原形毕露了! eval(f ...

  7. 精简的javascript下throttle和debounce代码

    //频率控制 函数连续调用时,fn 执行频率限定为 1次/waitMs.立即执行1次 function throttle(fn, waitMs) { var lastRun = 0; return f ...

  8. UIKit的外观属性及方法汇总

    本文授权转载,作者:楚天舒(简书) 开发中经常会碰到要设置UI的某个生僻的外观属性,虽然在头文件中都能查到,但一是每个控件的头文件比较分散繁杂,二是每个头文件里有太多不用到的方法和属性,所以这里将UI ...

  9. SQLite 加密 -- SQLCipher

    SQLite3 插件 github 下载地址 插件配置步骤地址 购买地址 其他加密方式介绍 SQLCipher API 地址 前言 应用使用 SQLite 来存储数据,很多时候需要对一部分的数据进行加 ...

  10. [软件测试基础2]基于selenium的自动化测试

    这次上机我们主要使用Selenium进行自动化测试,首先我们需要下载selenium-java的依赖项. 若使用maven管理项目,则在.pom文件中加入如下依赖项: <dependency&g ...