Skip to content
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

[Perf] RevEng: Get EngineEdition & CompatibilityLevel once per connection #20026

Merged
merged 1 commit into from
Feb 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ private static readonly ISet<string> _maxLengthRequiredTypes
"nvarchar"
};

private const string NamePartRegex
private const string _namePartRegex
= @"(?:(?:\[(?<part{0}>(?:(?:\]\])|[^\]])+)\])|(?<part{0}>[^\.\[\]]+))";

private static readonly Regex _partExtractor
= new Regex(
string.Format(
CultureInfo.InvariantCulture,
@"^{0}(?:\.{1})?$",
string.Format(CultureInfo.InvariantCulture, NamePartRegex, 1),
string.Format(CultureInfo.InvariantCulture, NamePartRegex, 2)),
string.Format(CultureInfo.InvariantCulture, _namePartRegex, 1),
string.Format(CultureInfo.InvariantCulture, _namePartRegex, 2)),
RegexOptions.Compiled,
TimeSpan.FromMilliseconds(1000));

Expand All @@ -76,6 +76,9 @@ private static readonly Regex _partExtractor
{ "bigint", new[] { -9223372036854775808L, 9223372036854775807L } }
};

private byte? _compatibilityLevel;
private int? _engineEdition;

/// <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
Expand Down Expand Up @@ -125,6 +128,9 @@ public override DatabaseModel Create(DbConnection connection, DatabaseModelFacto

try
{
_compatibilityLevel = GetCompatibilityLevel(connection);
_engineEdition = GetEngineEdition(connection);

databaseModel.DatabaseName = connection.Database;
databaseModel.DefaultSchema = GetDefaultSchema(connection);

Expand All @@ -135,7 +141,7 @@ public override DatabaseModel Create(DbConnection connection, DatabaseModelFacto
var tableList = options.Tables.ToList();
var tableFilter = GenerateTableFilter(tableList.Select(Parse).ToList(), schemaFilter);

if (SupportsSequences(connection))
if (SupportsSequences())
{
GetSequences(connection, databaseModel, schemaFilter, typeAliases);
}
Expand Down Expand Up @@ -166,11 +172,34 @@ public override DatabaseModel Create(DbConnection connection, DatabaseModelFacto
}
finally
{
_compatibilityLevel = null;
_engineEdition = null;

if (!connectionStartedOpen)
{
connection.Close();
}
}

static int GetEngineEdition(DbConnection connection)
{
using var command = connection.CreateCommand();
command.CommandText = @"
SELECT SERVERPROPERTY('EngineEdition');";
return (int)command.ExecuteScalar();
}

static byte GetCompatibilityLevel(DbConnection connection)
{
using var command = connection.CreateCommand();
command.CommandText = $@"
SELECT compatibility_level
FROM sys.databases
WHERE name = '{connection.Database}';";

var result = command.ExecuteScalar();
return result != null ? Convert.ToByte(result) : (byte)0;
}
}

private string? GetDefaultSchema(DbConnection connection)
Expand Down Expand Up @@ -420,8 +449,8 @@ private void GetTables(
using var command = connection.CreateCommand();
var tables = new List<DatabaseTable>();

var supportsMemoryOptimizedTable = SupportsMemoryOptimizedTable(connection);
var supportsTemporalTable = SupportsTemporalTable(connection);
var supportsMemoryOptimizedTable = SupportsMemoryOptimizedTable();
var supportsTemporalTable = SupportsTemporalTable();

var commandText = @"
SELECT
Expand Down Expand Up @@ -592,7 +621,7 @@ UNION ALL
LEFT JOIN [sys].[computed_columns] AS [cc] ON [c].[object_id] = [cc].[object_id] AND [c].[column_id] = [cc].[column_id]
LEFT JOIN [sys].[default_constraints] AS [dc] ON [c].[object_id] = [dc].[parent_object_id] AND [c].[column_id] = [dc].[parent_column_id]";

if (SupportsTemporalTable(connection))
if (SupportsTemporalTable())
{
commandText += " WHERE [c].[is_hidden] = 0";
}
Expand Down Expand Up @@ -804,7 +833,7 @@ FROM [sys].[indexes] AS [i]
WHERE "
+ tableFilter;

if (SupportsTemporalTable(connection))
if (SupportsTemporalTable())
{
commandText += @"
AND CAST([i].[object_id] AS nvarchar(12)) + '#' + CAST([i].[index_id] AS nvarchar(12)) NOT IN
Expand Down Expand Up @@ -1074,63 +1103,32 @@ FROM [sys].[foreign_keys] AS [f]
}
}

private bool SupportsTemporalTable(DbConnection connection)
private bool SupportsTemporalTable()
{
return CompatibilityLevel(connection) >= 130 && EngineEdition(connection) != 6;
return _compatibilityLevel >= 130 && _engineEdition != 6;
}

private bool SupportsMemoryOptimizedTable(DbConnection connection)
private bool SupportsMemoryOptimizedTable()
{
return CompatibilityLevel(connection) >= 120 && EngineEdition(connection) != 6;
return _compatibilityLevel >= 120 && _engineEdition != 6;
}

private bool SupportsSequences(DbConnection connection)
private bool SupportsSequences()
{
return CompatibilityLevel(connection) >= 110 && EngineEdition(connection) != 6;
}

private int EngineEdition(DbConnection connection)
{
using var command = connection.CreateCommand();
command.CommandText = @"
SELECT SERVERPROPERTY('EngineEdition');";
return (int)command.ExecuteScalar();
}

private byte CompatibilityLevel(DbConnection connection)
{
using var command = connection.CreateCommand();
command.CommandText = $@"
SELECT compatibility_level
FROM sys.databases
WHERE name = '{connection.Database}';";

var result = command.ExecuteScalar();
return result != null ? Convert.ToByte(result) : (byte)0;
return _compatibilityLevel >= 110 && _engineEdition != 6;
}

private static string DisplayName(string? schema, string name)
=> (!string.IsNullOrEmpty(schema) ? schema + "." : "") + name;

private static ReferentialAction? ConvertToReferentialAction(string? onDeleteAction)
{
switch (onDeleteAction)
=> onDeleteAction switch
{
case "NO_ACTION":
return ReferentialAction.NoAction;

case "CASCADE":
return ReferentialAction.Cascade;

case "SET_NULL":
return ReferentialAction.SetNull;

case "SET_DEFAULT":
return ReferentialAction.SetDefault;

default:
return null;
}
}
"NO_ACTION" => ReferentialAction.NoAction,
"CASCADE" => ReferentialAction.Cascade,
"SET_NULL" => ReferentialAction.SetNull,
"SET_DEFAULT" => ReferentialAction.SetDefault,
_ => null,
};
}
}