Skip to content

Commit

Permalink
Set Size to -1 to optimize SQL Server query plan execution - v7.0 (#1313
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ramonsmits authored Oct 27, 2023
1 parent 7c2268a commit ff16a13
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/SqlPersistence/Config/SqlDialect_MsSqlServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,18 @@ internal override void SetParameterValue(DbParameter parameter, object value)
if (value is ArraySegment<char> charSegment)
{
parameter.Value = charSegment.Array;
parameter.Size = charSegment.Count;

// Set to 4000 or -1 to improve query execution plan reuse
// Must be set when exceeding 4000 characters for nvarchar(max) https://stackoverflow.com/a/973269/199551
parameter.Size = charSegment.Count > 4000 ? -1 : 4000;
}
else if (value is string stringValue)
{
parameter.Value = stringValue;

// Set to 4000 or -1 to improve query execution plan reuse
// Must be set when exceeding 4000 characters for nvarchar(max) https://stackoverflow.com/a/973269/199551
parameter.Size = stringValue.Length > 4000 ? -1 : 4000;
}
else
{
Expand Down

0 comments on commit ff16a13

Please sign in to comment.