I've run into a bizzare problem that I can't seem to resolve: I have a query that I'm moving from ASP to ASP.NET and changing it to a paramaterized query at the same time. For some reason the paramterized query is MUCH slower than the non paramaterized query.
Here's a simplified example of the original query (in ASP)
Here's how I changed it in (ASP.NET)
then I set the parameters and run the query.... For some reason the code time's out running the query. So I fire up profiler and grab watch what's happening. Here's the code that get's sent to database server
I take this and feed it to SQL management studio.. It takes 43 seconds to run (ouch)
I then take that and modify it to this:
This takes under a second to return the same record set.
Does anybody have ANY ideas why there would be this huge difference in speed? I've never seen this happen before. I've never seen a difference in performance between the exec sp_executesql and just running the straight query. This is on SQL Server 2k5.
Mike, do you have any ideas?
Thanks.
Here's a simplified example of the original query (in ASP)
Code:
var SQL = "SELECT * FROM Table WHERE ID =" + ValueVariable
Here's how I changed it in (ASP.NET)
Code:
var SQL = "SELECT * FROM Table WHERE ID = @ValueVariable"
then I set the parameters and run the query.... For some reason the code time's out running the query. So I fire up profiler and grab watch what's happening. Here's the code that get's sent to database server
Code:
exec sp_executesql N'SELECT * FROM Table WHERE ID = @ValueVariable, N'@ValueVariable INT', @ValueVariable=1234
I then take that and modify it to this:
Code:
DECLARE @ValueVariable INT
SET @ValueVariable = 1234
SELECT * FROM Table WHERE ID = @ValueVariable
This takes under a second to return the same record set.
Does anybody have ANY ideas why there would be this huge difference in speed? I've never seen this happen before. I've never seen a difference in performance between the exec sp_executesql and just running the straight query. This is on SQL Server 2k5.
Mike, do you have any ideas?
Thanks.
