c# - 在列中检索大值的更快方法
问题描述
我在 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
});
}
解决方案
索引可以提高查询的性能,现在它会扫描所有表,因此查询工作非常缓慢。
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
推荐阅读
- php - 当我设置配置数据库时,它正在重置我的数据库连接
- c - 如何在 0 和 max 之间生成随机双精度(在 C 中)?
- android - 从 json api 获取嵌套值
- salesforce - 检查一百万条记录中是否存在数千条记录的有效方法
- javascript - 错误消息:未捕获的 TypeError:items.appendChild 不是函数
- html - Bootstrap 4 中的中心图像和 div 底层
- grammar - 这个递归规则中的顺序如何不给出相同的结果?
- c++ - 如何将调试器附加到 google v8?
- c# - 动态母版页,可访问两个母版中的变量
- javascript - find() 伪元素 jquery