Monday, February 29, 2016

Conditions for Server Minimal Logging in SQL Server

When doing minimal logging SQL Server logs only page/extent allocations instead of logging individual rows. For example, if N rows fit in a page, without minimal logging there will be N log records. With minimal logging there will be only 1 log record.

The following conditions need to be satisfied for minimal logging. -

1.       Database in Simple or Bulk Logged Recovery Model.
2.       Select INTO is Always Minimal Logged.
3.       For Other Bulk Import Methods  (INSERT INTO SELECT, BCP, BulkInsert ) the following additional conditions need to be satisfied.
·         TABLOCK must be specified on target table.
·         If the target table has indexes it must be empty.
·         If the table has no indexes it need not be empty.

Note: For a table with indexes, If you specify batch size for inserts (BCP/BulkInsert), only the first batch will be empty and will be minimally logged.

Friday, January 15, 2016

MDX Query to Find Latest Data Date For Measure Groups in a Cube.

SSAS Cube has integrates data from multiple fact tables, with one or more fact tables per subject area.
Often we are required to show on dashboard the latest data update date for each subject area.
The following MDX query gives the latest data date for the different measure groups in Adventure Works cube.

WITH
  MEMBER [Measures].[InternetSales Date] AS
    Max
    (
      nonempty(
         [Date].[Date].Members
        ,( [Measures].[Internet Sales Amount])
      )
     , [Date].[Date].CurrentMember.Member_Key
    )

MEMBER [Measures].[ResellerSales Date] AS
    Max
    (
      nonempty(
         [Date].[Date].Members
        ,([Measures].[Reseller Sales Amount] )
      )
     , [Date].[Date].CurrentMember.Member_Key
    )

MEMBER [Measures].[Finance Date] AS
    Max
    (
      nonempty(
         [Date].[Date].Members
        ,( [Measures].[Amount])
      )
     , [Date].[Date].CurrentMember.Member_Key
    )

SELECT
  {
     [Measures].[InternetSales Date],
        [Measures].[ResellerSales Date],
        [Measures].[Finance Date]

  } ON COLUMNS
FROM [Adventure Works]

Results
Last InternetSales Update
Last ResellerSales Update
Last Finance Update
20080731
20080601
20080630


The query uses a calculated measure with Max function to figure out the latest data date for measure group. We need one such calculated measure for each measure group.