I’m making frequent inserts and updates in large batches from c# code and I need to do it as fast as possible, please help me find all ways to speed up this process.
- Build command text using
StringBuilder, separate statements with; - Don’t use
String.FormatorStringBuilder.AppendFormat, it’s slower then multipleStringBuilder.Appendcalls - Reuse
SqlCommandandSqlConnection - Don’t use
SqlParameters (limits batch size) - Use
insert into table values(..),values(..),values(..)syntax (1000 rows per statement) - Use as few indexes and constraints as possible
- Use simple recovery model if possible
- ?
Here are questions to help update the list above
- What is optimal number of statements per command (per one
ExecuteNonQuery()call)? - Is it good to have inserts and updates in the same batch, or it is better to execute them separately?
My data is being received by tcp, so please don’t suggest any Bulk Insert commands that involve reading data from file or external table.
Insert/Update statements rate is about 10/3.
Use table-valued parameters. They can scale really well when using large numbers of rows, and you can get performance that approaches BCP level. I blogged about a way of making that process pretty simple from the C# side here. Everything else you need to know is on the MSDN site here. You will get far better performance doing things this way rather than making little tweaks around normal SQL batches.