1

I'm using Entity Framework 5 to connect to SQL Server 2008. I've imported a stored procedure into my project with no issues. The stored procedure has executed in the past with no issues at all up until I switched from using .GroupBy().First() in LINQ to get distinct results to getting unique results by using an OUTER APPLY in the stored procedure.

I switched to using an OUTER APPLY on SQL Server from the .GroupBy() in LINQ because, of course SQL is much faster.

Here is my SQL:

@USERID VARCHAR(MAX) = NULL,
@ITEMTYPE VARCHAR(MAX) = NULL,
@ITEMSUBTYPE VARCHAR(MAX) = NULL,
@ITEMGROUP VARCHAR(MAX) = NULL,
@ITEMNO VARCHAR(MAX) = NULL    

SELECT distinct orderformdump.itemno, 
case when @userid = '' then NULL else CAST((SELECT top 1 [UNITPRICE] FROM [ICPRICP] WHERE [ITEMNO] = ICITEM.ITEMNO AND [PRICELIST] in (select priclist from ARCUS where IDCUST in (select CUSTID from WEBLOGINACCESS where [USER] = @USERID)) and [CURRENCY] = 'CDN' and DPRICETYPE = 1) AS DECIMAL(18,2)) end as price,
isnull(deals.isindeal, CAST(0 AS BIT) ) isindeal
FROM ORDERFORMDUMP
INNER JOIN ICITEM ON ICITEM.FMTITEMNO = orderformdump.itemno
outer apply
(
 select top 1 CAST(1 AS BIT) as isindeal
 from PRD2 INNER JOIN PRH on PRD2.CODE = PRH.CODE 
 where ICITEM.ITEMNO = PRD2.ITEMNO and PRH.ACTIVE = 1  
 and cast(GETDATE() as DATE) between PRH.STARTDATE and isnull(PRH.ENDDATE, cast(GETDATE() as DATE))
) deals
where
(@ITEMNO IS NULL or ICITEM.FMTITEMNO = @ITEMNO)
and
(@ITEMSUBTYPE is null or ORDERFORMITEMSUBTYPEDUMP.ITEMSUBTYPE = @ITEMSUBTYPE)
and
(@ITEMTYPE is null or ORDERFORMITEMTYPEDUMP.ITEMTYPE = @ITEMTYPE)
and
(@ITEMGROUP is null or ORDERFORMITEMGROUPDUMP.ITEMGROUP = @ITEMGROUP)

Before, instead of the OUTER APPLY I had two LEFT JOINs and the isindeal column was inside a CASE statement:

...
case when PRH.ACTIVE = 1 and PRH.STARTDATE < cast(GETDATE() as DATE) and (PRH.ENDDATE IS NULL OR PRH.ENDDATE >= cast(GETDATE() as DATE)) then CAST(1 AS BIT) ELSE CAST(0 AS BIT) END as isindeal
...
LEFT JOIN PRD2 on ICITEM.ITEMNO = PRD2.ITEMNO
LEFT JOIN PRH on PRD2.CODE = PRH.CODE
...

Here's the imported class in Entity Framework:

public partial class PRODUCTS_Result
{
    public string itemno { get; set; }
    public Nullable<decimal> price { get; set; }
    public bool isindeal { get; set; }
}

This is how the stored procedure is called:

db.PRODUCTS(userid, itemtype, itemsubtype, itemgroup, itemno)

This is how it was being called before:

db.PRODUCTS(userid, itemtype, itemsubtype, itemgroup, itemno).GroupBy(i => i.itemno).Select(x => x.First());

Strangely enough, the timeout only happens when the stored procedure gets called with all null paramaters (ex. db.PRODUCTS(null, null, null, null, null))

I tried setting the timeout to 5 minutes instead of the default 30 seconds, ((IObjectContextAdapter)this).ObjectContext.CommandTimeout = 300;, but it just runs for the entire 5 minutes and still times out.

The stored procedure finishes execution in SQL Server in 3-5 seconds with all NULL values for the paramters, I've checked all the data types and they match up, so I've no idea why I get this timeout error only sometimes.

thetipsyhacker
  • 1,402
  • 4
  • 18
  • 39

1 Answers1

1

Short answer: add OPTION (RECOMPILE) to this query

Long answer: Read Erland Sommarskog's Dynamic Search Conditions in TSQL

David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67