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

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

该包用到的所有变量

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

1、得到所有分区:

①、主要设置如下图

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

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

  1. SELECT
  2. 'RmyyHisDW' AS DataSoureID,--数据源
  3. 'RmyyMZ' AS CubeName,--分区来自哪一个cube
  4. 'RmyyMZ' AS CubeID,
  5. 'Fact Mz Visit Table' AS MeasureGroup,--指定是一个度量值组
  6. 'Fact Mz Visit Table' AS MeasureGroupID,
  7. 'Fact Mz Visit Table' + Cast(MonthInfo.YearMonth AS VARCHAR(6)) AS Partition,--分区名称=度量值组名称+年月
  8. 'SELECT [dbo].[fact_mz_visit_table].[patient_id],
  9. [dbo].[fact_mz_visit_table].[times],
  10. [dbo].[fact_mz_visit_table].[name],
  11. [dbo].[fact_mz_visit_table].[age],
  12. [dbo].[fact_mz_visit_table].[ampm],
  13. [dbo].[fact_mz_visit_table].[charge_type],
  14. [dbo].[fact_mz_visit_table].[clinic_type],
  15. [dbo].[fact_mz_visit_table].[contract_code],
  16. [dbo].[fact_mz_visit_table].[visit_dept],
  17. [dbo].[fact_mz_visit_table].[doctor_code],
  18. [dbo].[fact_mz_visit_table].[gh_date],
  19. [dbo].[fact_mz_visit_table].[gh_date_time],
  20. [dbo].[fact_mz_visit_table].[gh_opera],
  21. [dbo].[fact_mz_visit_table].[haoming_code],
  22. [dbo].[fact_mz_visit_table].[icd_code],
  23. [dbo].[fact_mz_visit_table].[icd_code1],
  24. [dbo].[fact_mz_visit_table].[icd_code2],
  25. [dbo].[fact_mz_visit_table].[icd_code3],
  26. [dbo].[fact_mz_visit_table].[response_type],
  27. [dbo].[fact_mz_visit_table].[visit_date],
  28. [dbo].[fact_mz_visit_table].[visit_date_time],
  29. [dbo].[fact_mz_visit_table].[visit_flag]
  30. FROM [dbo].[fact_mz_visit_table]
  31. WHERE visit_flag <> 9 and where_clause' AS SQL,--要进行分区的SQL
  32. cast(MinDateKey as varchar(8)) as MinDateKey,--最小datekey
  33. cast(MaxDateKey as varchar(8)) as MaxDateKey--最大datekey
  34. FROM
  35. (SELECT t1.YearMonth,
  36. (SELECT Min(datekey) FROM dim_date t2 WHERE CONVERT(VARCHAR(6), t2.Date, 112) = t1.YearMonth) AS MinDateKey,
  37. (SELECT Max(datekey) FROM dim_date t2 WHERE CONVERT(VARCHAR(6), t2.Date, 112) = t1.YearMonth) AS MaxDateKey
  38. FROM (SELECT DISTINCT CONVERT(VARCHAR(6), [Date], 112) AS YearMonth FROM dim_date) AS t1
  39. ) MonthInfo
  40. WHERE
  41. 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

