.NET Core EF core 中对 dbcontext 的并行调用
Parallel calls to dbcontext in .NET Core EF core
项目使用:
- SQL 服务器
- .NET 核心 3
- EF核心
在我的项目中,我有数百万个 data/records 我需要在来自 4 个不同表的 API 中发出并行读取请求。
DBContext
有一个范围内的生命周期,这是推荐的。我需要对 DBContext
进行并行调用,但是当我尝试时,它说第二个实例已经启动,而另一个实例已经在使用中(因为生命周期是有范围的)。
- 我尝试对那些特定查询使用
AsNoTracking()
,但仍然出现同样的错误。
- 我无法使用瞬态生命周期。
项目使用存储库模式,不能在控制器中使用新的 DBcontext
实例。
public MemberRepository(
IMemberEntityMappingHelper memberEntityMappingHelper,
IMapperExtension mapper,
ProjectDBContext context)
: base(context, mapper)
{
_memberEntityMappingHelper = memberEntityMappingHelper;
}
public List<MemberSummaryModel> GetMembers(int foreignKeyId)
{
List<MemberEntity> members = _context.Members
.Include(x => x.Sample)
.Where(x =>
x.SampleForeignkeyId == foreignKeyId
&& x.IsDeleted == false)
.ToList();
return _memberEntityMappingHelper.EntityToSummaryModelList(members);
}
并行调用 -
Parallel.Invoke(() =>
{
// Read from table 1
},
() =>
{
// Read from table 2
});
我需要优化我的代码以缩短响应时间。
- 有什么方法可以 运行 并行查询(只读)?
DbContext
不是线程安全的,一个实例不能update/create一个实例的条目,但为什么我不能进行并行读取查询?
- 一次获取 30,000 行(当我使用所有索引时)预计需要多少时间?怎样才能减少它,对我来说,加载数据需要 2 分钟?
DbContext is not thread-safe, one instance cannot update/create entries to one instance,
but why I cannot make parallel read queries?
因为 DbContext 不是 Tthread 安全的。时期。不是 "update/create" - 根本不是。其中大部分都没有假设最后一个 DbConnection 上的并行 SQL 语句,而 MARS 并没有改变这一点——它允许重叠查询结果,而不是重叠查询执行。
How much time is it expected to get 30,000 rows at a time(when I am using all the
indexes)?
索引:不如对象的大小和您的网络连接相关。上次我发现它在半秒内有 50 万个条目 - 但那些条目很小而且网络连接非常快。
Is there any way I can run parallel queries (only read)?
是,多个数据库连接,多个 DbContext。
项目使用:
- SQL 服务器
- .NET 核心 3
- EF核心
在我的项目中,我有数百万个 data/records 我需要在来自 4 个不同表的 API 中发出并行读取请求。
DBContext
有一个范围内的生命周期,这是推荐的。我需要对 DBContext
进行并行调用,但是当我尝试时,它说第二个实例已经启动,而另一个实例已经在使用中(因为生命周期是有范围的)。
- 我尝试对那些特定查询使用
AsNoTracking()
,但仍然出现同样的错误。 - 我无法使用瞬态生命周期。
项目使用存储库模式,不能在控制器中使用新的
DBcontext
实例。public MemberRepository( IMemberEntityMappingHelper memberEntityMappingHelper, IMapperExtension mapper, ProjectDBContext context) : base(context, mapper) { _memberEntityMappingHelper = memberEntityMappingHelper; } public List<MemberSummaryModel> GetMembers(int foreignKeyId) { List<MemberEntity> members = _context.Members .Include(x => x.Sample) .Where(x => x.SampleForeignkeyId == foreignKeyId && x.IsDeleted == false) .ToList(); return _memberEntityMappingHelper.EntityToSummaryModelList(members); }
并行调用 -
Parallel.Invoke(() =>
{
// Read from table 1
},
() =>
{
// Read from table 2
});
我需要优化我的代码以缩短响应时间。
- 有什么方法可以 运行 并行查询(只读)?
DbContext
不是线程安全的,一个实例不能update/create一个实例的条目,但为什么我不能进行并行读取查询?- 一次获取 30,000 行(当我使用所有索引时)预计需要多少时间?怎样才能减少它,对我来说,加载数据需要 2 分钟?
DbContext is not thread-safe, one instance cannot update/create entries to one instance, but why I cannot make parallel read queries?
因为 DbContext 不是 Tthread 安全的。时期。不是 "update/create" - 根本不是。其中大部分都没有假设最后一个 DbConnection 上的并行 SQL 语句,而 MARS 并没有改变这一点——它允许重叠查询结果,而不是重叠查询执行。
How much time is it expected to get 30,000 rows at a time(when I am using all the indexes)?
索引:不如对象的大小和您的网络连接相关。上次我发现它在半秒内有 50 万个条目 - 但那些条目很小而且网络连接非常快。
Is there any way I can run parallel queries (only read)?
是,多个数据库连接,多个 DbContext。