SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Inconsistent query plan for recursive
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Samuelg78
Starting Member

Singapore
4 Posts

Posted - 06/29/2012 :  01:02:36  Show Profile  Reply with Quote
I have 2833 rows in my table (PK for UserId Column, No other indexes). From the query plan which use Eager spool, it took 135ms. Sometimes i dun understand why it use another query plan without Eager spool which took 3sec instead. Can someone explain to me why? How can i force it to use Eager spool all the time?


CREATE FUNCTION [dbo].[mChildrenTree]
(	
	@UserId varchar(12),
	@IncludeSelf tinyint = 0
)
RETURNS @ChildrenTable TABLE
(
	UserId	varchar(12) primary key,
	ParentId	varchar(12),
	[Rank]	tinyint,
	[Role] varchar(5)
)
AS

Begin
		
If @IncludeSelf = 1
Begin
Insert Into @ChildrenTable
Select UserId, ParentId, [Rank], [Role]
From mMemberTree Where Userid = @UserId
End
			
;with show_byUserid (parentid, userid, [rank], [Role]) 
as 
(
select parentid, userid, [rank], [Role] from mMemberTree where ParentId = @UserId
union all
select z.parentid, z.userid, z.[rank], z.[Role] from mMemberTree z inner join show_byUserid t on z.parentid = t.userid
				
)

Insert Into @ChildrenTable
Select UserId, ParentId, [Rank], [Role]
From show_byUserid

RETURN
End


Eager spool

W/O Eager spool

visakh16
Very Important crosS Applying yaK Herder

India
47173 Posts

Posted - 06/29/2012 :  22:54:00  Show Profile  Reply with Quote
what were values for @IncludeSelf parameter in both case?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000