I have been playing around with a few ways to batch insert a list of 10,000 objects into my database.
These objects don’t have any relationships, and only store POCO data.
I have started using a StatelessSession as many articles have suggested and I have noticed a marked improvement.
One thing remains though – after each insert NHibernate does a SELECT statement to get the ID of the object it just inserted. Is there a way to defer this until when I am going to actually Query for the objects? It seems it would be a huge performance benefit if NHibernate could just insert the rows and not worry about the IDs.
Is this possible?
No its not possible, sometimes it is worth considering the right tool for the job. If you have an identity column on your table then NHibernate
ALWAYSfollows a select with an insert even forBULKinserts. Another thing to consider is that your level one cache is going to grow quite large (unless you use stateless sessions) and this could lead to other complications.I understand your point of view wanted to use the goodness of NHibernate but sometimes we just can’t avoid using other tools…
If I need to send 10K inserts into my database I for one would use the
SQlBulkCopyclass and bypass NHibernate altogether.See here for more info