c# - How to optimize SQL query generated by Entity Framework in SQL Server Management Studio? -


i create query in linq returns table of active salesmen in shop:

projectdb3context db = new projectdb3context();  db.database.log = message => trace.writeline(message); var result = db.tblusers.join(db.tblsales,                               u => u.id,                               sl => sl.tbluserid,                               (u, sl) => new { u, sl })                .select(o => new                             {                                 userid = o.u.id,                                 login = o.u.userlogin,                                 fullname = o.u.name + " " +  o.u.surname,                                 itemstosell = db.tblsales.where(x => x.tbluserid == o.u.id).count()                             })                .distinct()                .orderbydescending(x => x.itemstosell)                .tolist(); 

the henerated sql query looks like:

select      [distinct1].[c1] [c1],      [distinct1].[id] [id],      [distinct1].[userlogin] [userlogin],      [distinct1].[c2] [c2],      [distinct1].[c3] [c3]     ( select distinct          [project1].[id] [id],          [project1].[userlogin] [userlogin],          1 [c1],          [project1].[name] + n' ' + [project1].[surname] [c2],          [project1].[c1] [c3]         ( select              [extent1].[id] [id],              [extent1].[userlogin] [userlogin],              [extent1].[name] [name],              [extent1].[surname] [surname],              (select                  count(1) [a1]                 [dbo].[tblsale] [extent3]                 [extent3].[tbluserid] = [extent1].[id]) [c1]              [dbo].[tbluser] [extent1]             inner join [dbo].[tblsale] [extent2] on [extent1].[id] = [extent2].[tbluserid]         )  [project1]     )  [distinct1]     order [distinct1].[c3] desc 

statistics:

sql server execution times: cpu time = 359 ms,  elapsed time = 529 ms. 

execution plan screen shot

i want optimize generated sql query , insert optimized query stored procedure. sql server management studio gives me tip create nonclustered index (tbluserid) on tblsale (you can see tip in image included).

when create using command:

create nonclustered index ix_productvendor_tbluserid  on tblsale (tbluserid);  

and run sql query in sql server management studio get:

sql server execution times: cpu time = 328 ms,  elapsed time = 631 ms. 

so takes longer after used index optimize sql query.

can me optimize query in sql server using indexes?

can me optimize query in sql server using indexes?

first off, before trying optimize sql query in database, make sure linq query optimal. not case yours. there unnecessary join in turn requires distinct etc. , tblsales accessed twice (see generated sql).

what trying achieve users sales ordered sales count descending. following simple query should produce desired result

var result = db.tblusers    .select(u => new    {        userid = u.id,        login = u.userlogin,        fullname = u.name + " " +  u.surname,        itemstosell = db.tblsales.count(s => s.tbluserid == u.id)     })     .where(x => x.itemstosel > 0)     .orderbydescending(x => x.itemstosell)     .tolist(); 

try , see new execution plan/time.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -