Slow query using non-deterministic user defined function

Published Jan 15 2019 04:04 PM 216 Views
Microsoft
First published on MSDN on Jul 08, 2014

Recently we worked with a customer who reported a query that used to run a few seconds in SQL Server 2000 but it never finishes in SQL Server 2008 R2 following upgrade.


We went around and tried quite a few things but couldn't get SQL Server 2008 R2 to generate similar plan. Upon closer look at 2008 R2's query plan, we noticed something unusual. The plan has a warning "NO JOIN PREDICATE". What this means is that a cartesian product is introduced.


To illustrate the problem, let's use an example setup:






drop function dbo.myfunc
go
drop view v1, v2
go
drop table t1, t2
go
create table t1 (c1 int not null, c2 varchar(100))
go
create table t2 (c1 int not null, c2 varchar(100))
go
set nocount on
go
declare @i int
begin tran
select @i = 0
while (@i < 1000)
begin
insert into t1 (c1, c2) values (@i, 'a')
insert into t2 (c1, c2) values (@i, 'b')
select @i = @i + 1
end
commit tran
go
drop function dbo.myFunc
go
create function dbo.myfunc (@c1 int)
returns int
--with schemabinding
as
begin
return (@c1 * 100 )
end
go
create view v1 as select c1, c2, dbo.myfunc(c1) as c3 from t1
go
create view v2 as select c1, c2, dbo.myfunc(c1) as c3 from t2
go





Now, let's run the following query


dbcc freeproccache
go
set statistics profile on
go


-- But by pulling UDF above join in this query we actually introduce a cartesian product (NO JOIN PREDICATE)
-- UDF is called 1 million times instead of 1000 times each for the two views!
select count(*) from v1 as t1 join v2 as t2 on t1.c3 = t2.c3
go
set statistics profile off
go



The above query is very slow as illustrated in the query plan below. In the line 6 for the query plan, there is a warning "no join predicate". The join resulted in 1,000,000 rows (1,000 x 1,000 rows from each table).


In line 5, the myfunc is called 2,000,000 times (1,000,000 for computing t1.c1 and 1,000,000 for t2.c1).


This is because starting SQL Server 2005, optimizer has rule changes that will disallow non-deterministic scalar functions to be 'pushed down' in some situations (like this one).





Solution



Many times, you can simply make a function deterministic by adding schemabinding option. In the above example, re-write the function with schemabinding, it will be much faster.


From the query plan, you will no longer see that the "NO JOIN PREDICATE". The scalare UDF is pushed down right after table scan and applied only 100 times on each table.


drop function dbo.myFunc
go
create function dbo.myfunc (@c1 int)
returns int
with schemabinding
as
begin
return (@c1 * 100 )
end






Obviously, the function can be made deterministic. If you use following, the function will not be deterministic even you use schemabidning because of getdate(). In such cases, you will continue to see "NO JOIN PREDICATE" Cartesian product joins.


drop function dbo.myFunc
go
create function dbo.myfunc (@c1 int)
returns int
with schemabinding
as
begin
return (@c1 * 100 * datepart (mm,getdate()))
end





Jack Li | Senior Escalation Engineer | Microsoft SQL Server Support



