Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning NHibernate 4

You're reading from   Learning NHibernate 4 Explore the full potential of NHibernate to build robust data access code

Arrow left icon
Product type Paperback
Published in Jul 2015
Publisher Packt
ISBN-13 9781784393564
Length 402 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Suhas H Chatekar Suhas H Chatekar
Author Profile Icon Suhas H Chatekar
Suhas H Chatekar
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Introduction to NHibernate FREE CHAPTER 2. Let's Build a Simple Application 3. Let's Tell NHibernate About Our Database 4. NHibernate Warm-up 5. Let's Store Some Data into the Database 6. Let's Retrieve Some Data from the Database 7. Optimizing the Data Access Layer 8. Using NHibernate in a Real-world Application 9. Advanced Data Access Patterns 10. Working with Legacy Database 11. A Whirlwind Tour of Other NHibernate Features Index

Avoiding the select N+1 problem


We had briefly mentioned select N+1 problem in the previous chapter. Select N+1 is bad from both performance and memory consumption point of view. We are going to discuss how we can avoid this problem. Before we get our hands dirty, let's spend some time trying to understand what is select N+1 problem.

What is the select N+1 problem?

It is easier to understand select N+1 problem if we read it in reverse – 1+N select. That is right. Let's see how.

Suppose you want to load all employees living in London and then for every employee iterate through the benefits that they are getting. Following is one way of doing that using a LINQ query:

[Test]
public void WithSelectNPlusOneIssue()
{
  using (var transaction = Database.Session.BeginTransaction())
  {
    var employees = Database.Session.Query<Employee>()
    .Where(e => e.ResidentialAddress.City == "London");

    foreach (var employee in employees)
    {
      foreach (var benefit in employee.Benefits)
...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image