Jump to content

Lesson Learned #249: All started with the phrase: In PowerBI Direct Query is slow-Partitioned table


Recommended Posts

Guest Jose_Manuel_Jurado
Posted

In some situations, customers that are using PowerBI and Direct Query reported performance issues depending how the query has been defined by PowerBI. In this scenarion, I would like to share with you how we fixed this performance issue using Partitioning

 

 

 

For this example, I download a demo database Release Wide World Importers sample database v1.0 · microsoft/sql-server-samples · GitHub and duplicate rows of a table Fact.Sale until having 234.767.360 rows. I choose HyperScale Database tier basically as a medium size database for OLAP.

 

 

 

In every analysis of performance with PowerBI, if you need to know how many rows we have per table use the following TSQL, instead of using SELECT COUNT() for performance improvements.

 

 

 

 

 

SELECT

t.NAME AS TableName,

s.Name AS SchemaName,

max(CASE i.type WHEN 5 THEN si.rowcnt ELSE p.rows END) AS RowCounts,

SUM(a.total_pages) * 8 AS TotalSpaceKB,

SUM(a.used_pages) * 8 AS UsedSpaceKB,

(SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB

FROM

sys.tables t

INNER JOIN

sys.indexes i ON t.OBJECT_ID = i.object_id

INNER JOIN

sysindexes si ON t.OBJECT_ID = si.id

INNER JOIN

sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id

INNER JOIN

sys.allocation_units a ON p.partition_id = a.container_id

LEFT OUTER JOIN

sys.schemas s ON t.schema_id = s.schema_id

WHERE t.is_ms_shipped = 0

AND i.OBJECT_ID > 255

GROUP BY

t.Name, s.Name

ORDER BY

t.Name, s.Name

 

 

 

Define the report.

 

 

In this situation, we have a report where we need to obtain filtering per Fiscal Month Label by FY2013-Aug, FY2014-Aug and FY2015-Aug the Quantity sold per item.

 

 

 

911x459vv2.png.f81fb5f4280610fc34b493993ed39ad0.png

 

 

 

PowerBI generates the following TSQL statement.

 

 

Using Azure Data Studio and SQL Server Profiler extension we could see the query.

 

 

 

 

 

 

SELECT

TOP (1000001) *

FROM

(

 

SELECT [t3].[Fiscal Month Label] AS [c43],[t5].[stock Item] AS [c59],SUM(

CAST([t6].[Quantity] as BIGINT)

)

AS [a0]

FROM

(

((

select [$Table].[sale Key] as [sale Key],

[$Table].[City Key] as [City Key],

[$Table].[Customer Key] as [Customer Key],

[$Table].[bill To Customer Key] as [bill To Customer Key],

[$Table].[stock Item Key] as [stock Item Key],

[$Table].[invoice Date Key] as [invoice Date Key],

[$Table].[Delivery Date Key] as [Delivery Date Key],

[$Table].[salesperson Key] as [salesperson Key],

[$Table].[WWI Invoice ID] as [WWI Invoice ID],

[$Table].[Description] as [Description],

[$Table].[Package] as [Package],

[$Table].[Quantity] as [Quantity],

[$Table].[unit Price] as [unit Price],

[$Table].[Tax Rate] as [Tax Rate],

[$Table].[Total Excluding Tax] as [Total Excluding Tax],

[$Table].[Tax Amount] as [Tax Amount],

[$Table].[Profit] as [Profit],

[$Table].[Total Including Tax] as [Total Including Tax],

[$Table].[Total Dry Items] as [Total Dry Items],

[$Table].[Total Chiller Items] as [Total Chiller Items],

[$Table].[Lineage Key] as [Lineage Key]

from [Fact].[sale] as [$Table]

) AS [t6]

 

LEFT OUTER JOIN

 

(

select [$Table].[Date] as [Date],

[$Table].[Day Number] as [Day Number],

[$Table].[Day] as [Day],

[$Table].[Month] as [Month],

[$Table].[short Month] as [short Month],

[$Table].[Calendar Month Number] as [Calendar Month Number],

[$Table].[Calendar Month Label] as [Calendar Month Label],

[$Table].[Calendar Year] as [Calendar Year],

[$Table].[Calendar Year Label] as [Calendar Year Label],

[$Table].[Fiscal Month Number] as [Fiscal Month Number],

[$Table].[Fiscal Month Label] as [Fiscal Month Label],

[$Table].[Fiscal Year] as [Fiscal Year],

[$Table].[Fiscal Year Label] as [Fiscal Year Label],

[$Table].[iSO Week Number] as [iSO Week Number]

from [Dimension].[Date] as [$Table]

) AS [t3] on

(

[t6].[Delivery Date Key] = [t3].[Date]

)

)

 

 

INNER JOIN

 

(

select [$Table].[stock Item Key] as [stock Item Key],

[$Table].[WWI Stock Item ID] as [WWI Stock Item ID],

[$Table].[stock Item] as [stock Item],

[$Table]. as ,

[$Table].[selling Package] as [selling Package],

[$Table].[buying Package] as [buying Package],

[$Table].[brand] as [brand],

[$Table]. as ,

[$Table].[Lead Time Days] as [Lead Time Days],

[$Table].[Quantity Per Outer] as [Quantity Per Outer],

[$Table].[is Chiller Stock] as [is Chiller Stock],

[$Table].[barcode] as [barcode],

[$Table].[Tax Rate] as [Tax Rate],

[$Table].[unit Price] as [unit Price],

[$Table].[Recommended Retail Price] as [Recommended Retail Price],

[$Table].[Typical Weight Per Unit] as [Typical Weight Per Unit],

[$Table].[Photo] as [Photo],

[$Table].[Valid From] as [Valid From],

[$Table].[Valid To] as [Valid To],

[$Table].[Lineage Key] as [Lineage Key]

from [Dimension].[stock Item] as [$Table]

) AS [t5] on

(

[t6].[stock Item Key] = [t5].[stock Item Key]

)

)

 

WHERE

(

([t3].[Fiscal Month Label] IN (N'FY2013-Aug',N'FY2014-Aug',N'FY2015-Aug'))

)

 

GROUP BY [t3].[Fiscal Month Label],[t5].[stock Item]

)

AS [MainTable]

WHERE

(

 

NOT(

(

[a0] IS NULL

)

)

 

)

 

 

 

 

 

  • Basically, this query joins the table Dimension.Date with Fact.Sale by Delivery Date Key grouping by Stock Item. As you could see the almost time 95% of this query is filtering the table Fact.Sale to prepare the filter by Fiscal Month Label.

 

 

 

901x187vv2.png.149bda7f5e6095617733defb961a5e0f.png

 

 

 

  • To solve this issue and improve the query, the suggestion was to change the table Fact.Sale to a partitioned table.

 

 

 

 

 

CREATE PARTITION FUNCTION myDateRangePF (date)

AS RANGE RIGHT FOR VALUES ('2013-01-01','2014-01-01','2015-01-01','2016-01-01')

GO

CREATE PARTITION SCHEME myPartitionScheme

AS PARTITION myDateRangePF ALL TO ([PRIMARY])

GO

 

ALTER TABLE Fact.Sale DROP CONSTRAINT PK_fACT_SALE

 

GO

ALTER TABLE Fact.Sale ADD CONSTRAINT PK_fACT_SALE PRIMARY KEY NONCLUSTERED ([sale Key])

WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,

ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO

CREATE CLUSTERED INDEX Fact_Sale_IX1 ON Fact.Sale ([Delivery Date Key])

WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,

ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)

ON myPartitionScheme([Delivery Date Key])

GO

 

 

 

 

 

  • After the implementation of this partitioning, we improved a lot (seconds) the execution because the table is already partitioned, and SQL server only needs to read the partitions associated with the filter.

 

 

 

875x453vv2.png.9d8a0e0b329a38c52de35c2db58e3aec.png

 

 

 

Other advantages that we have creating these partitions is to reduce our maintenance plans, for example:

 

 

 

 

 

 

Enjoy!

 

Continue reading...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...