此代码块从数据库收集作业顺序信息,将表连接在一起,并返回一个对象。那么一切都像魅力一样,但太慢了?
这是完成工作的代码;
public IQueryable<object> JsonGetJobOrdersBy(int status, int companyID)
{
IQueryable<object> result = this.db.JobOrder.Where(j => j.Status == status && j.RelatedCompanyID == companyID)
.Select(x => new
{
JobOrderID = x.JobOrderID,
CustomerName = x.Customers.Name,
CustomerSurName = x.Customers.Surname,
SerialNo = x.SerialNo,
DeviceName = x.DeviceModel.DeviceName,
BrandName = x.DeviceModel.DeviceBrand.BrandName,
State = x.State,
ServiceType = x.ServiceType,
InsertDate = x.InsertDate,
StaffFirstName = x.AttendedStaff.FirstOrDefault().SystemUsers.StaffFirstName,
StaffSurName = x.AttendedStaff.FirstOrDefault().SystemUsers.StaffSurName,
Importance = x.Importance
}).OrderByDescending(x => x.JobOrderID);
return result;
}
这个块产生了那么长的sql
SELECT
[Project6].[JobOrderID] AS [JobOrderID],
[Project6].[Name] AS [Name],
[Project6].[Surname] AS [Surname],
[Project6].[SerialNo] AS [SerialNo],
[Project6].[DeviceName] AS [DeviceName],
[Project6].[BrandName] AS [BrandName],
[Project6].[State] AS [State],
[Project6].[ServiceType] AS [ServiceType],
[Project6].[InsertDate] AS [InsertDate],
[Project6].[StaffFirstName] AS [StaffFirstName],
[Project6].[StaffSurName] AS [StaffSurName],
[Project6].[Importance] AS [Importance]
FROM ( SELECT
[Project5].[JobOrderID] AS [JobOrderID],
[Project5].[SerialNo] AS [SerialNo],
[Project5].[Importance] AS [Importance],
[Project5].[ServiceType] AS [ServiceType],
[Project5].[State] AS [State],
[Project5].[InsertDate] AS [InsertDate],
[Project5].[Name] AS [Name],
[Project5].[Surname] AS [Surname],
[Project5].[DeviceName] AS [DeviceName],
[Project5].[BrandName] AS [BrandName],
[Project5].[StaffFirstName] AS [StaffFirstName],
[Extent8].[StaffSurName] AS [StaffSurName]
FROM (SELECT
[Project3].[JobOrderID] AS [JobOrderID],
[Project3].[SerialNo] AS [SerialNo],
[Project3].[Importance] AS [Importance],
[Project3].[ServiceType] AS [ServiceType],
[Project3].[State] AS [State],
[Project3].[InsertDate] AS [InsertDate],
[Project3].[Name] AS [Name],
[Project3].[Surname] AS [Surname],
[Project3].[DeviceName] AS [DeviceName],
[Project3].[BrandName] AS [BrandName],
[Project3].[StaffFirstName] AS [StaffFirstName],
(SELECT TOP (1)
[Extent7].[StaffID] AS [StaffID]
FROM [dbo].[AttendedStaff] AS [Extent7]
WHERE [Project3].[JobOrderID] = [Extent7].[JobOrderID]) AS [C1]
FROM ( SELECT
[Project2].[JobOrderID] AS [JobOrderID],
[Project2].[SerialNo] AS [SerialNo],
[Project2].[Importance] AS [Importance],
[Project2].[ServiceType] AS [ServiceType],
[Project2].[State] AS [State],
[Project2].[InsertDate] AS [InsertDate],
[Project2].[Name] AS [Name],
[Project2].[Surname] AS [Surname],
[Project2].[DeviceName] AS [DeviceName],
[Project2].[BrandName] AS [BrandName],
[Extent6].[StaffFirstName] AS [StaffFirstName]
FROM (SELECT
[Extent1].[JobOrderID] AS [JobOrderID],
[Extent1].[SerialNo] AS [SerialNo],
[Extent1].[Importance] AS [Importance],
[Extent1].[ServiceType] AS [ServiceType],
[Extent1].[State] AS [State],
[Extent1].[InsertDate] AS [InsertDate],
[Extent2].[Name] AS [Name],
[Extent2].[Surname] AS [Surname],
[Extent3].[DeviceName] AS [DeviceName],
[Extent4].[BrandName] AS [BrandName],
(SELECT TOP (1)
[Extent5].[StaffID] AS [StaffID]
FROM [dbo].[AttendedStaff] AS [Extent5]
WHERE [Extent1].[JobOrderID] = [Extent5].[JobOrderID]) AS [C1]
FROM [dbo].[JobOrder] AS [Extent1]
INNER JOIN [dbo].[Customers] AS [Extent2] ON [Extent1].[CustomerID] = [Extent2].[CustomerID]
INNER JOIN [dbo].[DeviceModel] AS [Extent3] ON [Extent1].[DeviceModelID] = [Extent3].[DeviceModelID]
LEFT OUTER JOIN [dbo].[DeviceBrand] AS [Extent4] ON [Extent3].[BrandID] = [Extent4].[DeviceBrandID]
WHERE ([Extent1].[Status] = @p__linq__0) AND ([Extent1].[RelatedCompanyID] = @p__linq__1) ) AS [Project2]
LEFT OUTER JOIN [dbo].[SystemUsers] AS [Extent6] ON [Project2].[C1] = [Extent6].[UserID]
) AS [Project3] ) AS [Project5]
LEFT OUTER JOIN [dbo].[SystemUsers] AS [Extent8] ON [Project5].[C1] = [Extent8].[UserID]
) AS [Project6]
ORDER BY [Project6].[JobOrderID] DESC
好吧,我无法判断长期查询是否合适。我需要帮助才能创建一些更短且性能更好的查询。
答案 0 :(得分:0)
首先要确保在您的查询中包含(加入)您在select语句中使用的任何关系。根据你的select语句,我猜你需要添加类似的东西:
.Include("Customers").Include("DeviceModel").Include("AttendedStaff.SystemUsers")
接下来,您要查询的任何内容都应该是一个索引字段,即Status
和RelatedCompanyID
。