From 77ce57ce9789e9dd3f717031603e0f21257b45b5 Mon Sep 17 00:00:00 2001 From: maliming <6908465+maliming@users.noreply.github.com> Date: Wed, 14 Oct 2020 21:52:51 +0800 Subject: [PATCH] Use AsSplitQuery to improve query performance. https://docs.microsoft.com/en-us/ef/core/querying/related-data/eager#single-and-split-queries --- .../AbpAuditLoggingEfCoreQueryableExtensions.cs | 1 + .../EntityFrameworkCore/EfCoreAuditLogRepository.cs | 2 +- .../EfCoreIdentityUserRepository.cs | 10 ++++++---- .../IdentityEfCoreQueryableExtensions.cs | 5 +++-- .../AbpIdentityServerEfCoreQueryableExtensions.cs | 4 +++- .../ApiResources/ApiResourceRepository.cs | 3 ++- .../Abp/IdentityServer/Clients/ClientRepository.cs | 1 + 7 files changed, 17 insertions(+), 9 deletions(-) diff --git a/modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/AbpAuditLoggingEfCoreQueryableExtensions.cs b/modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/AbpAuditLoggingEfCoreQueryableExtensions.cs index 6dc4bfadb8..11ba294610 100644 --- a/modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/AbpAuditLoggingEfCoreQueryableExtensions.cs +++ b/modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/AbpAuditLoggingEfCoreQueryableExtensions.cs @@ -15,6 +15,7 @@ namespace Volo.Abp.AuditLogging } return queryable + .AsSplitQuery() .Include(x => x.Actions) .Include(x => x.EntityChanges).ThenInclude(ec=>ec.PropertyChanges); } diff --git a/modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/EfCoreAuditLogRepository.cs b/modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/EfCoreAuditLogRepository.cs index 06dcf8ffed..9ce2bee070 100644 --- a/modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/EfCoreAuditLogRepository.cs +++ b/modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/EfCoreAuditLogRepository.cs @@ -143,7 +143,7 @@ namespace Volo.Abp.AuditLogging.EntityFrameworkCore public Task GetEntityChange(Guid entityChangeId) { - return DbContext.Set().AsNoTracking().IncludeDetails().Where(x => x.Id == entityChangeId).FirstAsync(); + return DbContext.Set().AsNoTracking().IncludeDetails().OrderBy(x => x.Id).Where(x => x.Id == entityChangeId).FirstAsync(); } public virtual async Task> GetEntityChangeListAsync( diff --git a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserRepository.cs b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserRepository.cs index 5f6bcb7459..f96156e4ca 100644 --- a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserRepository.cs @@ -26,6 +26,7 @@ namespace Volo.Abp.Identity.EntityFrameworkCore { return await DbSet .IncludeDetails(includeDetails) + .OrderBy(x => x.NormalizedUserName) .FirstOrDefaultAsync( u => u.NormalizedUserName == normalizedUserName, GetCancellationToken(cancellationToken) @@ -41,14 +42,14 @@ namespace Volo.Abp.Identity.EntityFrameworkCore where userRole.UserId == id select role.Name; var organizationUnitIds = DbContext.Set().Where(q => q.UserId == id).Select(q => q.OrganizationUnitId).ToArray(); - + var organizationRoleIds = await ( from ouRole in DbContext.Set() - join ou in DbContext.Set() on ouRole.OrganizationUnitId equals ou.Id + join ou in DbContext.Set() on ouRole.OrganizationUnitId equals ou.Id where organizationUnitIds.Contains(ouRole.OrganizationUnitId) select ouRole.RoleId ).ToListAsync(GetCancellationToken(cancellationToken)); - + var orgUnitRoleNameQuery = DbContext.Roles.Where(r => organizationRoleIds.Contains(r.Id)).Select(n => n.Name); var resultQuery = query.Union(orgUnitRoleNameQuery); return await resultQuery.ToListAsync(GetCancellationToken(cancellationToken)); @@ -60,7 +61,7 @@ namespace Volo.Abp.Identity.EntityFrameworkCore { var query = from userOu in DbContext.Set() join roleOu in DbContext.Set() on userOu.OrganizationUnitId equals roleOu.OrganizationUnitId - join ou in DbContext.Set() on roleOu.OrganizationUnitId equals ou.Id + join ou in DbContext.Set() on roleOu.OrganizationUnitId equals ou.Id join userOuRoles in DbContext.Roles on roleOu.RoleId equals userOuRoles.Id where userOu.UserId == id select userOuRoles.Name; @@ -89,6 +90,7 @@ namespace Volo.Abp.Identity.EntityFrameworkCore { return await DbSet .IncludeDetails(includeDetails) + .OrderBy(x => x.NormalizedEmail) .FirstOrDefaultAsync(u => u.NormalizedEmail == normalizedEmail, GetCancellationToken(cancellationToken)); } diff --git a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityEfCoreQueryableExtensions.cs b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityEfCoreQueryableExtensions.cs index 5e4a0a2876..8b6e470600 100644 --- a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityEfCoreQueryableExtensions.cs +++ b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityEfCoreQueryableExtensions.cs @@ -1,4 +1,4 @@ -using System.Linq; +using System.Linq; using Microsoft.EntityFrameworkCore; namespace Volo.Abp.Identity.EntityFrameworkCore @@ -13,6 +13,7 @@ namespace Volo.Abp.Identity.EntityFrameworkCore } return queryable + .AsSplitQuery() .Include(x => x.Roles) .Include(x => x.Logins) .Include(x => x.Claims) @@ -42,4 +43,4 @@ namespace Volo.Abp.Identity.EntityFrameworkCore .Include(x => x.Roles); } } -} \ No newline at end of file +} diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/AbpIdentityServerEfCoreQueryableExtensions.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/AbpIdentityServerEfCoreQueryableExtensions.cs index 51b490fc47..89e76d5bc4 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/AbpIdentityServerEfCoreQueryableExtensions.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/AbpIdentityServerEfCoreQueryableExtensions.cs @@ -16,6 +16,7 @@ namespace Volo.Abp.IdentityServer } return queryable + .AsSplitQuery() .Include(x => x.Secrets) .Include(x => x.UserClaims) .Include(x => x.Scopes) @@ -41,6 +42,7 @@ namespace Volo.Abp.IdentityServer } return queryable + .AsSplitQuery() .Include(x => x.AllowedGrantTypes) .Include(x => x.RedirectUris) .Include(x => x.PostLogoutRedirectUris) @@ -52,4 +54,4 @@ namespace Volo.Abp.IdentityServer .Include(x => x.Properties); } } -} \ No newline at end of file +} diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/ApiResources/ApiResourceRepository.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/ApiResources/ApiResourceRepository.cs index d4fc77ede2..b59a4a6bf3 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/ApiResources/ApiResourceRepository.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/ApiResources/ApiResourceRepository.cs @@ -26,6 +26,7 @@ namespace Volo.Abp.IdentityServer.ApiResources { var query = from apiResource in DbSet.IncludeDetails(includeDetails) where apiResource.Name == name + orderby apiResource.Name select apiResource; return await query @@ -96,4 +97,4 @@ namespace Volo.Abp.IdentityServer.ApiResources return GetQueryable().IncludeDetails(); } } -} \ No newline at end of file +} diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Clients/ClientRepository.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Clients/ClientRepository.cs index 1c7a0ccccb..e52467e4fd 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Clients/ClientRepository.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Clients/ClientRepository.cs @@ -25,6 +25,7 @@ namespace Volo.Abp.IdentityServer.Clients { return await DbSet .IncludeDetails(includeDetails) + .OrderBy(x => x.ClientId) .FirstOrDefaultAsync(x => x.ClientId == clientId, GetCancellationToken(cancellationToken)); }