The title may sound a bit hyperbolic, but I promise that 18,000% was the rough page load speed gain realized on a recent project that I was working. Before we dive into the meat of the problem and its resolution, let’s set the stage. I was given a new project to pull down and become familiar with. After getting over the initial hiccups of loading a new project, I clicked around the application to get a feel for how it functions from a top-level view. In the process of clicking, I landed on one page where there was a progress bar indicator that was running on load and a search form waiting to be filled. After waiting a couple of seconds with no data showing, I figured that it just showed the animation until a search was performed. With that thought in mind, I went to perform a search, but before I could start, the data populated. It had taken what felt like an eternity to load that information, but as anyone knows when investigating a performance problem feelings don’t matter; numbers do.
Figure 2: Materializing a list of Cares from the database
Figure 3: Care class has a number of Virtual properties which have NOT been materialized in Figure 2.
Figure 4: Glimpse output showing the database connections
Figure 5: Telling EF to eagerly load the tables I need.
Figure 6: Much improved load time.
Figure 8: ViewModel to simplify EF data returned.
Figure 9: Simplified query
Figure 10: Final improved speed
So what can we learn from this? First, if you’re using EF, you absolutely must understand that it lazy loads virtual properties by default and this will come back to haunt you as soon as you iterate a collection of Entities that have them. Second, only return the data that you absolutely need for the view to trim-down load time. Obviously, there’s no need to transfer a bunch of data from the DB if we’re never even going to use it. Finally, it’s important to remember that when doing any performance tuning, you must measure before and after the “improvements” are made. While this was an easy example of low-hanging fruit, it’s not always obvious that the “enhancements” made will improve performance.
Director of Magic
We had a super magical year in 2018. Bit-Wizards is getting motivated for 2019 by reflecting on what an incredible… https://t.co/UDXZsV09PS
Happy birthday, Patrick! Hope your birthday is magical! Learn more about Patrick here! ---> https://t.co/N5gAUogw0r… https://t.co/et222GZvjM