﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Ayende Rahien commented on Reviewing NerdDinner: The Select N+1 pitfall</title><description>Rafal,
  
I wouldn't go quite that far.
  
The reason for this post is that I run into a bit of code that met the "we have select n+1 here" format quite well, and then tracked it down to end up in not having a select n+1.
  
There is still an issue of a superfluous select, and I think that showing suspicious code can be very helpful.
</description><link>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment17</link><guid>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment17</guid><pubDate>Fri, 31 Jul 2009 12:54:30 GMT</pubDate></item><item><title>Rafal commented on Reviewing NerdDinner: The Select N+1 pitfall</title><description>Yes, all apps have select n+1 problem, like all apps use too much memory and execute inefficient code. But is it a problem if the application does its job and gives acceptable results? Look, you can put any code in a loop so if I followed your logic I would have to declare all components as 'dormant' problems. 
</description><link>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment16</link><guid>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment16</guid><pubDate>Fri, 31 Jul 2009 12:49:32 GMT</pubDate></item><item><title>Ayende Rahien commented on Reviewing NerdDinner: The Select N+1 pitfall</title><description>Rafal,
  
_all_ apps have select n+1
  
You start from that assumption, and you can only prove it to be false under very limited circumstances
</description><link>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment15</link><guid>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment15</guid><pubDate>Fri, 31 Jul 2009 10:51:49 GMT</pubDate></item><item><title>Ayende Rahien commented on Reviewing NerdDinner: The Select N+1 pitfall</title><description>cowgaR,
  
Yes, you need to eager load
</description><link>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment14</link><guid>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment14</guid><pubDate>Fri, 31 Jul 2009 10:48:53 GMT</pubDate></item><item><title>Ayende Rahien commented on Reviewing NerdDinner: The Select N+1 pitfall</title><description>Look at the key map
  
ctrl+alt+f7, ctrl+shift+f7 on my machine
</description><link>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment13</link><guid>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment13</guid><pubDate>Fri, 31 Jul 2009 10:43:46 GMT</pubDate></item><item><title>Ayende Rahien commented on Reviewing NerdDinner: The Select N+1 pitfall</title><description>Hendry,
  
Yes, it does. But it does so in a way that make is explicit that something is going on there, in most cases.
</description><link>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment12</link><guid>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment12</guid><pubDate>Fri, 31 Jul 2009 10:43:01 GMT</pubDate></item><item><title>Rafal commented on Reviewing NerdDinner: The Select N+1 pitfall</title><description>Aren't you going too far in the hunt for Select N+1? I mean, you assume there's Select N+1 problem before even seeing the application at work or looking at database queries. This is not a very valuable review method. 
</description><link>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment11</link><guid>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment11</guid><pubDate>Fri, 31 Jul 2009 08:23:08 GMT</pubDate></item><item><title>cowgaR commented on Reviewing NerdDinner: The Select N+1 pitfall</title><description>excellent post and great point about spark's html data caching!
  
still, the question pointed by Hendry remains open, so the only remedy is to do an eagear load?
  
(not in L2S but in generall as L2S eager load is flawed anyway)
  
  
the nhProf note made a little grin on my face though :D
</description><link>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment10</link><guid>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment10</guid><pubDate>Fri, 31 Jul 2009 08:14:20 GMT</pubDate></item><item><title>Krzysztof Kozmic commented on Reviewing NerdDinner: The Select N+1 pitfall</title><description>@John
  
  
alt+shift+F12
</description><link>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment9</link><guid>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment9</guid><pubDate>Fri, 31 Jul 2009 07:00:03 GMT</pubDate></item><item><title>John Simons commented on Reviewing NerdDinner: The Select N+1 pitfall</title><description>"ReSharper will help us figure that one out"
  
What 's the shortcut to get that resharper menu open?
  
I only know of the right click Find Usages that opens in a Tool Window
</description><link>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment8</link><guid>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment8</guid><pubDate>Fri, 31 Jul 2009 02:37:52 GMT</pubDate></item><item><title>Hendry Luk commented on Reviewing NerdDinner: The Select N+1 pitfall</title><description>I agree and I'm a firm believer of VIewModel.
  
But how does it solve N+1 problem? Doesn't it merely shifts the N+1 problem from View to Controller?
</description><link>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment7</link><guid>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment7</guid><pubDate>Fri, 31 Jul 2009 02:32:12 GMT</pubDate></item><item><title>Ayende Rahien commented on Reviewing NerdDinner: The Select N+1 pitfall</title><description>Hendry,
  
