A query gets executed within seconds when run from the SQL Server client. However, the very same query takes up to 50 seconds when my application executes it. The application uses an XA datasource and the query shows up as an RPC call in the SQL Profiler. What's up with that?
Whenever I see 40 or 50 second delays, it suggests name resolution problems on the network, particularly if this happens when the database hasn't been accessed for a few minutes. The default DNS resolution normally times out after about 40 seconds and then tries to resolve the name via broadcast. After that, it's cached for a little while and seems to work fine.
Dig Deeper on Microsoft SQL Server Performance Monitoring and Tuning
Related Q&A from Greg Low
Should developers be granted permissions to production queue in a SQL Server environment? See why expert Greg Low suggests proc access by WITH ...
Find how to save stored procedure output to the network drive via SQL Server Management Studio.
Find how to query in SQL Server to get all database names created by users.