2014/1/17 TheSlowLoris <[email protected]>

> I'm executing a query using a subquery like so:
>
>                 Parent aliasParent = null;
>                 var queryOver = unitOfWork.Session.QueryOver<Parent>(() =>
> aliasParent).
>                     WithSubquery.WhereProperty(parent =>
> parent.ID).In(QueryOver.Of<Child>().
>                         Where(child => ...).
>                         And(child => child.Parent.ID == aliasParent.ID).
>                         Select(child => child.Parent.ID));
>
> The purpose is to only load Parent entities whose Child entities have
> certain criteria.
> The query executes correctly, and the resulting SQL is generated correctly.
> However, the following error appears in my debugger's output window:
>
> A first chance exception of type 'NHibernate.QueryException' occurred in
> NHibernate.dll
>
> This is a trapped exception within NHibernate.  I believe NHibernate
> throws the exception at one point in the query translation code, when it
> sees the alias and doesn't know what to do.  Only later does it then trap
> and respond to the exception in another part of the translation.  This is
> all fine, except that by using exception handling in this way results in
> poorer performance.  The performance impact is not significant unless
> someone is executing a lot of these kinds of queries in succession.  I
> estimate that the query should only take 10 ms to execute, but because of
> the exception handling, the query is taking almost 70 ms.
>


Without having looked at the code, I agree that exceptions shouldn't be
used in that way. I'm sceptical to your timings - is 10ms the pure
ADO.NETtime? Because NHibernate does a lot of things that could
account for the
70ms. I suspect it's highly unlikely that the exception alone adds 60ms
with no debugged attached (or even with the debugger).



>
> Is it worth reporting an issue over this?  It seems to me that using the
> exception handling is "by design", and that the performance impact may
> already be known and understood.
> I've experimented with modifying the NHibernate library, so it would be
> possible to submit a patch with the issue, however,
>

If you have a good quality fix for the issue, I don't see why it shouldn't
be fixed.



> Is there a workaround to the issue other than query the Child entities
> directly, fetching the Parent association, and then manually loading a list
> like so:
>
> List<Child> children = session.Query<Child>().Where(child =>
> ...).Fetch(child => child.Parent).ToList();
> foreach (Child child in children)
> {
>     if (parents.IndexOf(child.Parent) == -1)
>         parents.Add(child.Parent);
> }
> parents.Sort(...);
>
>
With LINQ subqueries:
var childQueryable = session.Query<Child>().Where(child => ...).Select(
child.Parent.ID);  // No ToList!
var parents = session.Query<Parent>().Where(parent =>
childQueryable.Contains(parent.ID);

/Oskar

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to