简体   繁体   中英

LINQ-to-Entity query optimization

I have performance problem with one (pretty long) query which takes about 1:30 min to execute. I was able to locate the part which takes (too) long to execute, but now I need advices on how to optimize my query.

var ticketList = (from t in db.Ticket
                          select t).ToList();

        int idFirma = Convert.ToInt32(kontakt.idFirma);

        gvTicketi.DataSource = from t in ticketList
                               orderby t.idTicket, t.RedniBroj, t.DatumPrijave
                               select new
                               {
                                   t.idTicket,
                                   t.idFirma,
                                   t.idKontakt,
                                   t.idManager,
                                   t.idNadredeniTicket,
                                   TicketNumber = t.idNadredeniTicket + "-" + t.RedniBroj,
                                   t.Biljeske,
                                   t.DatumDo,
                                   t.DatumPrijave,
                                   t.OpciPrioritet,
                                   t.Opis,
                                   t.OpisZatvoren,
                                   t.Prioritet,
                                   t.Status,
                                   t.Tip,
                                   t.VrstaPrijave,
                                   t.Zatvoren,
                                   t.DatumZatvaranja,
                                   t.IzdanRacun,
                                   NazivKontakta = t.Kontakt == null ? "Bez kontakta" : t.Kontakt.Ime + " " + t.Kontakt.Prezime,
                                   Manager = t.idManager == null ? "Svi manageri" : (from k in db.Kontakt
                                                                                     where k.idKontakt == t.idManager
                                                                                     select k.Ime + " " + k.Prezime).SingleOrDefault(),
                                   NazivTvrtke = t.Firma.Naziv,
                                   DailyCount = db.Daily.Count(dt => dt.idTicket == t.idTicket && dt.Dolazak == true),
                                   DailySum = db.Daily.Count(dt => dt.idTicket == t.idTicket) == 0 ? 0 : db.Daily.Where(dt => dt.idTicket == t.idTicket).Sum(dts => dts.EfektivnoSati)
                               };

Performance issue #1

Manager = t.idManager == null ? "Svi manageri" : (from k in db.Kontakt where k.idKontakt == t.idManager select k.Ime + " " + k.Prezime).SingleOrDefault(),

Performance issue #2

DailyCount = db.Daily.Count(dt => dt.idTicket == t.idTicket && dt.Dolazak == true),
DailySum = db.Daily.Count(dt => dt.idTicket == t.idTicket) == 0 ? 0 : db.Daily.Where(dt => dt.idTicket == t.idTicket).Sum(dts => dts.EfektivnoSati)

By removing these two parts I was able to speed up a query to 8 seconds.

Any suggestion would be appreciated. Thank you!

You can improve this query in a single form

DailySum  = db.Daily.Where(dt => dt.idTicket == t.idTicket)
                     .Sum(dts => (int?)dts.EfektivnoSati)

For the rest:

  1. Daily table: add an index on idTicket column

  2. Kontakt table: add an index on idKontakt column (if is not already a PK)

Or better get the SQL generated and see how SSMS will help you with the indexes (this if you're using SQL Server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM