CREATE PROCEDUE SearchDB @param int AS BEGIN SELECT col1, col2, col3 FROM tb1 INNER JOIN tb2 ON tb1.col1 = tb2.otherColumn WHERE dbo.IsMatch(@param, tb1.col1) END
This runs fine in SQL Management Studio, and gives the expected results. But when I call the stored proc from an ASP.NET application, the query never completes - it times out.
I know I'm connected to SQL Server, I can access other stored procs in the same database just fine. It is the dbo.IsMatch() user-defined function that is causing SQL Server to timeout -- if I replace the function with, say, 'WHERE tb2.col1 = 5' it works fine from .NET.
Anyone have any ideas? I'm a little lost on this one. The really weird thing is that another stored proc that uses the dbo.IsMatch() function runs fine from .NET.
I'm guessing you have much more data in the live ASP.NET environment and using a function like that will perform very badly with lots of data - badly enough that it times out in your case.
In cases like that where the function is probably doing something pretty simple anyway, and you're query lots of data, just put the expression right in the query and it will probably run considerably faster.
Also check your indexing, do you have an index on tb1.col1 (I don't know what the function does so I can't say for sure that you need one, but you probably do for the join anyway)?
Thank you, snSQL, you were correct. The amount of data was much larger in this case, causing it to timeout. I rewrote the query without the function, which resulted in a larger 'WHERE' clause but the query runs many orders of magnitude faster now.