I have a confused problem. I have a SP that include a query with few JOINs (2 of them are LEFT JOINs). The SP take 10 seconds, and if I execute the query as a query - it's take 200 ms...
This is not parameter sniffing issue: even if I clean the cache and execute the SP with only one set of parameters - still slow. I also try to execute the SP with recompile, and to add option (recompile) in the query in the SP - still slow.
I try those links:
- Query runs fast, but runs slow in stored procedure
- SQL Server: Query fast, but slow from procedure
- Query times out when executed from web, but super-fast when executed from SSMS
I have to mention also that:
- 2 parameters of the SP are table type.
- There is no dynamic SQL in the SP.
So - what is the story here???
This is the SP code:
CREATE PROCEDURE [dbo].[spr_spr]
@ListOfIDs dbo.tt_IDsList READONLY, -- (one column - ID)
@ListOfTwoIDs dbo.tt_TwoIDsRelationList READONLY, -- (two columns - FirstID, SecondID)
@SomeID int = NULL
AS
IF @SomeID IS NULL
SELECT ..... ,
cast ( (CASE WHEN le.ID IS NOT NULL THEN 1 ELSE 0 END) as bit) as HasLinkedID
FROM @ListOfIDs ids
JOIN dbo.tbl1 ra ON ids.ID = ra.RR_RowID
JOIN dbo.tbl2 rr ON ra.RR_RowID = rr.RowID
JOIN dbo.tbl3 res ON res.tbl3ID = ra.tbl3ID
JOIN dbo.tbl4 cal ON cal.ObjectID = rr.ObjectID
JOIN @ListOfTwoIDs IdsRel ON cal.FirstID = IdsRel.FirstID
AND res.SecondID = IdsRel.SecondID
LEFT JOIN dbo.tbl5 p ON ra.tbl5ID = p.tbl5ID
LEFT JOIN dbo.tbl6 le ON le.tbl6ID = ra.tbl6ID
ELSE
.... -- same query with one change