How can I 'pull' data from an MDX OLAP Cub

2019-07-23 08:43发布

I've been provided access to a cube and need to know if I can set up a stored procedure that can connect to a Cube and retrieve the contents (via an MDX Query). I need this to prevent having to export the data from the Management Studio or from Excel (via PowerPivot). I'm very new to cubes/olap queries so forgive any naivety I may show.

2条回答
疯言疯语
2楼-- · 2019-07-23 09:09

The easiest way is to create a linked server to your cube and then INSERT..SELECT FROM OPENQUERY http://sqlblog.com/blogs/stacia_misner/archive/2010/11/30/31193.aspx

This option has limitations in that

  • it has an 8000 character constraint on the MDX query
  • you must manually create a linked server for each database
  • special code is required to handle when result is empty
  • excessive data types in result set columns (ntext for columns and nvarchar(4000) for rows)

An advanced option is the ExecuteOLAP CLR stored procedure https://olapextensions.codeplex.com/

查看更多
淡お忘
3楼-- · 2019-07-23 09:18

If you choose to use OPENQUERY (easiest way, but having the limitations specified by Brian), the following procedure might be handy:

    /* 
PARAMS:
    @mdx: mdx statement
    @mdx_columns: specifies the mdx columns to retrieve from the executed mdx
    @linkedServer: linked server to be used
    @resultsTable: temporary table to hold results from mdx
    @resultsCols: if only some columns should be filled in @resultsTable, specify them here (e.g. '(col1, col2, ... )' )
    @expectedColCount: expected column count for mdx result. If actual column count is different from the expected count, no data is filled in
    @actualColCount: actual column count. Specify NULL if not interesed in this value
    @Debug: outputs debug info
*/
ALTER PROCEDURE [dbo].[exec_mdx_over_linked_server] (
    @mdx NVARCHAR(MAX),
    @mdx_columns NVARCHAR(1024) = '*',
    @linkedServer VARCHAR(64),
    @resultsTable VARCHAR(64),
    @resultsCols VARCHAR(1024) = '',
    @expectedColCount SMALLINT,
    @actualColCount SMALLINT = NULL OUTPUT,
    @Debug BIT = 0
)
AS
BEGIN
    SET NOCOUNT ON

    if (@Debug = 1)
        PRINT 'Started exec_mdx_over_linked_server procedure for populating ' + @resultsTable

    IF LEN(@MDX)>8000 RAISERROR ('MDX too long for openquery (exec_mdx_over_linked_server)', 
                                16,
                                1);

    declare @SQL NVARCHAR(MAX)

    IF (@Debug = 1)
    BEGIN
        -- getting results from mdx
        SET @SQL = 'SELECT ''Mdx results for ' + @resultsTable + ''' AS ''Mdx results'', ' + '*' + ' 
            FROM OPENQUERY(' + @linkedServer + ', ''' + @mdx + ''')';
        IF LEN(@SQL)>8000 RAISERROR ('MDX too long for openquery (exec_mdx_over_linked_server)', 
                                    16,
                                    1);
        EXEC (@SQL)
    END

    SET @SQL = '
        SELECT ' + '*' + ' INTO #resultsWithWeirdNameToAvoidTempCollisions
        FROM OPENQUERY(' + @linkedServer + ', ''' + @mdx + ''');

        SELECT @colCount = COUNT(*)
        FROM tempdb.sys.columns
        WHERE object_id = object_id(''tempdb..#resultsWithWeirdNameToAvoidTempCollisions'');

        if (@colCount = @expectedColCount)
            INSERT INTO ' + @resultsTable + @resultsCols + '
            SELECT ' + @mdx_columns +  ' FROM #resultsWithWeirdNameToAvoidTempCollisions'


    IF LEN(@SQL)>8000 RAISERROR ('MDX too long for openquery (exec_mdx_over_linked_server)', 
                                    16,
                                    1);

    if (@Debug = 1)
        PRINT 'dbo.exec_mdx_over_linked_server SQL = ' + @SQL

    DECLARE @colCount INT
    EXECUTE sp_executesql @SQL, N'@expectedColCount SMALLINT, @ColCount SMALLINT OUTPUT', @expectedColCount = @expectedColCount, @colCount = @actualColCount OUTPUT

    if (@Debug = 1)
    BEGIN
        PRINT '@expectedColCount = '; PRINT @expectedColCount
        PRINT '@actualColCount = '; PRINT @actualColCount
    END

    -- correction for small float numbers (< 10E-10)

    DECLARE @UpdateSql NVARCHAR(MAX) = N''
    DECLARE @SmallThreshold FLOAT = 0.00000000001


    SELECT @UpdateSql += '
                            UPDATE ' + @resultsTable + ' 
                            SET ' + QUOTENAME(COLUMN_NAME) + ' = 0 
                            WHERE TRY_CONVERT (FLOAT, ' + QUOTENAME(COLUMN_NAME) + ') IS NOT NULL
                                                 AND ABS(' + QUOTENAME(COLUMN_NAME) + ') < ' + CAST(@SmallThreshold AS NVARCHAR(30))
    FROM tempdb.INFORMATION_SCHEMA.COLUMNS with(NOLOCK)
    -- WHERE table_name like @resultsTable + '[_][_][_]%'
    -- changed, in order not to take into consideration objects from other spids
    WHERE table_name = object_name(object_id('tempdb..' + @resultsTable), (select database_id from sys.databases where name = 'tempdb'))

    IF (@Debug = 1)
    BEGIN
        PRINT '@UpdateSql = '; PRINT @UpdateSql;
    END

    EXEC (@UpdateSql);

END

It provides the following advantages:

  • handles the case when received result does not have the expected columns (nothing is performed)
  • performs some roundings for very small numbers (this might happen since the cube knows to work with floating point numbers only)
  • all mdx statements go through a single procedure

When profiling, I have noticed approx. 100ms overhead (execution through the procedure vs. direct execution against analysis server).

.NET developers can use ADOMD.NET framework, which allows running parameterized queries and having a smaller overhead.

查看更多
登录 后发表回答