I have what I thought was a simple query that I execute from my little log-processing application. The aim of this method is to simply get the highest date value out of the log table:
private DateTime GetLastEntryDate(string serverName, string siteName) { DateTime dt = DateTime.MinValue; using (var con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LogParserDB"].ConnectionString)) { con.Open(); using (var cmd = new SqlCommand("SELECT MAX(date) FROM iislogs WHERE host=@Host AND site=@Site", con)) { cmd.CommandTimeout = 120; cmd.Parameters.AddWithValue("Host", serverName); cmd.Parameters.AddWithValue("Site", siteName); var result = cmd.ExecuteScalar(); if (result != DBNull.Value) { dt = (DateTime)result; } } } return dt; }
There are some indexes on the table, but I'm not sure if that's relevant, because the problem I'm getting is that when I run this code, it throws a timeout after 2 minutes on the ExecuteScalar
line.
If I copy and paste that query into SSMS with the same parameters, it completes in 00:00:04, or even 00:00:00 (if I've just updated stats).
SELECT MAX(date) FROM iislogs WHERE host='servername' AND site='W3SVC1'
I have checked for blocking, and I can't see anything like that - that database is only being accessed by this one app, and it's not multi-threaded or anything like that.
Update: Interestingly, when I run the exact query as captured by Profiler, it also takes a long time in SSMS:
exec sp_executesql N'SELECT MAX(date) FROM iislogs WHERE host=@Host AND site=@Site',N'@Host nvarchar(13),@Site nvarchar(6)',@Host=N'servername',@Site=N'W3SVC1'
The actual columns are varchar(50)
and varchar(10)
respectively.