%3CLINGO-SUB%20id%3D%22lingo-sub-318364%22%20slang%3D%22en-US%22%3ESlow%20query%20using%20non-deterministic%20user%20defined%20function%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-318364%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20Jul%2008%2C%202014%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3ERecently%20we%20worked%20with%20a%20customer%20who%20reported%20a%20query%20that%20used%20to%20run%20a%20few%20seconds%20in%20SQL%20Server%202000%20but%20it%20never%20finishes%20in%20SQL%20Server%202008%20R2%20following%20upgrade.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EWe%20went%20around%20and%20tried%20quite%20a%20few%20things%20but%20couldn't%20get%20SQL%20Server%202008%20R2%20to%20generate%20similar%20plan.%20Upon%20closer%20look%20at%202008%20R2's%20query%20plan%2C%20we%20noticed%20something%20unusual.%20The%20plan%20has%20a%20warning%20%22NO%20JOIN%20PREDICATE%22.%20What%20this%20means%20is%20that%20a%20cartesian%20product%20is%20introduced.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ETo%20illustrate%20the%20problem%2C%20let's%20use%20an%20example%20setup%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3Edrop%20function%20dbo.myfunc%20%3CBR%20%2F%3E%20go%20%3CBR%20%2F%3E%20drop%20view%20v1%2C%20v2%20%3CBR%20%2F%3E%20go%20%3CBR%20%2F%3E%20drop%20table%20t1%2C%20t2%20%3CBR%20%2F%3E%20go%20%3CBR%20%2F%3E%20create%20table%20t1%20(c1%20int%20not%20null%2C%20c2%20varchar(100))%20%3CBR%20%2F%3E%20go%20%3CBR%20%2F%3E%20create%20table%20t2%20(c1%20int%20not%20null%2C%20c2%20varchar(100))%20%3CBR%20%2F%3E%20go%20%3CBR%20%2F%3E%20set%20nocount%20on%20%3CBR%20%2F%3E%20go%20%3CBR%20%2F%3E%20declare%20%40i%20int%20%3CBR%20%2F%3E%20begin%20tran%20%3CBR%20%2F%3E%20select%20%40i%20%3D%200%20%3CBR%20%2F%3E%20while%20(%40i%20%26lt%3B%201000)%20%3CBR%20%2F%3E%20begin%20%3CBR%20%2F%3E%20insert%20into%20t1%20(c1%2C%20c2)%20values%20(%40i%2C%20'a')%20%3CBR%20%2F%3E%20insert%20into%20t2%20(c1%2C%20c2)%20values%20(%40i%2C%20'b')%20%3CBR%20%2F%3E%20select%20%40i%20%3D%20%40i%20%2B%201%20%3CBR%20%2F%3E%20end%20%3CBR%20%2F%3E%20commit%20tran%20%3CBR%20%2F%3E%20go%20%3CBR%20%2F%3E%20drop%20function%20dbo.myFunc%20%3CBR%20%2F%3E%20go%20%3CBR%20%2F%3E%20create%20function%20dbo.myfunc%20(%40c1%20int)%20%3CBR%20%2F%3E%20returns%20int%20%3CBR%20%2F%3E%20--with%20schemabinding%20%3CBR%20%2F%3E%20as%20%3CBR%20%2F%3E%20begin%20%3CBR%20%2F%3E%20return%20(%40c1%20*%20100%20)%20%3CBR%20%2F%3E%20end%20%3CBR%20%2F%3E%20go%20%3CBR%20%2F%3E%20create%20view%20v1%20as%20select%20c1%2C%20c2%2C%20dbo.myfunc(c1)%20as%20c3%20from%20t1%20%3CBR%20%2F%3E%20go%20%3CBR%20%2F%3E%20create%20view%20v2%20as%20select%20c1%2C%20c2%2C%20dbo.myfunc(c1)%20as%20c3%20from%20t2%20%3CBR%20%2F%3E%20go%3C%2FP%3E%3CBR%20%2F%3E%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ENow%2C%20let's%20run%20the%20following%20query%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Edbcc%20freeproccache%20%3CBR%20%2F%3E%20go%20%3CBR%20%2F%3E%20set%20statistics%20profile%20on%20%3CBR%20%2F%3E%20go%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E--%20But%20by%20pulling%20UDF%20above%20join%20in%20this%20query%20we%20actually%20introduce%20a%20cartesian%20product%20(NO%20JOIN%20PREDICATE)%20%3CBR%20%2F%3E%20--%20UDF%20is%20called%201%20million%20times%20instead%20of%201000%20times%20each%20for%20the%20two%20views!%20%3CBR%20%2F%3E%20select%20count(*)%20from%20v1%20as%20t1%20join%20v2%20as%20t2%20on%20t1.c3%20%3D%20t2.c3%20%3CBR%20%2F%3E%20go%20%3CBR%20%2F%3E%20set%20statistics%20profile%20off%20%3CBR%20%2F%3E%20go%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EThe%20above%20query%20is%20very%20slow%20as%20illustrated%20in%20the%20query%20plan%20below.%20In%20the%20line%206%20for%20the%20query%20plan%2C%20there%20is%20a%20warning%20%22no%20join%20predicate%22.%20The%20join%20resulted%20in%201%2C000%2C000%20rows%20(1%2C000%20x%201%2C000%20rows%20from%20each%20table).%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EIn%20line%205%2C%20the%20myfunc%20is%20called%202%2C000%2C000%20times%20(1%2C000%2C000%20for%20computing%20t1.c1%20and%201%2C000%2C000%20for%20t2.c1).%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EThis%20is%20because%20starting%20SQL%20Server%202005%2C%20optimizer%20has%20rule%20changes%20that%20will%20disallow%20non-deterministic%20scalar%20functions%20to%20be%20'pushed%20down'%20in%20some%20situations%20(like%20this%20one).%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F68155i1F52C48BE07E8FD5%22%20%2F%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%3CH2%20id%3D%22toc-hId--1300931056%22%20id%3D%22toc-hId--1237027216%22%3ESolution%3C%2FH2%3E%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EMany%20times%2C%20you%20can%20simply%20make%20a%20function%20deterministic%20by%20adding%20schemabinding%20option.%20In%20the%20above%20example%2C%20re-write%20the%20function%20with%20schemabinding%2C%20it%20will%20be%20much%20faster.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EFrom%20the%20query%20plan%2C%20you%20will%20no%20longer%20see%20that%20the%20%22NO%20JOIN%20PREDICATE%22.%20The%20scalare%20UDF%20is%20pushed%20down%20right%20after%20table%20scan%20and%20applied%20only%20100%20times%20on%20each%20table.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Edrop%20function%20dbo.myFunc%20%3CBR%20%2F%3E%20go%20%3CBR%20%2F%3E%20create%20function%20dbo.myfunc%20(%40c1%20int)%20%3CBR%20%2F%3E%20returns%20int%20%3CBR%20%2F%3E%20with%20schemabinding%20%3CBR%20%2F%3E%20as%20%3CBR%20%2F%3E%20begin%20%3CBR%20%2F%3E%20return%20(%40c1%20*%20100%20)%20%3CBR%20%2F%3E%20end%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F68156i1334A0F48EF1BCA2%22%20%2F%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EObviously%2C%20the%20function%20can%20be%20made%20deterministic.%20If%20you%20use%20following%2C%20the%20function%20will%20not%20be%20deterministic%20even%20you%20use%20schemabidning%20because%20of%20getdate().%20In%20such%20cases%2C%20you%20will%20continue%20to%20see%20%22NO%20JOIN%20PREDICATE%22%20Cartesian%20product%20joins.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Edrop%20function%20dbo.myFunc%20%3CBR%20%2F%3E%20go%20%3CBR%20%2F%3E%20create%20function%20dbo.myfunc%20(%40c1%20int)%20%3CBR%20%2F%3E%20returns%20int%20%3CBR%20%2F%3E%20with%20schemabinding%20%3CBR%20%2F%3E%20as%20%3CBR%20%2F%3E%20begin%20%3CBR%20%2F%3E%20return%20(%40c1%20*%20100%20*%20datepart%20(mm%2Cgetdate()))%20%3CBR%20%2F%3E%20end%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EJack%20Li%20%7C%20Senior%20Escalation%20Engineer%20%7C%20Microsoft%20SQL%20Server%20Support%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%0A%20%0A%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-318364%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Jul%2008%2C%202014%20Recently%20we%20worked%20with%20a%20customer%20who%20reported%20a%20query%20that%20used%20to%20run%20a%20few%20seconds%20in%20SQL%20Server%202000%20but%20it%20never%20finishes%20in%20SQL%20Server%202008%20R2%20following%20upgrade.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-318364%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPerformance%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎Jan 15 2019 04:04 PM
Updated by: