我正在预订&在ASP.NET MVC5中开具发票的Web应用程序。 在我的预订索引页面(应用程序的主页面)上,我有一个搜索表单和一个显示PagedList内容的表。
目前预订表中有大约100.000条记录。他们中的大多数都有一个状态为DONE的发票。大多数查询适用于没有发票的预订或状态为PENDING的ivoice。 加载索引页大约需要8秒。 (空过滤器=>查询所有预订)这太长了......
大部分时间都花在从数据库中提取数据上。有什么方法可以加快查询速度?
回购:
public IQueryable<Booking> GetByFilter(SearchBookingViewModel model)
{
var bookings = _context.Bookings.Include(b => b.Invoice).Include(b => b.Customer).Include(b => b.Origin).Include(b => b.Destination);
if (!model.IncludeInvoiced)
{
bookings = bookings.Where(b => b.InvoiceID == null || b.Invoice.Status == InvoiceStatus.PENDING);
}
if (!string.IsNullOrWhiteSpace(model.Awb))
{
bookings = bookings.Where(b => b.Awb.ToUpper().Contains(model.Awb.ToUpper()));
}
if (!string.IsNullOrWhiteSpace(model.ClientRef))
{
bookings = bookings.Where(b => b.ClientRef.ToUpper().Contains(model.ClientRef.ToUpper()));
}
if (!string.IsNullOrWhiteSpace(model.CustomerCode))
{
bookings = bookings.Where(b => b.Customer.CustomerCode == model.CustomerCode);
}
if (!string.IsNullOrWhiteSpace(model.OriginCode))
{
bookings = bookings.Where(b => b.Origin.AirportCode == model.OriginCode);
}
if (!string.IsNullOrWhiteSpace(model.DestinationCode))
{
bookings = bookings.Where(b => b.Destination.AirportCode == model.DestinationCode);
}
if (model.FromDate.HasValue)
{
bookings = bookings.Where(b => b.BookingDate >= model.FromDate);
}
if (model.ToDate.HasValue)
{
bookings = bookings.Where(b => b.BookingDate <= model.ToDate);
}
return bookings.OrderByDescending(b => b.BookingID);
}
售票:
public class Booking
{
public int BookingID { get; set; }
public int? InvoiceID { get; set; }
public int CustomerID { get; set; }
public int OriginID { get; set; }
public int DestinationID { get; set; }
public BookingType BookingType { get; set; }
public string Awb { get; set; }
public string ClientRef { get; set; }
public DateTime BookingDate { get; set; }
public int NumberOfPieces { get; set; }
public int GrossWeight { get; set; }
public int? VolumetricWeight { get; set; }
public decimal? Rate { get; set; }
public decimal TotalCharges { get; set; }
public string Info { get; set; }
public string Location { get; set; }
// Navigation
public virtual Airport Origin { get; set; }
public virtual Airport Destination { get; set; }
public virtual ICollection<BookingDetail> BookingDetails { get; set; }
public virtual Customer Customer { get; set; }
public virtual Invoice Invoice { get; set; }
public virtual Pod Pod { get; set; }
}
控制器:
public ActionResult Search(Filter filter)
{
filter.Bookings = _db.Bookings.GetByFilter(filter).ToPagedList(filter.Page, filter.NumberPerPage);
return PartialView("_SearchResultsPartial", filter);
}
ToPagedList执行.Count()+ .Skip()。在IQueryable上执行()