首页 > 解决方案 > 在列中检索大值的更快方法

问题描述

我在 Azure 中使用 MVC、EF & Repository 和数据库。我有一张桌子,只有 5 列。ID、名称、消息、例外和其他详细信息。异常和其他细节,两者都是 varchar(max) 数据类型。现在,Exceptions 列的最大字符串长度为 2300 字,而 otherdetails 列的最大字符串长度为 1500 字。

谁能告诉我,找到整个数据的更快方法?因为现在我正在搜索一天的数据,所以执行需要将近 20 秒或更长时间。我在 SQL 中执行了相同的查询。但它花了同样的时间。另外,我尝试使用表格视图但同时执行。这是我的代码-

public IEnumerable<ErrorLogModel> GetErrorLogData(DateTime? startDate, DateTime? endDate)
    {
        return _unitOfWork.ErrorLogRepository.Get(e.Date >= startDate && e.Date <= endDate).Select(e => new ErrorLogModel
        {
            ID= e.ID,
            name = e.name,
            message= e.message,
            Exceptions = e.Exceptions,
            otherdetails = e.otherdetails
        });

    }

标签: c#sqlasp.net-mvcentity-frameworkazure

解决方案


索引可以提高查询的性能,现在它会扫描所有表,因此查询工作非常缓慢。

EF 6.1 支持索引,尝试像这样使用它:https ://docs.microsoft.com/ru-ru/ef/ef6/modeling/code-first/data-annotations#index

[Index]
public DateTime ExceptionDate { get; set; } 

如果您更喜欢 FluentAPI 并使用 EF 6.2,您可以使用以下答案:How to create index in Entity Framework 6.2 with code first


推荐阅读