Here is the situation:
I have a table value function with a datetime parameter ,lest's say tdf(p_date) ,
that filters about two million rows selecting those with column date smaller than p_date and computes some aggregate values on other columns.
It works great but if p_date is a custom scalar value function (returning the end of day in my case) the execution plan is altered an the query goes from 1 sec to 1 minute execution time.
A proof of concept table - 1K products, 2M rows:
CREATE TABLE [dbo].[POC](
[Date] [datetime] NOT NULL,
[idProduct] [int] NOT NULL,
[Quantity] [int] NOT NULL
) ON [PRIMARY]
The inline table value function:
CREATE FUNCTION tdf (@p_date datetime)
RETURNS TABLE
AS
RETURN
(
SELECT idProduct, SUM(Quantity) AS TotalQuantity,
max(Date) as LastDate
FROM POC
WHERE (Date < @p_date)
GROUP BY idProduct
)
The scalar value function:
CREATE FUNCTION [dbo].[EndOfDay] (@date datetime)
RETURNS datetime
AS
BEGIN
DECLARE @res datetime
SET @res=dateadd(second, -1,
dateadd(day, 1,
dateadd(ms, -datepart(ms, @date),
dateadd(ss, -datepart(ss, @date),
dateadd(mi,- datepart(mi,@date),
dateadd(hh, -datepart(hh, @date), @date))))))
RETURN @res
END
Query 1 - Working great
SELECT * FROM [dbo].[tdf] (getdate())
The end of execution plan: Stream Aggregate Cost 13% <--- Clustered Index Scan Cost 86%
Query 2 - Not so great
SELECT * FROM [dbo].[tdf] (dbo.EndOfDay(getdate()))
The end of execution plan: Stream Aggregate Cost 4% <--- Filter Cost 12% <--- Clustered Index Scan Cost 86%
The overhead is your scalar function.
The TVF here is expanded like an inline macro so
SELECT * FROM [dbo].[tdf] (getdate())
becomes
SELECT idProduct, SUM(Quantity) AS TotalQuantity, max(Date) as LastDate
FROM POC
WHERE Date < getdate()
GROUP BY idProduct
When you use end of day scalar function, SQL can not evaluate the EOD(GETDATE()) as a constant. I can't find my article quickly on how SQL evaluates this stuff, sorry.
I guess that it's being evaluated for each row, not upfront as you want.
I'd calulate the EOD statement separately:
DECLARE @eod datetime;
SET @eod = dbo.EndOfDay(getdate());
SELECT * FROM [dbo].[tdf] (@eod)
I'd also use this for the EOD function:
DATEADD(second, -1, DATEADD(day, 1, (DATEDIFF(day, 0, @date))))
EDIT: Other question I answered
You can rewrite EndOfDay as an inline UDF too, and use nested inline UDFs. Examples:
Many nested inline UDFs are very fast
Calculating third Wednesday of the month with inline UDFs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With