sql server 批量插入 bluk 和 表值参数

16 篇文章 0 订阅
9 篇文章 0 订阅

普通插入:

 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 插入:

      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 + "秒");
        }

表值参数:  需要到 sql server 先定义好 表参数 类型    

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


做法类似于 创建一个实体类

 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  + "秒");
        }

按照上面的数据 都是插入10万条数据 效率如下:

再测试 100万数据效率  普通插入 已经没耐性等了,确实等不到了

再测试下 1000万条数据的效率  这个效率确实太屌了

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值