title: NHibernate IsUpdateNecessary takes enormous amount of time
body: <p>My C# 3.5 application uses SQL Server 2008 R2, NHibernate and CastleProject ActiveRecord. The application imports emails to database along with their attachments. Saving of emails and attachments is performed by 50 emails in new session and transaction scope to make sure they are not stored in memory (there can be 100K of emails in some mailbox).</p>
  
  <p>Initially emails are saved very quickly. However, closer to 20K emails performance degrades dramatically. Using dotTrace I got the following picture: </p>
  
  <p><img src="https://i.stack.imgur.com/DBcgo.jpg" alt="enter image description here"></p>
  
  <p>Obviously, when I save an attachment, NHibernate tries to see if it really should save it and probably compares with another attachments in the session. To do so, it compares them byte by byte what takes almost 500 seconds (for the snapshot on the picture) and 600M enumerator operations.</p>
  
  <p>All this looks crazy, especially when I know for sure that SaveAndFlush indeed should save the attachment without any checks: I know for sure that it is new and should be saved.</p>
  
  <p>However, I cannot figure out, how to instruct NHibernate to avoid this check (IsUpdateNecessary). Please advise.</p>
  
  <p>P.S. I am not sure but it might appear that degradation of performance closer to 20K is not connected with having some older mails in memory: I noticed that in mailbox I am working with, larger emails are stored later than smaller so the problem may be only in attachments comparison.</p>
  
  <p><strong>Update:</strong>
  Looks like I need something like StatelessSessionScope, but there is no documentation on it even at CastleProject site! If I do something like</p>
  
  <pre><code>using (TransactionScope txScope = new TransactionScope())
  using (StatelessSessionScope scope = new StatelessSessionScope())
  {
    mail.Save();
  }
  </code></pre>
  
  <p>it fails with exception that Save is not supported by stateless session. I am supposed to insert objects into session, but I do not have any Session (only SessionScope, which adds up to SessionScope only single OpenSession method which accepts strange paramenters).</p>