我最近开始使用实体框架和代码优先迁移。我的应用程序现在是“实时”的,我开始看到事情变得越来越慢。我的数据库表中有大约 30.000 行使用最多。
这是一种我返回很多的方法,因为我必须返回表格,一个带有消息数据,一个带有每个收件人:
IQueryable<CompleteMessageModel> completeMessageModels =
from msg in db.NewMessageItems
join mr in db.MessageRecipients on msg.MessageId equals mr.MessageId
select
new CompleteMessageModel()
{
MessageId = msg.MessageId,
RecipientMessageId = mr.MessageRecipientId,
Title = msg.Title,
Message = msg.Message,
Recipients = msg.Recipients,
AuthorUserId = msg.AuthorId,
RecipientUserId = mr.RecipientId,
StatusCode = mr.StatusCode,
Timestamp = msg.Timestamp,
IsRead = mr.ReadTimestamp > 0,
ReadTimestamp = mr.ReadTimestamp,
GeoTag = msg.GeoTag
};
然后我使用这个 IQueryable 来询问高于某个时间戳和类似操作的消息。
我的问题是:这个查询可以进一步优化吗?
这是最常用查询的执行计划:
SELECT TOP (90)
[Project1].[MessageId] AS [MessageId],
[Project1].[MessageRecipientId] AS [MessageRecipientId],
[Project1].[Title] AS [Title],
[Project1].[Message] AS [Message],
[Project1].[Recipients] AS [Recipients],
[Project1].[AuthorId] AS [AuthorId],
[Project1].[RecipientId] AS [RecipientId],
[Project1].[StatusCode] AS [StatusCode],
[Project1].[Timestamp] AS [Timestamp],
[Project1].[C1] AS [C1],
[Project1].[ReadTimestamp] AS [ReadTimestamp],
[Project1].[GeoTag] AS [GeoTag]
FROM ( SELECT
[Extent1].[MessageId] AS [MessageId],
[Extent1].[Message] AS [Message],
[Extent1].[Title] AS [Title],
[Extent1].[AuthorId] AS [AuthorId],
[Extent1].[Timestamp] AS [Timestamp],
[Extent1].[Recipients] AS [Recipients],
[Extent1].[GeoTag] AS [GeoTag],
[Extent2].[MessageRecipientId] AS [MessageRecipientId],
[Extent2].[RecipientId] AS [RecipientId],
[Extent2].[ReadTimestamp] AS [ReadTimestamp],
[Extent2].[StatusCode] AS [StatusCode],
CASE WHEN ([Extent2].[ReadTimestamp] > 0) THEN cast(1 as bit) WHEN ( NOT ([Extent2].[ReadTimestamp] > 0)) THEN cast(0 as bit) END AS [C1]
FROM [dbo].[NewMessageModels] AS [Extent1]
INNER JOIN [dbo].[MessageRecipients] AS [Extent2] ON [Extent1].[MessageId] = [Extent2].[MessageId]
WHERE ([Extent2].[RecipientId] = @p__linq__0) AND (1 <> [Extent2].[StatusCode]) AND (3 <> [Extent2].[StatusCode]) AND ([Extent1].[Timestamp] >= @p__linq__1)
) AS [Project1]
ORDER BY [Project1].[Timestamp] DESC
如果它可以被优化,这在 c# 中会是什么样子?