Darren is absolutely correct, my first idea was to create a Stored Procedure and pass all the search parameters. And thus use the power of the database instead of the ORM.
But one of these parameters is an array and to pass that to a sproc I would need to use the Table Value Parameters of SQL Server 2008.
Two problems with that, first I need to support SQL Server 2005 also and an even bigger problem I needed to support Oracle as well.
In the end, here's the solution I came up with:
- Use the ORM to select only the Id's of the entities
- Do a foreach on all those Guids and insert each one in a temporary table
- Then call a Stored Procedure that will use the temp table to do all the full inserts
Step 2 has been optimized to use a for loop with a step of 1000 in combination with .SetParameterList(). This will generate one insert query every 1000 ids instead of 1000 inserts.
It's a compromise, but one I can live with for the moment.