I’m writing a command-line tool (to run on Windows 7 boxes) that inserts a single record into a table and then exits. I’m using C# (.NET 4.5) and SQL Server 2008.
The record of data contains a large (up to 6000 bytes) string, a time-stamp and an auto-incrementing integer ID primary key. There are no foreign keys attached to the table.
Also, since this is the most common usage scenario for this database I’d like to optimise for it. Are there any tricks with how I set up the database that will help keep individual insert times low?
[EDIT]
The code I’m currently using…
SqlConnection con = new SqlConnection(ConnectionString);
con.Open();
SqlTransaction trans = con.BeginTransaction();
SqlCommand cmd = new SqlCommand("INSERT INTO [msglog](message,project,username,computername,updated_at) VALUES (@m,@p,@u,@c,@i,@t)", con, trans);
cmd.Parameters.AddWithValue("@u", Username);
cmd.Parameters.AddWithValue("@c", Computername);
cmd.Parameters.AddWithValue("@m", msg);
cmd.Parameters.AddWithValue("@p", ProjectName);
cmd.Parameters.AddWithValue("@t", DateTime.Now);
cmd.ExecuteNonQuery();
trans.Commit();
In your code example, I suspect that most of the time is spent establishing a connection to SQL Server. I would recommend:
There may be other micro-optimizations, but really addressing my first concern above would give you the most significant speedup.