I have two queries very similars, using the Linq ExecuteQuery method the first take 30 seconds, while the second query is inmediate.
I execute the queries too in Microsoft SQL Server Management Studio and the two queries have a response time of 0 seconds.
Query 1 (slow)
IEnumerable<ViewDataTanksDataDevice> res=
this.ExecuteQuery<ViewDataTanksDataDevice>(
"SELECT TOP 1 * FROM ViewDataTanksDataDevicesSB WHERE id_tank = {0} AND date >= {1} order by date",
new object[] { tankId, date });
Query 2 (fast)
IEnumerable<ViewDataTanksDataDevice> res=
this.ExecuteQuery<ViewDataTanksDataDevice>(
"SELECT TOP 1 * FROM ViewDataTanksDataDevicesSB WHERE id_tank = {0} AND date <= {1} order by date desc",
new object[] { tankId, date });
Note 1: ViewDataTanksDataDevicesSB is a view with SCHEMA BINDING, and it has two index
- Index 1(id_tank, date asc)
- Index 2(id_tank, date desc)
Note 2: If I execute first the second query the result is identical: Query1 slow and Query 2 fast.
Note 3: The view have millions of registers, and the the results are identical for differents dates and tanks.