I have a SQL query which is taking a really long time based on a parameter check....
The query it self is part of a stored procedure used for a search screen. Basically you fill out a form with a whole lot of text/combo boxes to search from. I have multi-select combo boxes that will pass through multiple values for the same field in way of a delimited field.
The following is a cut down version of the query to illustrate my problem...
select o.id, o.createdBy into #results from jmsTransOther o where o.(WorkOrderId IN (SELECT intValue FROM dbo.fn_SplitInts(@WorkOrderIds, ',')) <strong>OR @WorkOrderIds = ''</strong>) I quite often use this parameter check OR @WorkOrderIds = '' which basically means if this is not true then perform the other side of the statement.
This works quite well in most cases but for some reason with this fn_SplitInts function which is basically converting the delimited list into a table and then performing a "IN" statement is taking a really long time.
This table has about 200,000 records in it - currently this takes around 40 seconds to search. However, if I remove the parameter check i.e. OR @WorkOrderIds = '', then it takes less than a second.
I can work around it but just wanted to know what is going on here....?
WITH RECOMPILEto your stored procedure. I suggest you do some research on parameter sniffing in general though, just so you understand whats going on