简体   繁体   中英

Recommendation on SQL Server Stored procedure performance tuning for Azure SQL Datawarehouse

We have a table in Azure SQL Data Warehouse which has the daily and monthly sales details. We have developed a dash board in React/Node which calls a stored procedure in Data Warehouse to display the sales trends. The stored procedure currently calculates aggregates based on different columns and different predicates.

The problem is that in the existing code there are multiple select statements with group by for calculating different sum total amounts.In order to make sp run faster I need to fetch all the aggregate amounts in one select query rather in multiple select queries.

I have rewritten the sp to use grouping sets and it runs faster in Azure SQL Server , but this functionality is not available in SQL Data Warehouse.

Please let me know if there is any alternate way to achieve this grouping set functionality in Azure SQL DW.

SQL Server 
--------------
select ,month,week,dept_no,sales_amount_this_year,sales_amount_last_year
from sales_table
where month=1 and year =2019 and dept_no in ('A1','B1',C1')
group by grouping sets ( (month),
                     (month,week),
                     (week,dept_no) )

The sales_table will be having around 100 million row every month. Please help in suggesting an alternative to grouping set in Azure SQL DW to get the same performance as the grouping set in SQL Server.

Please reference this document Group by options in SQL Data Warehouse . It provides the workarounds for GROUPIN SETS .

The GROUP BY T-SQL clause aggregates data to a summary set of rows. GROUP BY has some options that SQL Data Warehouse does not support. These options have workarounds. These options are

  • GROUP BY with ROLLUP
  • GROUPING SETS
  • GROUP BY with CUBE

For details, please see Rollup and grouping sets options :

The simplest option here is to use UNION ALL instead to perform the rollup rather than relying on the explicit syntax. The result is exactly the same

The following example using the GROUP BY statement with the ROLLUP option:

SELECT [SalesTerritoryCountry]
,      [SalesTerritoryRegion]
,      SUM(SalesAmount)             AS TotalSalesAmount
FROM  dbo.factInternetSales s
JOIN  dbo.DimSalesTerritory t       ON s.SalesTerritoryKey       = t.SalesTerritoryKey
GROUP BY ROLLUP (
                        [SalesTerritoryCountry]
                ,       [SalesTerritoryRegion]
                )
;

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM