c# - 如何在 C# 中有效地获取、处理和保存巨大的记录集?
问题描述
我正在努力实现以下目标:
- 从 SQL DB 获取数据。
将数据传递给具有第三方方法 MethodforResponse 的 PerformStuff 方法(它检查输入并提供响应)
将 response(xml) 保存回 SQL DB。
下面是示例代码。性能方面它不好,如果数据库中有 1000,000 条记录,它非常慢。
它有更好的做法吗?有什么想法或提示可以让它变得更好。
请帮忙。
using thirdpartylib;
public class Program
{
static void Main(string[] args)
{
var response = PerformStuff();
Save(response);
}
public class TestRequest
{
public int col1 { get; set; }
public bool col2 { get; set; }
public string col3 { get; set; }
public bool col4 { get; set; }
public string col5 { get; set; }
public bool col6 { get; set; }
public string col7 { get; set; }
}
public class TestResponse
{
public int col1 { get; set; }
public string col2 { get; set; }
public string col3 { get; set; }
public int col4 { get; set; }
}
public TestRequest GetDataId(int id)
{
TestRequest testReq = null;
try
{
SqlCommand cmd = DB.GetSqlCommand("proc_name");
cmd.AddInSqlParam("@Id", SqlDbType.Int, id);
SqlDataReader dr = new SqlDataReader(DB.GetDataReader(cmd));
while (dr.Read())
{
testReq = new TestRequest();
testReq.col1 = dr.GetInt32("col1");
testReq.col2 = dr.GetBoolean("col2");
testReq.col3 = dr.GetString("col3");
testReq.col4 = dr.GetBoolean("col4");
testReq.col5 = dr.GetString("col5");
testReq.col6 = dr.GetBoolean("col6");
testReq.col7 = dr.GetString("col7");
}
dr.Close();
}
catch (Exception ex)
{
throw;
}
return testReq;
}
public static TestResponse PerformStuff()
{
var response = new TestResponse();
//give ids in list
var ids = thirdpartylib.Methodforid()
foreach (int id in ids)
{
var request = GetDataId(id);
var output = thirdpartylib.MethodforResponse(request);
foreach (var data in output.Elements())
{
response.col4 = Convert.ToInt32(data.Id().Class());
response.col2 = data.Id().Name().ToString();
}
}
//request details
response.col1 = request.col1;
response.col2 = request.col2;
response.col3 = request.col3;
return response;
}
public static void Save(TestResponse response)
{
var Sb = new StringBuilder();
try
{
Sb.Append("<ROOT>");
Sb.Append("<id");
Sb.Append(" col1='" + response.col1 + "'");
Sb.Append(" col2='" + response.col2 + "'");
Sb.Append(" col3='" + response.col3 + "'");
Sb.Append(" col4='" + response.col4 + "'");
Sb.Append("></Id>");
Sb.Append("</ROOT>");
var cmd = DB.GetSqlCommand("saveproc");
cmd.AddInSqlParam("@Data", SqlDbType.VarChar, Sb.ToString());
DB.ExecuteNoQuery(cmd);
}
catch (Exception ex)
{
throw;
}
}
}
谢谢!
解决方案
我认为您的问题的根源是您逐条获取和插入数据。没有办法优化它。你需要改变一般的方法。
您应该考虑一种解决方案: 1. 将一个命令中的所有数据获取到数据库。2. 处理它。3. 使用像BULK INSERT这样的技术,在一个命令中将其保存回数据库。请注意 BULK INSERT 有一定的限制,因此请仔细阅读文档。
推荐阅读
- mathematical-optimization - 基于多变量的成本优化
- java - 如何在不使用 try and catch 的情况下删除“java.lang.ArrayIndexOutOfBoundsException”错误?
- javascript - Rxjs Forkjoin,如何为特定属性返回单个数组?
- r - 使用R根据具有相同标识符的另一行中的值替换一行中的值
- c# - 为什么滚动这个 xamarin 页面这么慢?
- command-line - JMetter如何从命令行更改stringProp user_define_classpath
- css - Angular Material中的模态布局响应
- python - 嗨,我是 django 的新手,我一直在尝试实现登录/注册应用程序
- css - 如何在 CSS 网格中滚动?
- javascript - 无法使用来自使用threejs的javascript代码内部的节点服务器的get请求加载pcd文件