// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Data; using System.Data.SqlClient; using System.Text; namespace E { public class Program { public const int N = 100000; public static void TestNormal() { using(SqlConnection con = new SqlConnection(@"Server=ARNEPC3\SQLEXPRESS;Integrated Security=SSPI;Database=Test")) { con.Open(); SqlCommand cre = new SqlCommand("CREATE TABLE utest (id INTEGER NOT NULL, x DECIMAL(18,2), s1 VARCHAR(50), s2 VARCHAR(50), PRIMARY KEY(id))", con); cre.ExecuteNonQuery(); DateTime dt1 = DateTime.Now; for(int i = 0; i < N/250; i++) { for(int j = 0; j < 250; j++) { int v = 250 * i + j; SqlCommand ins = new SqlCommand("INSERT INTO utest VALUES(" + v + "," + v + ",'Dette er test nummer " + v + "','Noget mere tekst')", con); ins.ExecuteNonQuery(); } } DateTime dt2 = DateTime.Now; SqlCommand sel = new SqlCommand("SELECT COUNT(*) AS n FROM utest", con); Console.WriteLine(N + "=" + (int)sel.ExecuteScalar()); SqlCommand drp = new SqlCommand("DROP TABLE utest", con); drp.ExecuteNonQuery(); Console.WriteLine("n INSERT n TX : " + (dt2 - dt1).TotalSeconds); } } public static void TestBundle() { using(SqlConnection con = new SqlConnection(@"Server=ARNEPC3\SQLEXPRESS;Integrated Security=SSPI;Database=Test")) { con.Open(); SqlCommand cre = new SqlCommand("CREATE TABLE utest (id INTEGER NOT NULL, x DECIMAL(18,2), s1 VARCHAR(50), s2 VARCHAR(50), PRIMARY KEY(id))", con); cre.ExecuteNonQuery(); DateTime dt1 = DateTime.Now; for(int i = 0; i < N/250; i++) { SqlTransaction tx = con.BeginTransaction(); for(int j = 0; j < 250; j++) { int v = 250 * i + j; SqlCommand ins = new SqlCommand("INSERT INTO utest VALUES(" + v + "," + v + ",'Dette er test nummer " + v + "','Noget mere tekst')", con); ins.Transaction = tx; ins.ExecuteNonQuery(); } tx.Commit(); } DateTime dt2 = DateTime.Now; SqlCommand sel = new SqlCommand("SELECT COUNT(*) AS n FROM utest", con); Console.WriteLine(N + "=" + (int)sel.ExecuteScalar()); SqlCommand drp = new SqlCommand("DROP TABLE utest", con); drp.ExecuteNonQuery(); Console.WriteLine("n INSERT n/250 TX : " + (dt2 - dt1).TotalSeconds); } } public static void TestUnion() { using(SqlConnection con = new SqlConnection(@"Server=ARNEPC3\SQLEXPRESS;Integrated Security=SSPI;Database=Test")) { con.Open(); SqlCommand cre = new SqlCommand("CREATE TABLE utest (id INTEGER NOT NULL, x DECIMAL(18,2), s1 VARCHAR(50), s2 VARCHAR(50), PRIMARY KEY(id))", con); cre.ExecuteNonQuery(); DateTime dt1 = DateTime.Now; for(int i = 0; i < N/250; i++) { string sqlstr = "INSERT INTO utest"; for(int j = 0; j < 250; j++) { int v = 250 * i + j; if(j > 0) { sqlstr += " UNION ALL"; } sqlstr += " SELECT " + v + "," + v + ",'Dette er test nummer " + v + "','Noget mere tekst'"; } SqlCommand ins = new SqlCommand(sqlstr, con); ins.ExecuteNonQuery(); } DateTime dt2 = DateTime.Now; SqlCommand sel = new SqlCommand("SELECT COUNT(*) AS n FROM utest", con); Console.WriteLine(N + "=" + (int)sel.ExecuteScalar()); SqlCommand drp = new SqlCommand("DROP TABLE utest", con); drp.ExecuteNonQuery(); Console.WriteLine("n/250 INSERT n/250 TX : " + (dt2 - dt1).TotalSeconds); } } public static void TestUnionSmart() { using(SqlConnection con = new SqlConnection(@"Server=ARNEPC3\SQLEXPRESS;Integrated Security=SSPI;Database=Test")) { con.Open(); SqlCommand cre = new SqlCommand("CREATE TABLE utest (id INTEGER NOT NULL, x DECIMAL(18,2), s1 VARCHAR(50), s2 VARCHAR(50), PRIMARY KEY(id))", con); cre.ExecuteNonQuery(); DateTime dt1 = DateTime.Now; for(int i = 0; i < N/250; i++) { StringBuilder sqlstr = new StringBuilder(); sqlstr.Append("INSERT INTO utest"); for(int j = 0; j < 250; j++) { int v = 250 * i + j; if(j > 0) { sqlstr.Append(" UNION ALL"); } sqlstr.Append(" SELECT " + v + "," + v + ",'Dette er test nummer " + v + "','Noget mere tekst'"); } SqlCommand ins = new SqlCommand(sqlstr.ToString(), con); ins.ExecuteNonQuery(); } DateTime dt2 = DateTime.Now; SqlCommand sel = new SqlCommand("SELECT COUNT(*) AS n FROM utest", con); Console.WriteLine(N + "=" + (int)sel.ExecuteScalar()); SqlCommand drp = new SqlCommand("DROP TABLE utest", con); drp.ExecuteNonQuery(); Console.WriteLine("n/250 INSERT n/250 TX : " + (dt2 - dt1).TotalSeconds); } } public static void Main(string[] args) { TestNormal(); TestBundle(); TestUnion(); TestUnionSmart(); } } }