-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nonoptimal database query #1302
Open
thefrozenfruit
wants to merge
2
commits into
danielgerlag:master
Choose a base branch
from
thefrozenfruit:issue-1169
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
...sistence.EntityFramework/Services/LargeDataOptimizedEntityFrameworkPersistenceProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using WorkflowCore.Persistence.EntityFramework.Models; | ||
using WorkflowCore.Models; | ||
using WorkflowCore.Persistence.EntityFramework.Interfaces; | ||
using System.Threading; | ||
using WorkflowCore.Interface; | ||
|
||
namespace WorkflowCore.Persistence.EntityFramework.Services | ||
{ | ||
public sealed class LargeDataOptimizedEntityFrameworkPersistenceProvider : EntityFrameworkPersistenceProvider, IPersistenceProvider | ||
{ | ||
private readonly IWorkflowDbContextFactory _contextFactory; | ||
|
||
public LargeDataOptimizedEntityFrameworkPersistenceProvider(IWorkflowDbContextFactory contextFactory, bool canCreateDb, bool canMigrateDb) | ||
: base(contextFactory, canCreateDb, canMigrateDb) | ||
{ | ||
_contextFactory = contextFactory; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public new async Task<IEnumerable<WorkflowInstance>> GetWorkflowInstances(WorkflowStatus? status, string type, DateTime? createdFrom, DateTime? createdTo, int skip, int take) | ||
{ | ||
using (var db = _contextFactory.Build()) | ||
{ | ||
IQueryable<PersistedWorkflow> query = db.Set<PersistedWorkflow>() | ||
.Include(wf => wf.ExecutionPointers) | ||
.ThenInclude(ep => ep.ExtensionAttributes) | ||
.Include(wf => wf.ExecutionPointers) | ||
.AsSplitQuery() | ||
.AsQueryable(); | ||
|
||
if (status.HasValue) | ||
{ | ||
query = query.Where(x => x.Status == status.Value); | ||
} | ||
|
||
if (!string.IsNullOrEmpty(type)) | ||
{ | ||
query = query.Where(x => x.WorkflowDefinitionId == type); | ||
} | ||
|
||
if (createdFrom.HasValue) | ||
{ | ||
query = query.Where(x => x.CreateTime >= createdFrom.Value); | ||
} | ||
|
||
if (createdTo.HasValue) | ||
{ | ||
query = query.Where(x => x.CreateTime <= createdTo.Value); | ||
} | ||
|
||
var rawResult = await query.OrderBy(x => x.PersistenceId).Skip(skip).Take(take).ToListAsync(); | ||
|
||
var result = new List<WorkflowInstance>(rawResult.Count); | ||
|
||
foreach (var item in rawResult) | ||
{ | ||
result.Add(item.ToWorkflowInstance()); | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
|
||
/// <inheritdoc/> | ||
public new async Task<WorkflowInstance> GetWorkflowInstance(string id, CancellationToken cancellationToken = default) | ||
{ | ||
using (var db = _contextFactory.Build()) | ||
{ | ||
var uid = new Guid(id); | ||
var raw = await db.Set<PersistedWorkflow>() | ||
.Include(wf => wf.ExecutionPointers) | ||
.ThenInclude(ep => ep.ExtensionAttributes) | ||
.Include(wf => wf.ExecutionPointers) | ||
.AsSplitQuery() | ||
.FirstAsync(x => x.InstanceId == uid, cancellationToken); | ||
|
||
return raw?.ToWorkflowInstance(); | ||
} | ||
} | ||
|
||
/// <inheritdoc/> | ||
public new async Task<IEnumerable<WorkflowInstance>> GetWorkflowInstances(IEnumerable<string> ids, CancellationToken cancellationToken = default) | ||
{ | ||
if (ids == null) | ||
{ | ||
return Array.Empty<WorkflowInstance>(); | ||
} | ||
|
||
using (var db = _contextFactory.Build()) | ||
{ | ||
var uids = ids.Select(i => new Guid(i)); | ||
var raw = db.Set<PersistedWorkflow>() | ||
.Include(wf => wf.ExecutionPointers) | ||
.ThenInclude(ep => ep.ExtensionAttributes) | ||
.Include(wf => wf.ExecutionPointers) | ||
.AsSplitQuery() | ||
.Where(x => uids.Contains(x.InstanceId)); | ||
|
||
var persistedWorkflows = await raw.ToListAsync(cancellationToken); | ||
|
||
return persistedWorkflows.Select(i => i.ToWorkflowInstance()); | ||
} | ||
} | ||
|
||
/// <inheritdoc/> | ||
public new async Task PersistWorkflow(WorkflowInstance workflow, CancellationToken cancellationToken = default) | ||
{ | ||
using (var db = _contextFactory.Build()) | ||
using (var transaction = await db.Database.BeginTransactionAsync(IsolationLevel.RepeatableRead, cancellationToken)) | ||
{ | ||
var uid = new Guid(workflow.Id); | ||
var existingEntity = await db.Set<PersistedWorkflow>() | ||
.Where(x => x.InstanceId == uid) | ||
.Include(wf => wf.ExecutionPointers) | ||
.ThenInclude(ep => ep.ExtensionAttributes) | ||
.Include(wf => wf.ExecutionPointers) | ||
.AsSplitQuery() | ||
.AsTracking() | ||
.FirstAsync(cancellationToken); | ||
|
||
_ = workflow.ToPersistable(existingEntity); | ||
|
||
await db.SaveChangesAsync(cancellationToken); | ||
|
||
await transaction.CommitAsync(cancellationToken); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because it seems you're checking for
raw
being not nullThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/danielgerlag/workflow-core/blob/master/src/providers/WorkflowCore.Persistence.EntityFramework/Services/EntityFrameworkPersistenceProvider.cs#L108