• .NET 一次读取几百条数据优化,从原来30分钟优化到30秒


    1、全部数据读取到内存,

    不要使用string,而是使用stringbuilder,stringbuilder的效率非常高

    2、添加到数据库

    不要使用excute,而是使用事务,几百万条数据会请求数据库几百万次,而使用事务,只请求一次,速度提高几百万倍

      string file = fileDialog.FileName;//返回文件的完整路径    
                        SqliteHelper.ExecuteNonQuery("delete  from  BaseData");
                        StreamReader sr = new StreamReader(file, Encoding.Default);
                        string content;
                        StringBuilder sql = new StringBuilder();
                        int count = 0;
                        int num = 0;
                        DateTime starttime = DateTime.Now;
                        var a = sr.ReadToEnd();
                        List<string> resultList = a.Split('.').ToList();
                        List<BaseData> baseDatas = new List<BaseData>();  
                        List<string> sqlString = new List<string>();
                        Thread thread = new Thread(() =>
                        {
                            foreach (var item in resultList)
                            {
                                var c = item.Replace("
    ", "");
                                if (string.IsNullOrEmpty(c))
                                    continue;
                                List<string> str =c .Split(',').ToList();
                 
                                if (str.Count() > 0)
                                {
                                    //Console.WriteLine(content.ToString());
                                   //  sql.AppendLine($"INSERT INTO BaseData (   ProductCode,XinHao,Sku) values ('{str[0]}','{str[1]}','{str[2]}');");
    
                                    sqlString.Add($"INSERT INTO BaseData (   ProductCode,XinHao,Sku) values ('{str[0]}','{str[1]}','{str[2]}');");
                                    num++;
                                   // baseDatas.Add(new BaseData() {  ProductCode = str[0], XinHao = str[1], Sku = str[2]});
                                    //count++;
                                    //label1.Text = $"{count}条";
    
                      
                                    //if (num % 5000 == 0)
                                    //{
                                    //    //count += DapperHelper<BaseData>.insertDbBatch2(baseDatas);
                                    //    //count += DapperHelper<BaseData>.ExecuteTransaction(sqlString);
                                    //    count += DapperHelper<BaseData>.insertDbBatch3(sqlString);
                                    //    sqlString.Clear();
                                    //    sql.Clear();
                                    //    num = 0;
                                    //    baseDatas.Clear();
                                  
                                    //}
                                    DateTime endtime = DateTime.Now;
                                    label2.Text = $"{(endtime - starttime).TotalSeconds}秒";
    
                                }
                            }
    
    
                            if (sqlString.Any())
                            {
    
                                // count += SqliteHelper.ExecuteNonQuery(sql.ToString());
                                //count += DapperHelper<BaseData>.insertDbBatch(sql.ToString());
                                //  count += DapperHelper<BaseData>.Execute(sql.ToString(),null); ;
                               count += DapperHelper<BaseData>.insertDbBatch3(sqlString);
                                DateTime endtime = DateTime.Now;
                                label2.Text = $"{(endtime - starttime).TotalSeconds}分";
                                if (count > 0)
                                    MessageBox.Show("保存成功");
                                else
                                    MessageBox.Show("保存失败");
                            }
                        });
                        thread.Start();
    
    
                    }
      public static int insertDbBatch3(List<string> sqlList)
            {
                
                SQLiteConnection conn = new SQLiteConnection(connectionString);
                conn.Open();
                SQLiteCommand cmd = new SQLiteCommand(conn);
                SQLiteTransaction trans = conn.BeginTransaction(); // <-------------------
                cmd.Transaction = trans;
                int cnt = 0;
                try
                {
    
                    foreach (var sql in sqlList)
                    {
                        cmd.CommandText = sql;
                        cnt += cmd.ExecuteNonQuery();
                    }
                   
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }
    
    
                }
                return cnt;
            }
  • 相关阅读:
    【计算机网络】网络地址转换NAT
    红黑树
    引用和取地址区别
    [网络编程] TCP、UDP区别以及TCP传输原理、拥塞避免、连接建立、连接释放总结
    操作系统大端模式和小端模式
    点乘和叉乘
    HMM模型
    Application_Start
    跨函数使用内存空间
    框架
  • 原文地址:https://www.cnblogs.com/topguntopgun/p/12311417.html
Copyright © 2020-2023  润新知