不同 DbContext 的并行执行比非并行版本慢

Parallel execution of different DbContexts is slower than nonparallel version

我有一个简单的 table,有 2 列(一列是 identity,另一列是一些 char 列):

CREATE TABLE [dbo].[tbl]
(
    [id] [INT] IDENTITY(1,1) NOT NULL,
    [col] [CHAR](32) NULL,
    CONSTRAINT [PK_tbl] PRIMARY KEY CLUSTERED ([id] ASC)
)

我们有一个函数可以执行一些较长的 运行 操作。这是一些伪代码:

void doWork()
{
    using(context)
    {
        doLongPart1(context);
        ...
        doLongPartN(context);
    }
}

现在我正尝试使用各自的上下文将它们隔离在单独的任务中。但令人惊讶的是,有任务的版本比没有任务的版本要花更多的时间。我在这里插入 10,000 行。时间是:~54000ms 对于没有任务的版本和 ~57000ms 有任务。我正在使用 EF6.0,这里是完整的重现代码:

初始版本

static void Main(string[] args)
{
    Stopwatch stopwatch = Stopwatch.StartNew();
    var c = 10000;

    var c1 = new TestEntities();
    for (int i = 1; i < c / 2; i++)
        c1.tbls.Add(new tbl { col = i.ToString() });

    c1.SaveChanges();

    var c2 = new TestEntities();
    for (int i = c / 2; i < c; i++)
        c2.tbls.Add(new tbl { col = i.ToString() });

    c2.SaveChanges();

    stopwatch.Stop();

    Console.WriteLine(stopwatch.ElapsedMilliseconds);
    Console.ReadLine();
}

有任务的版本

static void Main(string[] args)
{
    Stopwatch stopwatch = Stopwatch.StartNew();
    var c = 10000;
    Task[] tasks = new Task[2];

    tasks[0] = Task.Run(() =>
    {
        var c1 = new TestEntities();

        for (int i = 1; i < c / 2; i++)
            c1.tbls.Add(new tbl { col = i.ToString() });
        c1.SaveChanges();
    });

    tasks[1] = Task.Run(() =>
    {
        var c2 = new TestEntities();

        for (int i = c / 2; i < c; i++)
            c2.tbls.Add(new tbl { col = i.ToString() });
        c2.SaveChanges();
    });

    Task.WaitAll(tasks);

    stopwatch.Stop();
    Console.WriteLine(stopwatch.ElapsedMilliseconds);

    Console.ReadLine();
}   

我也试过通过存储过程来做到这一点:

CREATE PROC spTbl @s CHAR(32)
AS
    INSERT INTO dbo.tbl (col)
    VALUES (@s)

和代码:

static void Main(string[] args)
{
    Stopwatch stopwatch = Stopwatch.StartNew();
    var c = 10000; 
    Task[] tasks = new Task[2];

    tasks[0] = Task.Run(() =>
    {
        var c1 = new TestEntities();

        for (int i = 1; i < c / 2; i++)
            c1.spTbl(i.ToString());
    });

    tasks[1] = Task.Run(() =>
    {
        var c2 = new TestEntities();

        for (int i = c / 2; i < c; i++)
            c2.spTbl(i.ToString());
    });

    Task.WaitAll(tasks);
}

我什至尝试配置 SQL 服务器:

sp_configure 'show advanced options', 1;
GO
RECONFIGURE WITH OVERRIDE;
GO
sp_configure 'max degree of parallelism', 8;
GO
RECONFIGURE WITH OVERRIDE;
GO

但对我来说没有任何用处。谁能指出我正确的方向?

C# 程序不会使您的数据库更快。

如果您的问题是在数据库中插入操作的速度,那么使其并行不会解除瓶颈,它仍然是在数据库中插入操作的问题。

一般能看的就是两(3)个东西

1) 在您的数据库服务器中更快 drives/SSD(和或更多 ram)

2) 减少索引的数量,因为这些都必须在插入操作时更新

对于某些操作,您甚至可以删除索引,插入您需要的所有数据,然后重新创建索引,因为您的索引只会在每次插入行时更新一次

更新: 现在我更详细地查看了您的数字,1000 queries/second 在我见过的应用程序中并不少见。

更新 2: 您可能会考虑的一种可能的解决方案是批量插入 https://efbulkinsert.codeplex.com/ 因为即使使用存储过程,您的开销也在于查询次数而不是查询(假设您没有索引和计算列)