- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我已经处理这个问题 2 天了,我找不到解决方案。
using (TaxablePersonContext context = new TaxablePersonContext(this.ConnectionString))
{
context.Configuration.AutoDetectChangesEnabled = false;
foreach(TaxablePerson p in persons) // Persons has always size 1000
{
// TaxablePerson has some other properties e.g. Name, VatId, Street,...
p.RecCreatedBy = "application name";
p.RecCreatedOn = this.SynchronizationStartDateTime;
p.RecModifiedBy = "application name";
p.RecModifiedOn = this.SynchronizationStartDateTime;
p.RecSyncDate = this.SynchronizationStartDateTime;
p.RecActive = true;
}
DateTime start1 = DateTime.Now;
context.TaxablePersons.AddRange(persons);
TimeSpan end1 = DateTime.Now.Subtract(start1);
DateTime start2 = DateTime.Now;
context.SaveChanges();
TimeSpan end2 = DateTime.Now.Subtract(start1);
}
我在 sql server 中插入 1000 条记录需要将近 10 秒,插入 10.000 条记录需要 98 秒。您能否建议如何提高 Entity Framework 插入性能。我读了这篇文章Fastest Way of Inserting in Entity Framework并包含了这篇文章中提到的提示,但插入速度仍然很慢。我需要插入 260.000 条记录,这需要 52 分钟。我正在插入 1000 个批处理,上面的代码演示了这一点。数据是从文件中读取的,当我达到 1000 条记录时,我会与数据库同步。我还可以做些什么?有人提到在使用时设置 context.Configuration.AutoDetectChangesEnabled = false;性能从几分钟提高到几秒钟。我错过了什么?我正在使用 Entity Framework 6.1.3。
最佳答案
使用 sql profiler,我发现 Entity 框架会一个接一个地发送查询,例如
INSERT INTO MyTable (id, name) VALUES (1, 'Bob')
INSERT INTO MyTable (id, name) VALUES (2, 'Peter')
INSERT INTO MyTable (id, name) VALUES (3, 'Joe')
所以实际上 sql server 在这种情况下执行 1000 个插入非常慢 - 将近 10 秒(尽管在事务中执行)。然后我构造了具有多个值的插入 - 具有许多值的 SQL 和 1000 条记录的插入花费了 5 秒(50%更好 - 更早的 10 秒)。 SQL Server 对您可以传递的 SQL 参数有限制,即 2100,因此这是您可以使用此方法做的最好的事情。
private void MultiRecordsInsert(TaxablePersonContext context, List<TaxablePerson> personsToAdd)
{
List<SqlParameter> parameters = new List<SqlParameter>();
string firstQuery = @"insert into TaxablePerson (c1, c2, c3) values ";
string query = firstQuery;
for (int i = 0; i < personsToAdd.Count; i++)
{
query += "(@c1" + i.ToString();
query += ",@c2" + i.ToString();
query += ",@c3" + i.ToString() + "),";
parameters.Add(new SqlParameter("@c1" + i.ToString(), personsToAdd[i].c1));
parameters.Add(new SqlParameter("@c2" + i.ToString(), personsToAdd[i].c2));
parameters.Add(new SqlParameter("@c3" + i.ToString(), personsToAdd[i].c3));
// table has 16 columns (I reduced here for simplicity) so: 2100 / 16 = 131,
// used 100
//
if (i % 100 == 0)
{
query = query.Substring(0, query.Length - 1); // remove last comma
context.Database.ExecuteSqlCommand(query, parameters.ToArray());
query = firstQuery;
parameters = new List<SqlParameter>();
}
}
if (parameters.Count > 0) // what is left
{
query = query.Substring(0, query.Length - 1);
context.Database.ExecuteSqlCommand(query, parameters.ToArray());
}
}
关于c# - 改进 Entity Framework 插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32716361/
我对编码还比较陌生,但并非完全没有经验。处理有关金融计算器的学校作业。如果你们中的任何人可以查看我的代码以了解不良做法/可能的改进等,那就太好了。 我确实添加了一个“动画”启动(有很多 printf
小目标Trick 论文链接: https://paperswithcode.com/paper/slicing-aided-hyper-inference-and-fine-tuning 代码链接:h
if (firstPositionCpc && (firstPosition > 0 && firstPositionCpc 0 && topOfPageCpc 0 && firstPageCpc
我有 2 个表:“packages”和“items”。 “packages”有以下列:pack_id | item_id “items”有以下列......:item_id |输入 一个包可以有多个
我目前有一个 Pandas Dataframe,我在其中执行列之间的比较。我发现一种情况,在进行比较时存在空列,由于某种原因比较返回 else 值。我添加了一个额外的语句来将其清理为空。看看我是否可以
我正在处理一个查询,通过首先舍入它们的主要日期时间键来连接一个数据库中的多个表。数据库包含来自 openhab 的性能数据,每个表只有一个名为 Time 的主日期时间行和一个名为 Value 的值行。
问候 我有一个程序创建一个类的多个实例,在所有实例上运行相同的长时间运行的 Update 方法并等待完成。我从 this question 开始关注 Kev 的方法将更新添加到 ThreadPool.
我想在下学期的类(class)中取得领先,所以我制作了这个基本版本的 Blackjack 来开始理解 C 的基础知识,我希望您有任何想法可以帮助我更好地理解 C 和其正常的编码实践。 C 中的很多东西
我有一个要求,比如: 给定一个数组,其中包含随机数。需要输出元素出现的次数,有自带解决方案: var myArr = [3,2,1,2,3,1,4,5,4,6,7,7,9,1,123,0,123];
这是我的数据库项目。 表user_ select id, name from user_; id | name ----+---------- 1 | bartek 2 | bartek
我已经完成了一个小批量脚本来调整(动态)一些图像的大小: for a in *.{png,PNG,jpg,JPG,jpeg,JPEG,bmp,BMP} ; do convert "$a" -resiz
是否有更 pythonic 的方法来执行以下代码?我想在一行中完成 parsed_rows 是一个可以返回大小为 3 或 None 的元组的函数。 parsed_rows = [ parse_row(
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improv
下面的代码完成了我想要的,但还有其他更像 python 风格的方式吗? 文件格式: key1:value1,key2:value2,... key21:value21,key22:value22,..
如果两个英文单词只包含相同的字母,则它们是相似的。例如,food 和 good 不相似,但 dog 和 good 相似。 (如果A与B相似,则A中的所有字母都包含在B中,B中的所有字母都包含在A中。)
我有以下结构来表示二叉树: typedef struct node *pnode; typedef struct node { int val; pnode left; pnode
我有一个区域,它由受约束的 delaunay 三角剖分表示。我正在解决在两点之间寻找路径的问题。我正在使用 Marcelo Kallmann 提供的论文作为解决此问题的引用点。然而,而不是使用 Kal
如果我需要检查文本(字符串)中是否存在单词 A 或单词 B,如果我这样做会有性能差异: if(text.contains(wordA) || text.contains(wordB)) 要使用一些正则
Adjust To 我有上面这个简单的页面,上面有一个标签和一个文本框。我想在文本框中输入文本。 对我有帮助的 XPATH 是 //*[contains(tex
以下伪代码的elisp代码 if "the emacs version is less than 23.1.x" do something else something-else 写成 (if
我是一名优秀的程序员,十分优秀!