View Models, mostly. Which force me to explicitly marshal the value.
  
More likely, I would simply watch stuff like in NH Prof
</description><link>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment6</link><guid>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment6</guid><pubDate>Fri, 31 Jul 2009 02:09:40 GMT</pubDate></item><item><title>Ayende Rahien commented on Reviewing NerdDinner: The Select N+1 pitfall</title><description>Haacked,
  
I follow the logic of doing that in the view, in fact, I can argue for it for delayed batch execution purposes (when the later you run it, the better you are).
  
I don't like it very much because it tend to create problems down the wrong, big ones.
  
Changing the view should NOT be a cause to change the database behavior, and that is very often the case in such things.
  
  
As for output caching, I don't like the way spark is doing that at all. HTML generation, for the most part, is the cheapest part in the whole deal, and not something that I particularily worry about.
  
Data is what I tend to cache, and for that, I _certainly_ don't want to do it in the view.
</description><link>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment5</link><guid>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment5</guid><pubDate>Fri, 31 Jul 2009 02:02:25 GMT</pubDate></item><item><title>Hendry Luk commented on Reviewing NerdDinner: The Select N+1 pitfall</title><description>More interestingly, I'm curious about better approach you would use to tackle that in this particular example. 
  
The concept of rich domain-model and lazy-load often colide each other, sometimes causing massive N+1 explossion.
  
As another example, let's say User has a list of RegisteredRsvps, defined as lazy-load association.
  
And the user has instance property (or method):
  
public bool IsRegistered
  
{
  
    get { return !this.RegisteredRsvps.IsEmpty(); }
  
}
  
  
This is a pretty simple and valid domain design (I believe). But it's a dormant N+1. (perhaps all attempts for rich domain abstractions are likely to end up with dormant N+1s). 
  
That code works fine for current purposes, until we have a page that displays all users in a grid with a 'Status' column (Registered or Unregistered). It will fire an N+1 call.
  
How would you tackle this? Do all controllers (who're about to loop over User.IsRegistered property) need to know they got to tell UserRepository to force join-load ReservedRsvps property? Why does UI controller even need to know about this knowledge anyway?
</description><link>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment4</link><guid>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment4</guid><pubDate>Fri, 31 Jul 2009 01:45:27 GMT</pubDate></item><item><title>Sebastian Markb&amp;#229;ge commented on Reviewing NerdDinner: The Select N+1 pitfall</title><description>I love this post.
  
  
It clearly illustrates the issues that can show up if you do something "immoral". Even if it can seem trivial on each level alone, somewhere down the pipeline, it'll get ya.
</description><link>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment3</link><guid>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment3</guid><pubDate>Fri, 31 Jul 2009 00:27:02 GMT</pubDate></item><item><title>Haacked commented on Reviewing NerdDinner: The Select N+1 pitfall</title><description>Interesting point about your moral objection to the view kicking off a database call. 
  
  
In this case, the view doesn't know it's causing a database call. It's merely a consequence of returning an IQueryable which defers the call till it's being accessed.
  
  
I don't think this is necessarily bad in this case because the view is in process with the controller and very unlikely to be disconnected from the controller. Why do I say that? Well the responsibility for setting up the database call still lies in the controller and model. So I would say that separation of concerns is maintained. After all, if you called ToList() in the controller, the view code wouldn't have to change.
  
  
However, from another angle, I understand the moral objection from the perspective of it's kind of hidden. What if I dispose the data context in the controller action method. That seems like a reasonable thing to do, but would cause a problem.
  
  
In any case, Spark View Engine's new output caching feature relies on delayed execution of the model: 
[whereslou.com/2009/07/28/spark-output-caching](http://whereslou.com/2009/07/28/spark-output-caching)  
  
What do you think of that? It solves a particular problem in a very neat manner, but it faces your moral objection.
</description><link>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment2</link><guid>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment2</guid><pubDate>Thu, 30 Jul 2009 22:39:12 GMT</pubDate></item><item><title>Dmitry commented on Reviewing NerdDinner: The Select N+1 pitfall</title><description>It looks like the function was designed to see if the currently logged-in user is registered; and it is not like you can have several logged-in users per context at the same time.
  
  
But I can see where a scenario like this can cause a potential SELECT N+1 problem. Traditional lazy-loading is not the only way to create this problem.
  
  
</description><link>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment1</link><guid>http://ayende.com/4093/reviewing-nerddinner-the-select-n-1-pitfall#comment1</guid><pubDate>Thu, 30 Jul 2009 21:13:06 GMT</pubDate></item></channel></rss>