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


i have 2 queries similars, using linq executequery method first take 30 seconds, while second query inmediate.

i execute queries in microsoft sql server management studio , 2 queries have response time of 0 seconds.

query 1 (slow)

ienumerable<viewdatatanksdatadevice> res=  this.executequery<viewdatatanksdatadevice>(    "select top 1 * viewdatatanksdatadevicessb id_tank = {0} , date >= {1} order date",     new object[] { tankid, date }); 

query 2 (fast)

ienumerable<viewdatatanksdatadevice> res=  this.executequery<viewdatatanksdatadevice>(    "select top 1 * viewdatatanksdatadevicessb id_tank = {0} , date <= {1}     order date desc",     new object[] { tankid, date }); 

note 1: viewdatatanksdatadevicessb view schema binding, , has 2 index

  1. index 1(id_tank, date asc)
  2. index 2(id_tank, date desc)

note 2: if execute first second query result identical: query1 slow , query 2 fast.

note 3: view have millions of registers, , the results identical differents dates , tanks.

i resolved after 1 week, viewing execution plan (thanks yahia suggestion)!!!

in 2 queries specified index (thanks denis) , had specify hint noexpand.

noexpand explanation can found at: table hints

so, final queries are:

query 1

ienumerable<viewdatatanksdatadevice> res=  this.executequery<viewdatatanksdatadevice>(    "select top 1 * viewdatatanksdatadevicessb (index(ix_viewdatatanksdatadevicessb_tankiddate) noexpand) id_tank = {0} , date >= {1} order date",     new object[] { tankid, date }); 

query 2

ienumerable<viewdatatanksdatadevice> res=  this.executequery<viewdatatanksdatadevice>(    "select top 1 * viewdatatanksdatadevicessb (index(ix_viewdatatanksdatadevicessb_tankiddate) noexpand) id_tank = {0} , date <= {1} order date desc",     new object[] { tankid, date }); 

Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -