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
- index 1(id_tank, date asc)
- 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
Post a Comment