I have a stored procedure which is going really slow. I ran the SP and looked at the execution plan and was able to see what was taking so long time.
The part that is slow:
DECLARE
@id int
,@date datetime
,@endDate datetime
SELECT @id = 3483
,@date = DATEADD(DAY, -10, GETDATE())
,@endDate = GETDATE()
SET NOCOUNT ON
SELECT *
,prevId = dbo.fnGetPrevId(id)
FROM dbo.table WITH(READUNCOMMITTED)
And the part in this query that is slow is where I call the function dbo.fnGetPrevId.
dbo.fnGetPrevId:
DECLARE @prevId int
SELECT TOP 1 @prevId = t2.id
FROM dbo.table2 AS t2 WITH(READUNCOMMITTED)
RETURN @prevId
Is this possible to rewrite for better performance without create index or something like that to table?