icematrix8
New Member
I'm building an ASP.Net application with Entity Framework (Code First) and I've implemented a Repository pattern like the one in this example.I only have two tables in my database. One called \[code\]Sensor\[/code\] and one called \[code\]MeasurePoint\[/code\] (containing only \[code\]TimeStamp\[/code\] and \[code\]Value\[/code\]). A sensor can have multiple measure points. At the moment I have 5 sensors and around 15000 measure points (approximately 3000 points for each sensor).In one of my MVC controllers I execute the following line (to get the most recent MeasurePoint for a Sensor)\[code\]DbSet<Sensor> dbSet = context.Set<Sensor>();var sensor = dbSet.Find(sensorId);var point = sensor.MeasurePoints.OrderByDescending(measurePoint => measurePoint.TimeStamp).First();\[/code\]This call takes ~1s to execute which feels like a lot to me. The call results in the following SQL query\[code\]SELECT [Extent1].[MeasurePointId] AS [MeasurePointId], [Extent1].[Value] AS [Value], [Extent1].[TimeStamp] AS [TimeStamp], [Extent1].[Sensor_SensorId] AS [Sensor_SensorId]FROM [dbo].[MeasurePoint] AS [Extent1]WHERE ([Extent1].[Sensor_SensorId] IS NOT NULL) AND ([Extent1].[Sensor_SensorId] = @EntityKeyValue1)\[/code\]Which only takes ~200ms to execute, so the time is spent somewhere else.I've profiled the code with the help of Visual Studio Profiler and found that the call that causes the delay is \[code\]System.Data.Objects.Internal.LazyLoadBehavior.<>c_DisplayClass7`2.<GetInterceptorDelegate>b_1(!0,!1)\[/code\]So I guess it has something to do with lazy loading. Do I have to live with performance like this or are there improvements I can make? Is it the ordering by time that causes the performance drop, if so, what options do I have?Update:I've updated the code to show where \[code\]sensor\[/code\] comes from.