Skip to content

Commit 5b68cea

Browse files
authored
Updates to Scalar UDF inlining Demo
1 parent 87e7dad commit 5b68cea

1 file changed

Lines changed: 72 additions & 67 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -1,67 +1,72 @@
1-
-- ******************************************************** --
2-
-- Scalar UDF Inlining
3-
4-
-- See https://aka.ms/IQP for more background
5-
6-
-- Demo scripts: https://aka.ms/IQPDemos
7-
8-
-- Demo uses SQL Server 2019 Public Preview and also works on Azure SQL DB
9-
10-
-- Email IntelligentQP@microsoft.com for questions\feedback
11-
-- ******************************************************** --
12-
USE [master];
13-
GO
14-
15-
ALTER DATABASE [WideWorldImportersDW] SET COMPATIBILITY_LEVEL = 150;
16-
GO
17-
18-
ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE;
19-
GO
20-
21-
USE [WideWorldImportersDW];
22-
GO
23-
24-
/*
25-
Adapted from SQL Server Books Online
26-
https://docs.microsoft.com/sql/relational-databases/user-defined-functions/scalar-udf-inlining?view=sqlallproducts-allversions
27-
*/
28-
CREATE OR ALTER FUNCTION
29-
dbo.customer_category(@CustomerKey INT)
30-
RETURNS CHAR(10) AS
31-
BEGIN
32-
DECLARE @total_amount DECIMAL(18,2);
33-
DECLARE @category CHAR(10);
34-
35-
SELECT @total_amount =
36-
SUM([Total Including Tax])
37-
FROM [Fact].[OrderHistory]
38-
WHERE [Customer Key] = @CustomerKey;
39-
40-
IF @total_amount < 500000
41-
SET @category = 'REGULAR';
42-
ELSE IF @total_amount < 1000000
43-
SET @category = 'GOLD';
44-
ELSE
45-
SET @category = 'PLATINUM';
46-
47-
RETURN @category;
48-
END
49-
GO
50-
51-
-- Before (show actual query execution plan for legacy behavior)
52-
SELECT TOP 100
53-
[Customer Key], [Customer],
54-
dbo.customer_category([Customer Key]) AS [Discount Price]
55-
FROM [Dimension].[Customer]
56-
ORDER BY [Customer Key]
57-
OPTION (RECOMPILE,USE HINT('DISABLE_TSQL_SCALAR_UDF_INLINING'));
58-
59-
-- After (show actual query execution plan for Scalar UDF Inlining)
60-
SELECT TOP 100
61-
[Customer Key], [Customer],
62-
dbo.customer_category([Customer Key]) AS [Discount Price]
63-
FROM [Dimension].[Customer]
64-
ORDER BY [Customer Key]
65-
OPTION (RECOMPILE);
66-
67-
1+
-- ******************************************************** --
2+
-- Scalar UDF Inlining
3+
4+
-- See https://aka.ms/IQP for more background
5+
6+
-- Demo scripts: https://aka.ms/IQPDemos
7+
8+
-- Demo uses SQL Server 2019 Public Preview and also works on Azure SQL DB
9+
10+
-- Email IntelligentQP@microsoft.com for questions\feedback
11+
-- ******************************************************** --
12+
USE [master];
13+
GO
14+
15+
ALTER DATABASE [WideWorldImportersDW] SET COMPATIBILITY_LEVEL = 150;
16+
GO
17+
18+
ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE;
19+
GO
20+
21+
USE [WideWorldImportersDW];
22+
GO
23+
24+
/*
25+
Adapted from SQL Server Books Online
26+
https://docs.microsoft.com/sql/relational-databases/user-defined-functions/scalar-udf-inlining?view=sqlallproducts-allversions
27+
*/
28+
CREATE OR ALTER FUNCTION
29+
dbo.ufn_customer_category(@CustomerKey INT)
30+
RETURNS CHAR(10) AS
31+
BEGIN
32+
DECLARE @total_amount DECIMAL(18,2);
33+
DECLARE @category CHAR(10);
34+
35+
SELECT @total_amount =
36+
SUM([Total Including Tax])
37+
FROM [Fact].[OrderHistory]
38+
WHERE [Customer Key] = @CustomerKey;
39+
40+
IF @total_amount < 500000
41+
SET @category = 'REGULAR';
42+
ELSE IF @total_amount < 1000000
43+
SET @category = 'GOLD';
44+
ELSE
45+
SET @category = 'PLATINUM';
46+
47+
RETURN @category;
48+
END
49+
GO
50+
51+
SELECT * FROM sys.sql_modules
52+
WHERE object_id = OBJECT_ID('ufn_customer_category')
53+
GO
54+
55+
-- Before (show actual query execution plan for legacy behavior)
56+
SELECT TOP 100
57+
[Customer Key], [Customer],
58+
dbo.ufn_customer_category([Customer Key]) AS [Discount Price]
59+
FROM [Dimension].[Customer]
60+
ORDER BY [Customer Key]
61+
OPTION (RECOMPILE,USE HINT('DISABLE_TSQL_SCALAR_UDF_INLINING'));
62+
GO
63+
64+
-- After (show actual query execution plan for Scalar UDF Inlining)
65+
SELECT TOP 100
66+
[Customer Key], [Customer],
67+
dbo.ufn_customer_category([Customer Key]) AS [Discount Price]
68+
FROM [Dimension].[Customer]
69+
ORDER BY [Customer Key]
70+
OPTION (RECOMPILE);
71+
GO
72+

0 commit comments

Comments
 (0)