I found an performance issue with SubselectFetch nhibernate class.
My mapping file looks like:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Business" namespace="Business.Entities">
<class name="Job" table="Job">
<id name="Id" column="JobId" type="Int32" unsaved-value="0">
<generator class="identity"/>
</id>
<property column="InternalId" type="string" name="Title" not-null="true" />
<property column="Description" type="String" name="Description" not-null="true" />
<property column="Url" type="String" name="Url" not-null="true" />
<bag name="CategoryIds" table="JobCategory" fetch="subselect" inverse="true" lazy="true" generic="true" >
<key column="JobId" />
<element column="CategoryId" type="Int32"/>
</bag>
<bag name="RegionIds" table="JobRegion" fetch="subselect" inverse="true" lazy="true" generic="true" >
<key column="JobId" />
<element column="RegionId" type="Int32"/>
</bag>
</class>
</hibernate-mapping>
I'm trying to load 1000 objects from table Job using a query like:
Code:
IQuery query = Session.CreateQuery("from Job j where j.Id in (:id_list)");
query.SetParameterList("id_list", jobIds);
IList<Job> result = query.List<Job>();
NHibernateUtil.Initialize(result[0].CategoryIds);
NHibernateUtil.Initialize(result[0].RegionIds);
Nhibernate generates three expected queries that take about only ~10% of all query execution time.
Code:
select job0_.JobId as JobId0_, job0_.Title as Title0_, from Job job0_ where (job0_.JobId in(@p0 , @p1 , ... , @p999))
SELECT categoryid0_.JobId as JobId__0_, categoryid0_.CategoryId as CategoryId0_ FROM JobCategory categoryid0_ WHERE categoryid0_.JobId in (select job0_.JobId from Job job0_ where (job0_.JobId in(@p0 , @p1 , ..., @p999)));
SELECT regionids0_.JobId as JobId__0_, regionids0_.RegionId as RegionId0_ FROM JobRegion regionids0_ WHERE regionids0_.JobId in (select job0_.JobId from Job job0_ where (job0_.JobId in(@p0 , @p1 , @p2 , ..., @p999)));
But almost all other time ~70% is taken by SubselectFetch constructor, which is executed in CreateSubselects method.
Is it correct behaviour?