Friday 2 August 2013

SSAS: How to Create multiple partitions in a single XMLA script

This piece was originally posted last week, 25th June, prior to the post on multiple processing scripts in an xmla command: http://sqlbanana.blogspot.co.uk/2013/07/ssas-how-to-run-multiple-xmla-scripts_6877.html

For some reason the post was not published and lost, so i am reposting it here.
------------------



I was recently hit with a fairly simple problem, but it took me a while to figure out how to do it, so I thought I'd share it to save others some time.

I wanted create a series of partitions on the measure groups of my SSAS 2008R2 database.  I needed a partition for each month, for 5 years.  For each measure group this corresponds to 60 partitions, and I did not want to have to run them one at a time or in multiple windows. 

The solution is to create a single xmla command, that will create all the required partitions in a single batch. Here's how...

First create your Batch tags.  We want all our partitions to be created in a single batch.  Having multiple batch tags in a single session will throw an error, but multiple Create scripts inside a single batch is perfectly fine.

 Batch tags:

<Batch xmlns="http://schemas.microsoft.com/analysisservices/2003/engine" Transaction="true">
.
.
.
</Batch>

  Then put all your create scripts for each partition inside these start and end tags.
<Create xmlns="http://schemas.microsoft.com/analysisservices/2003/engine">
.
.
</Create>
Thus the entire script to create the partitions all in a single command will look something like this:

<Batch xmlns="http://schemas.microsoft.com/analysisservices/2003/engine" Transaction="true">
<
Create xmlns="http://schemas.microsoft.com/analysisservices/2003/engine"><
ParentObject><
DatabaseID>MyDB</DatabaseID><
CubeID>MyCube</CubeID><
MeasureGroupID>MyFACT</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>FACT_20130301</ID><
Name>FACT_20130301</Name><
Source xsi:type="QueryBinding"><
DataSourceID>MyDS</DataSourceID><
QueryDefinition>SELECT COL1 FROM FACT
WHERE (DATE_KEY = 20130301)
</QueryDefinition></
Source><
StorageMode>Molap</StorageMode><
ProcessingMode>Regular</ProcessingMode><
Slice>[Dates].[Date].&amp;[20130301]</Slice><
ProactiveCaching><
SilenceInterval>-PT1S</SilenceInterval><
Latency>-PT1S</Latency><
SilenceOverrideInterval>-PT1S</SilenceOverrideInterval><
ForceRebuildInterval>-PT1S</ForceRebuildInterval><
Source xsi:type="ProactiveCachingInheritedBinding" /></
ProactiveCaching></
Partition></
ObjectDefinition></
Create>
<
Create xmlns="http://schemas.microsoft.com/analysisservices/2003/engine"><
ParentObject><
DatabaseID>MyDB</DatabaseID><
CubeID>MyCube</CubeID><
MeasureGroupID>MyFACT</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>FACT_20130302</ID><
Name>FACT_20130302</Name><
Source xsi:type="QueryBinding"><
DataSourceID>MyDS</DataSourceID><
QueryDefinition>SELECT COL1 FROM FACT
WHERE (DATE_KEY = 20130302)
</QueryDefinition></
Source><
StorageMode>Molap</StorageMode><
ProcessingMode>Regular</ProcessingMode><
Slice>[Dates].[Date].&amp;[20130302]</Slice><
ProactiveCaching><
SilenceInterval>-PT1S</SilenceInterval><
Latency>-PT1S</Latency><
SilenceOverrideInterval>-PT1S</SilenceOverrideInterval><
ForceRebuildInterval>-PT1S</ForceRebuildInterval><
Source xsi:type="ProactiveCachingInheritedBinding" /></
ProactiveCaching></
Partition></
ObjectDefinition></
Create>
</
Batch>


We can extend this now to include all 60 partitions in a single file/Batch. If you ever need to rerun, or modify the scripts, you now have them all in a single place, instead of 60 different places.

Hope this helps :)

5 comments:

  1. Hi I tried to create three partitions using the process above, but it didn't work. I am able to create two partitions using batch but not more then that, could you please check.

    ReplyDelete
    Replies
    1. Hi Koch,

      What error message do you get? Can you share your code for creating three partitions and I will take a look?

      Thanks
      Guy

      Delete
  2. OLE DB provider "MSOLAP" for linked server "Sp***" returned message "XML parsing failed at line 68, column 0: Unexpected end of input.
    .".
    OLE DB provider "MSOLAP" for linked server "Sp***" returned message "The following system error occurred: ".
    OLE DB provider "MSOLAP" for linked server "Sp***" returned message "XML parsing failed at line 66, column 10:
    The name in the end tag of the element must match the element type in the start tag. I am trying to create dynamically calling the xmla through tsql and running it against link server

    ReplyDelete
  3. declare @myXMLA nvarchar(max),
    @currentyear nvarchar(4),
    @JanuaryValue nvarchar(50),@FebruaryValue nvarchar(50),@MarchValue nvarchar(50),@AprilValue nvarchar(50)


    select @JanuaryValue ='sales_'+ @currentyear + '-' + '01'
    select @FebruaryValue ='sales_'+ @currentyear + '-' + '02'
    select @MarchValue ='sales_'+ @currentyear + '-' + '03'
    select @AprilValue ='sales_'+ @currentyear + '-' + '04'



    Set @myXMLA =N'





    FA15042014_CK
    TestCube
    Sales



    '+@JanuaryValue+'
    '+@JanuaryValue+'

    BIDS
    SELECT * FROM [**].[Fact] a left join dimcalendar b
    on a.DkCalendar =b.DkCalendar where
    b.calendarmonthvalue = 1 and b.calendaryear = <= '''+@currentyear+'''

    Molap
    Regular

    -PT1S
    -PT1S
    -PT1S
    -PT1S


    AggregationDesign







    FA15042014_CK
    TestCube
    Sales



    '+@FebruaryValue+'
    '+@FebruaryValue+'

    BIDS
    SELECT * FROM [**].[Fact] a left join dimcalendar b
    on a.DkCalendar =b.DkCalendar where
    b.calendarmonthvalue = 2 and b.calendaryear = <= '''+@currentyear+'''>

    Molap
    Regular

    -PT1S
    -PT1S
    -PT1S
    -PT1S


    9606
    AggregationDesign






    FA15042014_CK
    TESTCUBE
    Sales



    '+@MarchValue+'
    '+@MarchValue+'

    BIDS
    SELECT * FROM [**].[Fact] a left join dimcalendar b
    on a.DkCalendar =b.DkCalendar where
    b.calendarmonthvalue = 3 and b.calendaryear = <= '''+@currentyear+'''

    Molap
    Regular

    -PT1S
    -PT1S
    -PT1S
    -PT1S


    9666
    AggregationDesign



    '

    Exec (@myXMLA) At SSASDP;----Link Server

    This is how I am trying to query, I can create for two months but not more than that

    ReplyDelete
    Replies
    1. The error message suggests that the XMLA is not formed correctly. Looking at your code, however, it is not an XMLA command and it does not contain any of the tags mentioned in the post. Are you manipulating the above strings further to build up the actual XMLA command before sending it?

      As you are building the string dynamically, i would suggest carefully checking the final dynamic string that is sent to the SSAS server. If you can paste that final string here, it may point to the problem.

      Delete