Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq performance: Two queries, the first response immediately and the second is very slow

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

  1. Index 1(id_tank, date asc)
  2. 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.

like image 760
Juan Carlos Velez Avatar asked Dec 04 '25 11:12

Juan Carlos Velez


2 Answers

I resolved it after one week, viewing the execution plan (thanks Yahia by the suggestion)!!!

In the two queries I specified the INDEX (thanks Denis) and I had to specify the hint NOEXPAND.

NOEXPAND explanation can be found at: Table hints

So, the final queries are:

Query 1

IEnumerable<ViewDataTanksDataDevice> res= 
this.ExecuteQuery<ViewDataTanksDataDevice>(
   "SELECT TOP 1 * FROM ViewDataTanksDataDevicesSB with (index(IX_ViewDataTanksDataDevicesSB_TankIdDate) noexpand) WHERE id_tank = {0} AND date >= {1} order by date", 
   new object[] { tankId, date });

Query 2

IEnumerable<ViewDataTanksDataDevice> res= 
this.ExecuteQuery<ViewDataTanksDataDevice>(
   "SELECT TOP 1 * FROM ViewDataTanksDataDevicesSB with (index(IX_ViewDataTanksDataDevicesSB_TankIdDate) noexpand) WHERE id_tank = {0} AND date <= {1} order by date desc", 
   new object[] { tankId, date });
like image 148
Juan Carlos Velez Avatar answered Dec 06 '25 23:12

Juan Carlos Velez


Without a comparison of the query plans etc. this is hard to answer...

BUT from your description it seems that both queries are fast when run in SSMS and one of them is slow when run fron .NET - the reason for such a different behaviour might be in the settings used for the DB session, SSMS has defaults that differ from the defaults used in ADO.NET (which is what LINQ uses). A very detailed explanation including several hints on how to solve certain situations in this regard can be found here.

Please post more details, esp. query plans to get more specific help...

like image 44
Yahia Avatar answered Dec 07 '25 00:12

Yahia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!