Select N+1 Situation

Posts   
 
    
Rishi
User
Posts: 69
Joined: 31-Oct-2011
# Posted on: 02-Mar-2012 22:01:38   

Hi,

I am using LLBLGen 3.1. I am fetching all the entities using pre-fetch path and this is in loop using adapter. ORM Profiler is showing Select N+1 warning.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39909
Joined: 17-Aug-2003
# Posted on: 03-Mar-2012 12:17:08   

You don't post any detail about your query so we can't help you further. Please post more details so we know what kind of queries you're executing.

Frans Bouma | Lead developer LLBLGen Pro
Rishi
User
Posts: 69
Joined: 31-Oct-2011
# Posted on: 05-Mar-2012 17:04:55   

Please download ORM Profiler snapshot file from below link for your reference.

http://dl.dropbox.com/u/7290619/Snapshot_20120302155446.zip

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 06-Mar-2012 05:47:40   

Hi Rishi,

If you open the snapshot in the ORMProfiler and see the 'Queries, grouped' tab you will see that some queries are repeated with the same parameters over and over again, that's why the warnings. This occurs if, for example you are fetching the same entity in a loop.

For the methods registered in the profiler I assume you are using Adapter, so there is no lazy loading happening.

For instance, you are fetching the Login entity a lot of times with the same parameter (an email address). This could be normal if the calls are in different connections but they all are in the same connection so it seems like you are doing something like:

var adapter = new DataAccessAdapter();
while(...)
{
     var someLogin = new LoginEntity();
     adapter.FethEntityUsingUniqueConstraint(someLogin, theSameUCFilter);
     ...
}

In that case, you could fetch the login outside the loop:

var adapter = new DataAccessAdapter();
var someLogin = new LoginEntity();
adapter.FethEntityUsingUniqueConstraint(someLogin, theSameUCFilter);
while(...)
{
     ...
}

As you are doing some import operations it could be that some repeateable queries are unavoidable. ORMProfiler is saying: "Warning, it looks like you can improve something there." So you should look into your code to see if you can put some repeatable fetches outside the loop.

David Elizondo | LLBLGen Support Team