[英]Linq to Entities Query Optimisation
我有一個LINQ查詢,它將根據是否滿足一些條件來選擇票證。 該查詢本身運行良好且可以正常運行,但是我擔心隨着可能的返回值數量的增加,它會變得更糟。
誰能提出改進或優化此查詢以使其運行更快/更好的方法嗎? 我在違反標准做法時做錯了什么嗎?
這是查詢。 我換了一些名字。 我想的是, 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();
編輯:所以我創建了一個存儲過程,盡管我不是SQL方面的佼佼者。 這個可以嗎?
@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
因此,似乎在我的初始語句中使用類似於存儲過程的存儲過程比使用丑陋的LINQ OR語句要有效得多。
我將其標記為答案,以便將其關閉。 看看我對SPROC的原始疑問。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.