【发布时间】:2019-06-20 08:32:21
【问题描述】:
我有以下表格:
- 公司历史: Id StartDate EndDate
- CustomerHistory Id CompanyId StartDate EndDate
- CustomerPayment Id CustomerId StartDate EndDate 金额
我想制作一份报告,以搜索特定日期范围(firstDay、endDay)内的所有付款及其客户和公司。
我仅使用此代码为 CustomerPayment 执行此操作,并且我必须对 CustomerHistory 和 companyHistory 表执行相同操作。
而且性能真的很差。
有什么方法可以改进查询或者我应该改变我的数据库设计吗?
var payments= db.CustomerPayment.Where(s =>>
((firstDay <= s.EndDate && lastDay >= s.StartDate) &&
(lastDay <= s.EndDate || lastDay >= s.EndDate) &&
(firstDay > s.StartDate || firstDay < s.StartDate)) ||
((s.EndDate == null) && ((firstDay >= s.StartDate) ||
(firstDay <= s.StartDate && lastDay >= s.StartDate)));
【问题讨论】:
-
使用 SQL Profiler 获取该查询的 SQL,然后在该 SQL 上使用 SQL Tuning Advisor 查看缺少哪些索引。