Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy are my NHibernate queries so slow?
    primarykey
    data
    text
    <p>I'm building an ASP web forms app with Fluent NHibernate v2.0.50727 to SQL server backend.</p> <p>For some reason querying tables with small data (~14 rows) takes seconds.</p> <p>The maps are all simple: <code>Id(x =&gt; x.Id)</code> and <code>Map(x =&gt; x.Name)</code>. CertificateGroup also Map()'s <code>Color</code> and <code>Rank</code>.</p> <p>I've isolated the nhibernate code for testing:(this is not representative of my application but is simplified and isolated for SO)</p> <pre><code>protected void Page_Load(object sender, EventArgs e) { var config = Fluently.Configure() .Database(MsSqlConfiguration.MsSql2008.ConnectionString("...")) .Mappings(m =&gt; m.FluentMappings.AddFromAssemblyOf&lt;PersonMap&gt;()) .BuildConfiguration(); var factory = config.BuildSessionFactory(); using (var session = factory.OpenSession()) { using (var transaction = session.BeginTransaction()) { var departments = session.QueryOver&lt;DepartmentModel&gt;().List(); // these all take seconds to execute - this has 14 results var jobs = session.QueryOver&lt;JobModel&gt;().List(); // 113 results var certificates = session.QueryOver&lt;CertificateModel&gt;().List(); //this one about 4 seconds for 210 results var groups = session.QueryOver&lt;CertificateGroupModel&gt;().List(); var association = new CertificateAssociationModel { Department = departments.First(), Job = jobs.First(), Certificate = certificates.First(), Group = groups.First() }; session.SaveOrUpdate(association); transaction.Commit(); } } } </code></pre> <p>I swapped nhibernate for entity framework and ran the above code and the fetches were instant.</p> <p>Any help appreciated.</p> <p>EDIT:</p> <p>Here are the mappings (as I explained above): For Department, Job and Certificate:</p> <pre><code>public class DepartmentMap : ClassMap&lt;DepartmentModel&gt; { public DepartmentMap() { Table("tblDepartment"); Id(x =&gt; x.Id); Map(x =&gt; x.Name); } } </code></pre> <p><code>CertificateGroupModel</code> also has <code>Map(x =&gt; x.Rank);</code> <code>Map(x =&gt; x.Color);</code></p> <p>Entity classes are all the same:</p> <pre><code>public class CertificateModel { public virtual Int32 Id { get; protected set; } public virtual String Name { get; set; } } </code></pre> <p>Except CertificateGroupModel which also has:</p> <pre><code>public virtual String Color { get; set; } public virtual Int32 Rank { get; set; } </code></pre> <p>Here are my NHiberate profiler results:</p> <pre><code> -- statement #1 begin transaction with isolation level: Unspecified -- statement #2 SELECT this_.Id as Id4_0_, this_.Name as Name4_0_ FROM tblDepartment this_ -- statement #3 SELECT this_.Id as Id5_0_, this_.Type as Type5_0_ FROM tblJobTitles this_ -- statement #4 SELECT this_.Id as Id2_0_, this_.Name as Name2_0_ FROM tblCertificate this_ -- statement #5 SELECT this_.Id as Id1_0_, this_.Name as Name1_0_, this_.Rank as Rank1_0_, this_.Color as Color1_0_ FROM tbl_certificate_groups this_ -- statement #6 INSERT INTO lnk_certificate_associations (Certificate_id, Department_id, Job_id, Group_id) VALUES (1 /* @p0 */, 1 /* @p1 */, 1 /* @p2 */, 1 /* @p3 */); select SCOPE_IDENTITY() -- statement #7 commit transaction </code></pre> <p><a href="http://i.snag.gy/bTKHm.jpg" rel="nofollow noreferrer">nhibprofile http://i.snag.gy/bTKHm.jpg</a></p> <p>IL code generated:</p> <pre><code> var departments = session.QueryOver&lt;DepartmentModel&gt;().List(); IL_008F: ldloc.2 /* session */ IL_0090: callvirt instance NHibernate.IQueryOver`2&lt;!!0, !!0&gt; NHibernate.ISession::QueryOver&lt;Core.Domain.Model.DepartmentModel&gt;() IL_0095: callvirt instance [mscorlib]System.Collections.Generic.IList`1&lt;!0&gt; NHibernate.IQueryOver`1&lt;Core.Domain.Model.DepartmentModel&gt;::List() IL_009A: stloc.s departments </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload