sql server batch insert bluk and table valued parameters

Normal insertion:

 public static void CommonInsert()
        {
            var num = 0;
            var watch = new Stopwatch();

            using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ToString()))
            {
                con.Open();
                var cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandText = "insert into t_orders (goods_id,goods_num) values(@goods_id,@goods_num)";

                var pramsList = new List<SqlParameter>();
                pramsList.Add(new SqlParameter("@goods_id", 1));
                pramsList.Add(new SqlParameter("@goods_num", 2));
                cmd.Parameters.AddRange(pramsList.ToArray());

                watch.Start();
                for (int i = 0; i < 10; i++)
                {
                    for (int x = i * 10000; x < (i + 1) * 10000; x++)
                    {
                        num += cmd.ExecuteNonQuery();
                    }
                }
                watch.Stop();
                Console.WriteLine($"当前插入数量:{num}");
            }

            Console.WriteLine(watch.ElapsedMilliseconds / 1000 + "秒");
        } 

bluk insert:

      public static void BulkInsert()
        {
            var num = 0;
            var watch = new Stopwatch();

            using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ToString()))
            {
                con.Open();
                var bluk = new SqlBulkCopy(con);
                bluk.DestinationTableName = "t_orders";
                watch.Start();
                for (int i = 0; i < 10; i++)
                {
                    var tb = Createtable();

                    for (int x = i * 10000; x < (i + 1) * 10000; x++)
                    {
                        var row = tb.NewRow();
                        row[0] = x;
                        row[1] = 1;
                        row[2] = 2;
                        tb.Rows.Add(row);
                    }
                    bluk.BatchSize = tb.Rows.Count;
                    bluk.WriteToServer(tb);
                    num += bluk.BatchSize;
                }
                watch.Stop();
                Console.WriteLine($" BulkInsert 当前插入数量:{num}");
                con.Close();
            }
            Console.WriteLine("BulkInsert" + watch.ElapsedMilliseconds / 1000 + "秒");
        }

Table-valued parameters: You need to define the table parameter type first in sql server    

CREATE TYPE t_orders_cache AS TABLE  (id int, goods_id int,  goods_num int) 


The approach is similar to creating an entity class

 public static void TableValueInsert()
        {
            var num = 0;
            var watch = new Stopwatch();

            using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ToString()))
            {
                con.Open();
                var cmd = new SqlCommand(" insert into t_orders (goods_id,goods_num ) select goods_id,goods_num  from @t_orders",  con);

                watch.Start();
                for (int i = 0; i < 10; i++)
                {
                    var tb = Createtable();

                    for (int x = i * 10000; x < (i + 1) * 10000; x++)
                    {
                        var row = tb.NewRow();
                        row[0] = x;
                        row[1] = 1;
                        row[2] = 2;
                        tb.Rows.Add(row);
                    }
                    //指定作为参数的表
                    cmd.Parameters.Clear();
                    var tbaleVal = cmd.Parameters.AddWithValue("@t_orders", tb);
                    //tbaleVal.SqlDbType = SqlDbType.Structured;
                    //必须指明
                    // 需要先在 sql serveer 中 定义这个类型 
                    // CREATE TYPE t_orders_cache AS TABLE  (id int, goods_id int,  goods_num int) 
                    // 类似 List<>
                    tbaleVal.TypeName = "t_orders_cache";

                    num += cmd.ExecuteNonQuery();
                }
                watch.Stop();
                Console.WriteLine($" TableValueInsert 当前插入数量:{num}");
                con.Close();
            }
            Console.WriteLine("TableValueInsert" + (decimal )watch.ElapsedMilliseconds /(decimal)1000  + "秒");
        }

According to the above data, 100,000 pieces of data are inserted. The efficiency is as follows:

Then test the efficiency of 1 million data, ordinary inserts have no patience to wait, really can't wait

Then test the efficiency of 10 million pieces of data. This efficiency is really too awkward.

 

 

Guess you like

Origin blog.csdn.net/qq_36445227/article/details/108854847