Loading...

nhusers@googlegroups.com

[Prev] Thread [Next]  |  [Prev] Date [Next]

[nhusers] Deep Eager load avoiding N+1 and cartesian Brad Laney Fri Feb 03 17:04:55 2012

Hello.

I am trying to avoid N+1 and cartesian product when I need to do deep
eager loads. I want to do this for performance. It is an EAV type of a
data structure.

First situation:
A -> B -> C. Three tables. Regular primary keys off ints and a list in
the parent that goes to the child.

I have an inherited Id column, of int, on each type.
My mappings are fine, everything can be selected using a
QueryOver.Fetch for eager loading, but this causes a cartesian
product.

I tried using futures. This apparently does nothing like it's
advertised on peoples blogs, etc. It's supposed to allow you to
preload records to avoid N+1 and cartesian, but NH apparently just
ignores it completely.

                var r = s.QueryOver<NHMapTest1>()
                    .Where(x => x.Id == id)
                    .Future();

                s.QueryOver<NHMapTest2>()
                    .Where(x => x.Parent.Id == id)
                    .Future();
Running r.Single(); returns the entity, but if you do .Items it'll
query the DB, even tho they were selected in the 2nd future. Both
queries run correctly and return valid results.


    public class NHMapTest1 : Entity
    {
        public virtual string Title { get; set; }
        private IList<NHMapTest2> _items;
        public virtual IList<NHMapTest2> Items { get { return
_items; } }
    }

    public class NHMapTest2 : Entity
    {
        public virtual string Title { get; set; }
        private IList<NHMapTest3> _items;
        public virtual IList<NHMapTest3> Items { get { return
_items; } }
        public virtual NHMapTest1 Parent { get; set; }
    }

    public class NHMapTest3 : Entity
    {
        public virtual string Title { get; set; }
        public virtual NHMapTest2 Parent { get; set; }
    }


2nd Situation:
A -> B -> C -> D when C is a composite key reference to B and D.


    public class NHMapTest1 : Entity
    {
        public virtual string Title { get; set; }
        private IList<NHMapTest2> _items;
        public virtual IList<NHMapTest2> Items { get { return
_items; } }
    }

    public class NHMapTest2 : Entity
    {
        public virtual string Title { get; set; }
        private IList<NHMapTest3> _items;
        public virtual IList<NHMapTest3> Items { get { return
_items; } }
        public virtual NHMapTest1 Parent { get; set; }
    }

    public class NHMapTestComposite2 : Entity
    {
        public virtual NHMapTest2 NHMapTest2 { get; set; }
        public virtual NHMapTest3 NHMapTest3 { get; set; }
        public virtual string Title { get; set; }
    }

    public class NHMapTest3 : Entity
    {
        public virtual string Title { get; set; }
        private IList<NHMapTest4> _items;
        public virtual IList<NHMapTest4> Items { get { return
_items; } }
        public virtual NHMapTest2 Parent { get; set; }
    }

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [EMAIL PROTECTED]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.