③主要代码为

  1. /*
  2. Microsoft SQL Server Integration Services Script Task
  3. Write scripts using Microsoft Visual C# 2008.
  4. The ScriptMain is the entry point class of the script.
  5. */
  6. using System;
  7. using System.Data;
  8. using Microsoft.SqlServer.Dts.Runtime;
  9. using System.Windows.Forms;
  10. using Microsoft.AnalysisServices;
  11. namespace ST_f33f263fa3864817a3291fc4715774d3.csproj
  12. {
  13. [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
  14. public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
  15. {
  16. #region VSTA generated code
  17. enum ScriptResults
  18. {
  19. Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
  20. Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
  21. };
  22. #endregion
  23. /*
  24. The execution engine calls this method when the task executes.
  25. To access the object model, use the Dts property. Connections, variables, events,
  26. and logging features are available as members of the Dts property as shown in the following examples.
  27. To reference a variable, call Dts.Variables["MyCaseSensitiveVariableName"].Value;
  28. To post a log entry, call Dts.Log("This is my log text", 999, null);
  29. To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, true);
  30. To use the connections collection use something like the following:
  31. ConnectionManager cm = Dts.Connections.Add("OLEDB");
  32. cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;";
  33. Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
  34. To open Help, press F1.
  35. */
  36. public void Main()
  37. {
  38. // TODO: Add your code here
  39. // Dts.TaskResult = (int)ScriptResults.Success;
  40. //将参数赋给变量
  41. String sPartition = (String)Dts.Variables["Partition"].Value;
  42. String sCubeName = (String)Dts.Variables["CubeName"].Value;
  43. String sMeasureGroup = (String)Dts.Variables["MeasureGroup"].Value;
  44. String sServer = "localhost";
  45. String sDataBaseID = (String)Dts.Variables["DatabaseID"].Value;
  46. String sCubeID = (String)Dts.Variables["CubeID"].Value;
  47. String sMeasureGroupID = (String)Dts.Variables["MeasureGroupID"].Value;
  48. String sDataSoureID = (String)Dts.Variables["DataSoureID"].Value;
  49. String sSQL = (String)Dts.Variables["SQL"].Value;
  50. String sMaxDateKey = (String)Dts.Variables["MaxDateKey"].Value;
  51. String sMinDateKey = (String)Dts.Variables["MinDateKey"].Value;
  52. string aSql = sSQL.Replace("where_clause", "visit_date >=" + sMinDateKey + " and visit_date <=" + sMaxDateKey);
  53. ConnectionManager cm = Dts.Connections.Add("MSOLAP100");
  54. cm.ConnectionString = "Provider=MSOLAP.4;Data Source=localhost;Integrated Security=SSPI;Initial Catalog=" + sDataBaseID;
  55. Microsoft.AnalysisServices.Server aServer = new Server();
  56. aServer.Connect(sServer);
  57. Microsoft.AnalysisServices.Database aDatabase = aServer.Databases.FindByName(sDataBaseID);
  58. Microsoft.AnalysisServices.Cube aCube = aDatabase.Cubes.FindByName(sCubeName);
  59. Microsoft.AnalysisServices.MeasureGroup aMeasureGroup = aCube.MeasureGroups.FindByName(sMeasureGroup);
  60. //判断分区是否存在
  61. if (aMeasureGroup.Partitions.Contains(sPartition))
  62. {
  63. Dts.Variables["IsNetePresent"].Value = false;
  64. Dts.Variables["Xmla_Script"].Value = "";
  65. Dts.TaskResult = (int)ScriptResults.Success;
  66. }
  67. else
  68. {
  69. Dts.Variables["IsNetePresent"].Value = true;
  70. Dts.Variables["Xmla_Script"].Value =
  71. "<Create xmlns=\"http://schemas.microsoft.com/analysisservices/2003/engine\">"
  72. + "<ParentObject>"
  73. + "<DatabaseID>" + sDataBaseID + "</DatabaseID>"
  74. + "<CubeID>" + sCubeID + "</CubeID>"
  75. + "<MeasureGroupID>" + sMeasureGroupID + "</MeasureGroupID>"
  76. + "</ParentObject>"
  77. + "<ObjectDefinition>"
  78. + "<Partition xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
  79. +"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\">"
  80. + "<ID>" + sPartition + "</ID>"
  81. + "<Name>" + sPartition + "</Name>"
  82. + "<Source xsi:type=\"QueryBinding\">"
  83. + "<DataSourceID>" + sDataSoureID + "</DataSourceID>"
  84. + "<QueryDefinition>" + aSql + "</QueryDefinition>"
  85. + "</Source>"
  86. + "<StorageMode>Molap</StorageMode> <ProcessingMode>Regular</ProcessingMode>"
  87. + "<ProactiveCaching> <SilenceInterval>-PT1S</SilenceInterval> <Latency>-PT1S</Latency> <SilenceOverrideInterval>-PT1S</SilenceOverrideInterval> <ForceRebuildInterval>-PT1S</ForceRebuildInterval>"
  88. + "<Source xsi:type=\"ProactiveCachingInheritedBinding\" /> </ProactiveCaching>"
  89. + "</Partition>"
  90. + "</ObjectDefinition>"
  91. + "</Create>";
  92. Dts.TaskResult = (int)ScriptResults.Success;
  93. }
  94. }
  95. }
  96. }

④、判断是否执行下一步

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. Nginx 和 Apache 开启目录浏览功能

    1.Nginx 在相应项目的 Server 段中的 location 段中,添加 autoindex on.例如: server { listen ; server_name www.dee.prac ...

  2. 使用Jmeter监测服务器cpu、内存等性能

    jmeter中可以监控服务器的CPU和内存使用情况,但是需要安装一些插件还需要在被监测服务器上开启服务. 1.下载JMeterPlugins-Standard-1.4.0.zip插件.下载后将JMet ...

  3. Sublime Text 2 JS 格式化插件 JsFormat的配置使用

    (转自http://www.jb51.net/softjc/178401.html) 这里下载这插件包 https://github.com/jdc0589/JsFormat ,点油下角的zip就能下 ...

  4. css3知识

    一.box-sizing 属性 规定两个并排的带边框的框 二.align-items (适用于父类容器上) 设置或检索弹性盒子元素在侧轴(纵轴)方向上的对齐方式. 值: flex-start:弹性盒子 ...

  5. Ms sql 2005 中的bit 数据类型

    bit 整型数据 1.0 或 NULL(在表中的表现形式). 注释: 不能对 bit 类型的列使用索引. Microsoft® SQL Server™ 优化用于 bit 列的存储.如果一个表中有不多于 ...

  6. Python学习【第五篇】循环语句

    Python循环语句 接下来将介绍Python的循环语句,程序在一般情况下是按顺序执行的. 编程语言提供了各种控制结构,允许更复杂的执行路径. 循环语句允许我们执行一个语句或语句组多次. Python ...

  7. Python之路-python(css布局、JavaScript)

    CSS布局 JavaScript css布局: 后台管理界面一:(左右标签都有下来菜单) 利用position: absolute;让某个标签固定在具体位置,然后使用overflow: auto;属性 ...

  8. EF 保证线程内唯一 上下文的创建

    1.ef添加完这个对象,就会自动返回这个对象数据库的内容,比如下面这个表是自增ID 最后打印出来的ID  就是自增的结果 2.lambda 中怎么select * var userInfoList = ...

  9. Hadoop等软件常见运行问题及解决办法

    Hadoop常见问题及解决办法  1.问题:java.io.IOException: Could not locate executable null\bin\winutils.exe in the ...

  10. IIS/IIS Express/Asp.net配置片段记录

    事情的起因是,我们在项目中使用了URLRewriter.dll作为实现伪静态的工具,在VS2010及之前的开发环境中,该功能运行正常,但在VS Express 2012 for Web中就不起作用了, ...