I have a huge difference of time execution between a 1-minute query and the same one in a table-valued function.
But the most weired thing is that running the UDF with another (valid) company_id argument gives me a result in ~40 seconds and as soon as I change this company_id for 12 (valid again), it never stops. The execution plans of these two queries are absolutely not the same and of course, the long one is the most complicated. BUT the execution plan between the batch version and the UDF version are the same AND the batch version is fast...!
If I do the following query "by hand", the execution time is 1min36s with 306 rows:
SELECT
dbo.date_only(Call.date) AS date,
count(DISTINCT customer_id) AS new_customers
FROM
Call
LEFT OUTER JOIN
dbo.company_new_customers(12, 2009, 2009) new_customers
ON dbo.date_only(new_customers.date) = dbo.date_only(Call.date)
WHERE
company_id = 12
AND year(Call.date) >= 2009
AND year(Call.date) <= 2009
GROUP BY
dbo.date_only(Call.date)
I stored this exactly same query in a function and ran it like that :
SELECT * FROM company_new_customers_count(12, 2009, 2009)
13 minutes for now that it is running... And I am sure that it will never give me any result.
Yesterday, I had the exact same infinite-loop-like behaviour during more than 4h (so I stopped it).
Here is the definition of the function:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION company_new_customers_count
(
@company_id int,
@start_year int,
@end_year int
)
RETURNS TABLE
AS
RETURN
(
SELECT
dbo.date_only(Call.date) AS date,
count(DISTINCT customer_id) AS new_customers
FROM
Call
LEFT OUTER JOIN
dbo.company_new_customers(@company_id, @start_year, @end_year) new_customers
ON dbo.date_only(new_customers.date) = dbo.date_only(Call.date)
WHERE
company_id = @company_id
AND year(Call.date) >= @start_year
AND year(Call.date) <= @end_year
GROUP BY
dbo.date_only(Call.date)
)
GO
I would be very happy to understand what is going on.
Thanks
Additional:
Definition of company_new_customers:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Description: Create the list of new customers of @company_id
-- in the given period.
-- =============================================
CREATE FUNCTION company_new_customers
(
@company_id int,
@start_year int,
@end_year int
)
RETURNS TABLE
AS
RETURN
(
SELECT
customer_id,
date
FROM
( -- select apparition dates of cutomers before @end_year
SELECT
min(date) AS date,
customer_id
FROM
Call
JOIN
Call_Customer ON Call_Customer.call_id = Call.call_id
WHERE
company_id = @company_id
AND year(date) <= @end_year
GROUP BY
customer_id
) new_customers
WHERE
year(date) >= @start_year -- select apparition dates of cutomers after @start_year
)
GO
Definition of date_only:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Julio Guerra
-- Create date: 14/10/2010
-- Description: Return only the date part of a datetime value
-- Example: date_only('2010-10-25 13:00:12') returns 2010-10-25
-- =============================================
CREATE FUNCTION date_only
(
@datetime datetime
)
RETURNS datetime
AS
BEGIN
RETURN dateadd(dd, 0, datediff(dd, 0, @datetime))
END
GO
Execution Plan of SELECT * FROM company_new_customers_count(8, 2009, 2009)
Execution Plan of SELECT * FROM company_new_customers_count(12, 2009, 2009)