表中需要添加500 000行。但我的代码版本需要花费太多时间。 表问题5字段:id,NameQuestion,DetailsQuestion,UserId(Guid),Category(另一个表,多对多) 添加到表的代码示例问题500 000行。
public void AddFive()
{
var q = new Question();
int counQ = 0;
for (int i=1; i<=100000; i++)
{
foreach (var x in GetUsers()) // 5 пользователей
{
++counQ;
q = new Question
{
NameQuestion = "TestQuestion" + counQ,
DetailsQuestion = "TestQuestion" + counQ,
UserId =x,
Category = GetcategoryList()
};
_context.Question.Add(q);
}
_context.SaveChanges();
}
}
public List<Guid> GetUsers()
{
List<Guid> userList = (from x in _context.aspnet_Users
select x.UserId).ToList();
return userList;
}
public List<Category> GetcategoryList()
{
var category = new List<Category>();
Random rand = new Random();
var count = rand.Next(1, 7);
for (int i = 1; i <= count; i++)
{
var idCategory = rand.Next(1, 7);
category.Add(_context.Category.Find(idCategory));
}
return category.ToList();
}
如何加快这个过程?
答案 0 :(得分:0)
您应该为每个新用户每100次迭代关闭并重新创建一个新上下文,以清除上下文并避免加收费用
考虑从您的上下文关闭Configuration.AutoDetectChangesEnabled属性,以防止自动调用DetectChanges()方法
答案 1 :(得分:0)
要插入如此大量的记录,最好使用bcp(批量复制):
questionDT = new DataTable();
questionDT.Columns.Add(new DataColumn("NameQuestion", typeof(string)));
questionDT.Columns.Add(new DataColumn("DetailsQuestion", typeof(string)));
questionDT.Columns.Add(new DataColumn("UserId", typeof(int)));
var q = new Question();
int counQ = 0;
for (int i=1; i<=100000; i++)
{
foreach (var x in GetUsers())
{
++counQ;
var dr = questionDT.NewRow();
dr["NameQuestion"] = "TestQuestion" + counQ;
dr["DetailsQuestion"] = "TestQuestion" + counQ;
dr["UserId"] = x;
questionDT.Rows.Add(dr);
}
}
using (var c = new SqlConnection(connectionstring))
{
c.Open();
using (var bcp = new SqlBulkCopy(c))
{
bcp.BatchSize = 10000;
bcp.ColumnMappings.Add("NameQuestion", "NameQuestion");
bcp.ColumnMappings.Add("DetailsQuestion", "DetailsQuestion");
bcp.ColumnMappings.Add("UserId", "UserId");
bcp.DestinationTableName = "Questions";
bcp.BulkCopyTimeout = 0;
bcp.WriteToServer(questionDT);
}
c.Close();
}
BCP适用于大型插入,它会删除索引和外键以进行更新,并在更新后重新添加它们