We currently have a very simple query that returns the last row of a table for a given account.
SELECT TOP 1 * FROM tbl WHERE AccountID = @AccountID ORDER BY tblID DESC;
When filling a DataTable
from a DataAdapter
with this query using the parameter, it times out when there is no data in the table for that account. If there is data in table, it runs fine.
SqlCommand cmd = new SqlCommand(con);
cmd.commandText = "SELECT TOP 1 * FROM tbl WHERE AccountID = @AccountID ORDER BY tblID DESC"
cmd.Parameters.Add(new SqlParamter("@AccountID", 123));
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt); // TIMES OUT
If we change the query so that it doesn't use a parameter, it runs without issue.
"SELECT TOP 1 * FROM tbl WHERE AccountID = 123 ORDER BY tblID DESC"
The table is quite large (millions of rows), but the query without the paramter runs almost instantly.
Any advice would be appreciated