简体   繁体   English

Linq to实体查询优化

[英]Linq to Entities Query Optimisation

I've got a LINQ query that will select a ticket based on whether it satisfies a few conditions. 我有一个LINQ查询,它将根据是否满足一些条件来选择票证。 The query itself runs fine and works okay, but I'm worried that with an increase in the number of possible returns it will get noticeably worse as it goes. 该查询本身运行良好且可以正常运行,但是我担心随着可能的返回值数量的增加,它会变得更糟。

Could anyone suggest ways to improve or optimise this query to run faster/better? 谁能提出改进或优化此查询以使其运行更快/更好的方法吗? Am I doing something horribly wrong against standard practice? 我在违反标准做法时做错了什么吗?

Here's the query. 这是查询。 I've swapped some names. 我换了一些名字。 I'm thinking that the select 's in select new statement are a problem as well. 我想的是, select的在select new语句是一个问题为好。

var attentionObj = (from c in context.SupportTicketEntities
                    where
                    c.StatusID != 3 && 
                    ((c.IsAllocated == false)    // not allocated
                    || (c.FlaggedForAssist == true && c.AllocatedToEmployeeID == empId)     // is flagged for assist
                    || ((from d in c.SupportTicketDatas
                         where c.AllocatedToEmployeeID == empId
                         && c.FogBugzAttachments.Count == 0
                         orderby d.TimeStamp descending
                         select d.TimeStamp).FirstOrDefault() < warningDt)   // ticket not replied to
                    || ((from e in c.SupportTicketDatas
                         where c.AllocatedToEmployeeID == empId
                         orderby e.TimeStamp descending
                         select e).FirstOrDefault().RaisedByUser == true)   // customer has replied
                    || (c.IsEscalated == true))  // ticket escalated
                    select new
                    {
                        RefID = c.RefID,
                        TicketID = c.SupportTicketID,
                        ClientCompany = c.ClientData.Company,
                        IsAllocated = c.IsAllocated,
                        DateLogged = c.DateLogged,
                        IsFlagged = c.FlaggedForAssist,
                        NeedsReply = (from d in c.SupportTicketDatas
                                      orderby d.TimeStamp descending
                                      select d.TimeStamp).FirstOrDefault() < warningDt,
                        CustReply = (from e in c.SupportTicketDatas
                                     orderby e.TimeStamp descending
                                     select e).FirstOrDefault().RaisedByUser == true,
                        IsEscalated = c.IsEscalated
                    }).ToArray();

EDIT: So I've created a stored procedure, although I'm not the best at SQL. 编辑:所以我创建了一个存储过程,尽管我不是SQL方面的佼佼者。 Is this okay? 这个可以吗?

@empId int,
@warningDt datetime
AS
SELECT * FROM SupportTickets
WHERE StatusID != 3
AND IsAllocated = 0
UNION
SELECT * FROM SupportTickets
WHERE StatusID != 3
AND FlaggedForAssist = 1
AND AllocatedToEmployeeID = @empId
UNION
SELECT * FROM SupportTickets
WHERE StatusID != 3
AND AllocatedToEmployeeID = @empId
AND NOT EXISTS (SELECT FogBugzLinkID FROM FogBugzAttachment)
AND (SELECT MAX(TimeStamp) 
 FROM SupportTicketData
 WHERE SupportTickets.SupportTicketID = SupportTicketData.SupportTicketID) < @warningDt
UNION
SELECT SupportTickets.* FROM SupportTickets
JOIN SupportTicketData ON SupportTickets.SupportTicketID = SupportTicketData.SupportTicketID
WHERE SupportTickets.StatusID != 3
AND SupportTickets.AllocatedToEmployeeID = @empId
AND SupportTicketData.TimeStamp = (SELECT MAX(TimeStamp) FROM SupportTicketData
                   WHERE SupportTickets.SupportTicketID = SupportTicketData.SupportTicketID)
AND SupportTicketData.RaisedByUser = 1
UNION
SELECT * FROM SupportTickets
WHERE StatusID != 3
AND SupportTickets.IsEscalated = 1

So it appears that using a stored procedure like the one in my initial statement is much much more efficient than using the hideous LINQ OR statement. 因此,似乎在我的初始语句中使用类似于存储过程的存储过程比使用丑陋的LINQ OR语句要有效得多。

I'll mark this as the answer so it gets closed. 我将其标记为答案,以便将其关闭。 Look at my original question for the SPROC. 看看我对SPROC的原始疑问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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