我怎样才能运行这个实体框架样本更快 [英] How can I run this entity framework sample faster

查看:141
本文介绍了我怎样才能运行这个实体框架样本更快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code添加约1500记录数据库:

I have this code for adding about 1500 record to database:

async void button7_Click(object sender, EventArgs e)
{
    var task = await Task.Run(() =>
    {
        Random rnd = new Random();
        for (int i = 0; i <= 1500; i++)
        {
            db.Tbls.Add(new Tbl()
            {
                Name = "User" + i + 1,
                Num = rnd.Next(10, i + 10) / 10
            });
            progress.Report(i * 100 / 1500);
        }

        db.SaveChanges();
        return db.Tbls.Count();

    });
}

但是,经过大约4秒钟完成的过程,但因为我用异步/的await 不冻结UI。现在的问题是:我怎么能改善这个code跑得更快。我如何在这里使用并行编程?你能告诉我如何使用TPL的例子吗?

But it took about 4 second to complete the process but because I used async/await it doesn't freezes the UI. Now my question is: how can I improve this code to run faster. How can I use parallel programming here? Can you show me an example of how use TPL?

修改

我用的并行循环和的AddRange 的建议,但没有任何效果。在所有建议的方式,我的进程仍然需要约4秒。我真的AP preciate如果有人能帮助我解决这个问题。

I used parallel loop and AddRange as suggested but it has no effect. In all suggested ways my process still take about 4 seconds. I really appreciate if someone can help me to solve this issue.

推荐答案

并行处理不会是很大的帮助在这里,因为你的循环本身不花时间在所有。所有消耗的时间进入两件事情:

Parallel processing won't be of big help here, because your loop by itself takes no time at all. All consumed time goes into two things:


  1. 在新项目添加到实体框架上下文(你在这里使用EF据我所知) - 它的背景下进行的许多项目某些操作(你加不只是项目),并将其变为慢您添加的多个项目。

  1. When you add new item to Entity Framework context (and you use EF here as I understand) - it performs some operations on many items in the context (not just the item you added) and it goes slower and slower the more items you added.

Perfoming 1500数据库插入还需要一些时间。

Perfoming 1500 database inserts also takes some time.

在这里做最简单的事情是减少1点在上面的列表中所用的时间。你可以这样说:

The easiest thing to do here is to reduce time taken by point 1 in the list above. You can do it like this:

async void button7_Click(object sender, EventArgs e)
{
    var task = await Task.Run(() =>
    {
        Random rnd = new Random();
        var tbls = new List<Tbl>();
        for (int i = 0; i <= 1500; i++)
        {
            tbls.Add(new Tbl()
            {
                Name = "User" + i + 1,
                Num = rnd.Next(10, i + 10) / 10
            });
            progress.Report(i * 100 / 1500);
         }
        db.Tbls.AddRange(tbls);
        db.SaveChanges();
        return db.Tbls.Count();

    });
}

通过使用的AddRange 和第一收集在简单的列表中的所有项目你会greately减少你的$ C消耗的时间$ c键的至少1秒以下

By using AddRange and first collecting all items in simple List you will greately reduce time consumed by your code to at least below 1 second.

更新。如果你想使用并行循环,即使这不会帮助,你可以做到这样的:

Update. If you want to use parallel loop, even if that won't help, you can do it like this:

int seed = Environment.TickCount;
var random = new ThreadLocal<Random>(() => new Random(Interlocked.Increment(ref seed)));
var tbls = new ConcurrentBag<Tbl>();
Parallel.For(0, 1500, (i) => {
    tbls.Add(new Tbl()
    {
        Name = "User" + i + 1,
        Num = random.Value.Next(10, i + 10) / 10
    });
});                
db.Tbls.AddRange(tbls);

注意事项:


  • 随机,所以我们使用线程本地实例(一个用于并行循环内的每个线程)不是线程安全的,每一个不同的种子值。

  • 列表不是线程安全的 - 我们使用 ConcurrentBag 而不是

  • Random is not thread safe so we use thread local instances (one for each thread inside parallel loop), each one with different seed value.
  • List is not thread safe - we use ConcurrentBag instead.

这篇关于我怎样才能运行这个实体框架样本更快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