Reviewing NerdDinner: The Select N+1 pitfall
During my review of NerdDinner, I came across the following piece of code:
And I knew that there is a Select N+1 problem here. This is quite similar to the problem that I described here. RSVPs is a lazily loaded collection. As such, calling Any() on it will result in a database query.
Now the question was whatever or not it is used somewhere in a loop. ReSharper will help us figure that one out:
The first usage location is here:
And this certainly doesn’t look like it can cause a Select N+1, right. Just to be sure, I checked where it is called from the client side as well:
This looks all right, we will have a query, but it is only one per request.
Great, now let us take a look at the second usage. You can tell that this is going to be a problem just by the ascx suffix. The code is pretty simple:
I have a moral issue with the view generating a call to the database, but at least it isn’t being done in a loop.
I am certain, however, that we are going to find this .ascx file in a grid, which will cause a loop. This .ASCX file is being used in Details.aspx and is used like this:
However, it is not actually being used in a loop.
There is not a Select N+1 issue, which renders this entire post moot.
However, while it is not an active Select N+1, it is a dormant one. It would be very easy to have a new requirements that would make use of the RSVPStatus.ascx file in a loop and cause the issue.