title: Simple get using NHibernate (using mapping-by-code) is extremely slow
body: <p>So i've run out of ideas as to why this is so slow. Maybe you can help.
  So I am trying to do a simple get on record from an oracle db using nHibernate mapping by code. I'm using nHibernate verison 3.3.1.4 from nuget.</p>
  
  <p>Here is the mapping code:</p>
  
  <pre><code>public class Person
  {
      public virtual PersonKey Key { get; set; }
      public virtual string FirstName { get; set; }
      public virtual string LastName { get; set; }
  }
  
  public class PersonKey
  {
      public virtual string PersonId { get; set; }
  
      public override bool Equals(object obj)
      {
          if (obj == null)
              return false;
          var t = obj as PersonKey;
          if (t == null)
              return false;
          if (PersonId == t.PersonId)
              return true;
          return false;
      }
      public override int GetHashCode()
      {
          return (PersonId).GetHashCode();
      }
  }
  
  public class PersonMap : ClassMapping&lt;Person&gt;
  {
      public PersonMap()
      {
          Schema("MyDB");
          Table("Person");
          ComponentAsId(id =&gt; id.Key, idMapper =&gt; idMapper.Property(p =&gt; p.PersonId));
          Property(i =&gt; i.FirstName);
          Property(i =&gt; i.LastName);
      }
  }
  </code></pre>
  
  <p>Here is the code to create the sessionfactory and retrieve the data.</p>
  
  <pre><code>var mapper = new ModelMapper();
          var cfg = new Configuration();
  
          mapper.AddMappings(Assembly.GetExecutingAssembly().GetExportedTypes());
  
          cfg.DataBaseIntegration(c =&gt;
          {
              c.ConnectionString = @"User Id=user;Password=password;Data Source=MyDB;";
              c.Driver&lt;OracleClientDriver&gt;();
              c.Dialect&lt;Oracle10gDialect&gt;();
  
              c.LogSqlInConsole = true;
              c.LogFormattedSql = true;
              c.AutoCommentSql = true;
          });
  
          cfg.AddMapping(mapper.CompileMappingForAllExplicitlyAddedEntities());
          var sessionFactory = cfg.BuildSessionFactory();
  
          stopwatch.Stop();
          Console.WriteLine("Building session factory: {0}", stopwatch.ElapsedMilliseconds);
          stopwatch.Restart();
  
          Person entity = null;
  
          using (var session = sessionFactory.OpenSession())
          using(var tx = session.BeginTransaction())
          {
              entity = (Person) session.Get("Person", new PersonKey(){PersonId = "1"});
              tx.Commit();
          }
  </code></pre>
  
  <p>The query generated is as follows:</p>
  
  <pre><code>SELECT person0_.PersonId as PersonId0_0_, 
  person0_.FirstName as FirstName0_0_, person0_.LastName as LastName0_0_,  
  FROM MyDB.Person person0_
  WHERE person0_.PersonId=:p0;
  :p0 = '1' 
  </code></pre>
  
  <p>It takes roughly 80-100 seconds on average to get access to the retrieved object. This includes creating the session as well.</p>
  
  <p>So some obvious things I looked for:</p>
  
  <ul>
  <li>The personId coloumn is indexed (it is the primary key of the table).</li>
  <li>The database is on a server, so to check it is not the network eating the time, I ran the above generated query using AD0.Net (Oracleconnection + Oraclecommand). This roughly takes 180ms this includes creating the connection and mapping the record to entity class.</li>
  <li>Ran the query via PL/SQL developer (took about 32ms).</li>
  <li>Check the query plan on the generated query (confirmed the use of the index unique scan and not a full table scan).</li>
  <li>I've run the above code against a similar sized sql 2012 db on my local and, it's crazy fast incomparison, around 180ms via the NHibernate code.</li>
  <li>Ran nhprof (evaluation) and got the following results:</li>
  </ul>
  
  <p><img src="https://i.stack.imgur.com/N2fpe.png" alt="Results from NHibernate profiler"></p>
  
  <p>It seems like the query is run and the results are returned very fast from the database (according to nhprof results), but maybe its the hydration of the query's values into the entity that is taking up the time. I am at a loss at what could be chewing up the time!</p>
  
  <p>My next move is to attach the nHibernate source to the solution and step through it, but download access at work is limited (even to github!). Till then any ideas?</p>
  
  <p>Cheers.</p>
  
  <p><strong>Update:</strong> So I've got the nHibernate source in my project and stepped through it. Everything is moving along nicely until the program gets to this piece of code:</p>
  
  <pre><code>for (count = 0; count &lt; maxRows &amp;&amp; rs.Read(); count++)              
  </code></pre>
  
  <p>It is on <code>rs.Read()</code> that the execution eats up the time, where rs is the oracle datareader. This piece of code is in the <code>DoQuery</code> function in the Loader.cs file. </p>
  
  <p>The odd thing is if the query passed in is a non-parameterised dynamic query (e.g. <code>select ... from person where personid = '1'</code>), the execution is lighting fast (~ 1ms), but if its parameterised (e.g. <code>select ... from person where personid = :p1</code>), then it's ridiculously slow. Since I want to harness the power of nHibernate, I need to use the generated parameterised queries. I am still trying to figure out why the oracle reader</p>