-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Query: Auto-buffer data readers in SqlServer when split query & MARS off
Resolves #21420
- Loading branch information
Showing
7 changed files
with
227 additions
and
9 deletions.
There are no files selected for viewing
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
75 changes: 75 additions & 0 deletions
75
src/EFCore.SqlServer/Query/Internal/SqlServerCompiledQueryCacheKeyGenerator.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,75 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Linq.Expressions; | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Query; | ||
using Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
|
||
namespace Microsoft.EntityFrameworkCore.SqlServer.Query.Internal | ||
{ | ||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public class SqlServerCompiledQueryCacheKeyGenerator : RelationalCompiledQueryCacheKeyGenerator | ||
{ | ||
private readonly ISqlServerConnection _sqlServerConnection; | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public SqlServerCompiledQueryCacheKeyGenerator( | ||
[NotNull] CompiledQueryCacheKeyGeneratorDependencies dependencies, | ||
[NotNull] RelationalCompiledQueryCacheKeyGeneratorDependencies relationalDependencies, | ||
[NotNull] ISqlServerConnection sqlServerConnection) | ||
: base(dependencies, relationalDependencies) | ||
{ | ||
Check.NotNull(sqlServerConnection, nameof(sqlServerConnection)); | ||
|
||
_sqlServerConnection = sqlServerConnection; | ||
} | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public override object GenerateCacheKey(Expression query, bool async) | ||
=> new SqlServerCompiledQueryCacheKey( | ||
GenerateCacheKeyCore(query, async), | ||
_sqlServerConnection.IsMultipleActiveResultSetsEnabled); | ||
|
||
private readonly struct SqlServerCompiledQueryCacheKey | ||
{ | ||
private readonly RelationalCompiledQueryCacheKey _relationalCompiledQueryCacheKey; | ||
private readonly bool _multipleActiveResultSetsEnabled; | ||
|
||
public SqlServerCompiledQueryCacheKey( | ||
RelationalCompiledQueryCacheKey relationalCompiledQueryCacheKey, bool multipleActiveResultSetsEnabled) | ||
{ | ||
_relationalCompiledQueryCacheKey = relationalCompiledQueryCacheKey; | ||
_multipleActiveResultSetsEnabled = multipleActiveResultSetsEnabled; | ||
} | ||
|
||
public override bool Equals(object obj) | ||
=> !(obj is null) | ||
&& obj is SqlServerCompiledQueryCacheKey sqlServerCompiledQueryCacheKey | ||
&& Equals(sqlServerCompiledQueryCacheKey); | ||
|
||
private bool Equals(SqlServerCompiledQueryCacheKey other) | ||
=> _relationalCompiledQueryCacheKey.Equals(other._relationalCompiledQueryCacheKey) | ||
&& _multipleActiveResultSetsEnabled == other._multipleActiveResultSetsEnabled; | ||
|
||
public override int GetHashCode() => HashCode.Combine(_relationalCompiledQueryCacheKey, _multipleActiveResultSetsEnabled); | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/EFCore.SqlServer/Query/Internal/SqlServerQueryCompilationContext.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,46 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Query; | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
|
||
namespace Microsoft.EntityFrameworkCore.SqlServer.Query.Internal | ||
{ | ||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public class SqlServerQueryCompilationContext : RelationalQueryCompilationContext | ||
{ | ||
private readonly bool _multipleActiveResultSetsEnabled; | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public SqlServerQueryCompilationContext( | ||
[NotNull] QueryCompilationContextDependencies dependencies, | ||
[NotNull] RelationalQueryCompilationContextDependencies relationalDependencies, | ||
bool async, | ||
bool multipleActiveResultSetsEnabled) | ||
: base(dependencies, relationalDependencies, async) | ||
{ | ||
_multipleActiveResultSetsEnabled = multipleActiveResultSetsEnabled; | ||
} | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public override bool IsBuffering => base.IsBuffering | ||
|| (QuerySplittingBehavior == EntityFrameworkCore.QuerySplittingBehavior.SplitQuery | ||
&& !_multipleActiveResultSetsEnabled); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/EFCore.SqlServer/Query/Internal/SqlServerQueryCompilationContextFactory.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,61 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Query; | ||
using Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.EntityFrameworkCore.SqlServer.Query.Internal | ||
{ | ||
/// <summary> | ||
/// <para> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </para> | ||
/// <para> | ||
/// The service lifetime is <see cref="ServiceLifetime.Scoped" />. This means that each | ||
/// <see cref="DbContext" /> instance will use its own instance of this service. | ||
/// The implementation may depend on other services registered with any lifetime. | ||
/// The implementation does not need to be thread-safe. | ||
/// </para> | ||
/// </summary> | ||
public class SqlServerQueryCompilationContextFactory : IQueryCompilationContextFactory | ||
{ | ||
private readonly QueryCompilationContextDependencies _dependencies; | ||
private readonly RelationalQueryCompilationContextDependencies _relationalDependencies; | ||
private readonly ISqlServerConnection _sqlServerConnection; | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public SqlServerQueryCompilationContextFactory( | ||
[NotNull] QueryCompilationContextDependencies dependencies, | ||
[NotNull] RelationalQueryCompilationContextDependencies relationalDependencies, | ||
[NotNull] ISqlServerConnection sqlServerConnection) | ||
{ | ||
Check.NotNull(dependencies, nameof(dependencies)); | ||
Check.NotNull(relationalDependencies, nameof(relationalDependencies)); | ||
|
||
_dependencies = dependencies; | ||
_relationalDependencies = relationalDependencies; | ||
_sqlServerConnection = sqlServerConnection; | ||
} | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public virtual QueryCompilationContext Create(bool async) | ||
=> new SqlServerQueryCompilationContext( | ||
_dependencies, _relationalDependencies, async, _sqlServerConnection.IsMultipleActiveResultSetsEnabled); | ||
} | ||
} |
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