实体框架核心如何提高查询速度

时间:2018-08-11 16:38:59

标签: performance linq timezone entity-framework-core

所以使用Entity Framework Core还是一个新手,但是最近遇到了一些与我拥有的一些功能有关的性能问题。我想知道我可以采取哪些明显的步骤来提高性能。

public int GetMilesTraveled(int userId)
{
    var bookings = new BookingRepository().GetList(p => p.OwnerId == userId &&
                                                   TimeProvider.Current.UtcNow > GetArrivalTimeInUTC(p),
                                                   p => p.Route);
    return bookings.Sum(p => p.Route.Distance);
}

public DateTime GetArrivalTimeInUTC(Booking booking)
{
    var airport = new AirportRepository().GetSingle(p => p.Id == booking.Route.ArrivalAirportId);

    return GetTimeZoneInUTC(airport.Timezone,
                            new DateTime(booking.FlightDate.Date.Ticks + booking.Route.ArrivalTime.Ticks));
}

public DateTime GetTimeZoneInUTC(string timeZoneId, DateTime dateTime)
{
    var timeZone = TimeZoneInfo.FindSystemTimeZoneById(
            TZConvert.IanaToWindows(timeZoneId));

    var UTC_Time = TimeZoneInfo.ConvertTimeToUtc(
            DateTime.SpecifyKind(dateTime,
                                 DateTimeKind.Unspecified),
                                 timeZone);

    return UTC_Time;
}

此刻,我的GetMilesTraveled(...)函数最多需要 2秒返回大约32行的结果。这对我来说是不可持续的,因为最坏的情况是大约 500 行的结果。

我相信GetArrivalTimeInUTC(...)功能是当前的瓶颈,但是我不确定如何加以改进,因为我想将时区信息存储为IANA时区,因此我不必处理与夏令时相关的实例(我相信这是如何工作的)。

至于在原始查询中不包括Airport作为导航属性,我的Repository接口没有一种简单的方法来执行.NetCore中引入的ThenInclude(...)类型的操作,而且我还没有想出了如何将其合并。

我可以进行任何明显的性能增强以轻松加快速度吗?

0 个答案:

没有答案
相关问题