-
Notifications
You must be signed in to change notification settings - Fork 292
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
Add SqlCommand.EnableOptimizedParameterBinding Feature #1041
Changes from all commits
0057c01
372c15d
3e43f8c
250bec5
b84da27
a553c1b
1f88591
afb5050
34e552e
206d30d
4f465fa
aa1b1f8
d6c66a1
92961b1
a1302d0
3a01525
2b5e8c9
421ee21
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -286,8 +286,13 @@ The following console application creates updates data within the **AdventureWor | |
<exception cref="T:System.InvalidOperationException"> | ||
The | ||
<see cref="T:Microsoft.Data.SqlClient.SqlConnection" /> | ||
closed or dropped during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support). | ||
</exception> | ||
closed or dropped during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support). | ||
|
||
- or - | ||
|
||
<see cref="P:Microssoft.Data.SqlClient.SqlCommand.EnableOptimizedParameterBinding" /> | ||
is set to true and a parameter with direction Output or InputOutput has been added to the <see cref="P:Microsoft.Data.SqlClient.SqlCommand.Parameters" /> collection. | ||
</exception> | ||
<exception cref="T:System.IO.IOException"> | ||
An error occurred in a | ||
<see cref="T:System.IO.Stream" /> | ||
|
@@ -399,8 +404,13 @@ To set up this example, create a new Windows application. Put a <xref:System.Win | |
<exception cref="T:System.InvalidOperationException"> | ||
The | ||
<see cref="T:Microsoft.Data.SqlClient.SqlConnection" /> | ||
closed or dropped during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support). | ||
</exception> | ||
closed or dropped during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support). | ||
|
||
- or - | ||
|
||
<see cref="P:Microssoft.Data.SqlClient.SqlCommand.EnableOptimizedParameterBinding" /> | ||
is set to true and a parameter with direction Output or InputOutput has been added to the <see cref="P:Microsoft.Data.SqlClient.SqlCommand.Parameters" /> collection. | ||
</exception> | ||
</BeginExecuteNonQuery> | ||
<BeginExecuteReader name="default"> | ||
<summary> | ||
|
@@ -477,8 +487,13 @@ The following console application starts the process of retrieving a data reader | |
<exception cref="T:System.InvalidOperationException"> | ||
The | ||
<see cref="T:Microsoft.Data.SqlClient.SqlConnection" /> | ||
closed or dropped during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support). | ||
</exception> | ||
closed or dropped during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support). | ||
|
||
- or - | ||
|
||
<see cref="P:Microssoft.Data.SqlClient.SqlCommand.EnableOptimizedParameterBinding" /> | ||
is set to true and a parameter with direction Output or InputOutput has been added to the <see cref="P:Microsoft.Data.SqlClient.SqlCommand.Parameters" /> collection. | ||
</exception> | ||
<exception cref="T:System.IO.IOException"> | ||
An error occurred in a | ||
<see cref="T:System.IO.Stream" /> | ||
|
@@ -584,8 +599,13 @@ This example also passes the `CommandBehavior.CloseConnection` and `CommandBehav | |
<exception cref="T:System.InvalidOperationException"> | ||
The | ||
<see cref="T:Microsoft.Data.SqlClient.SqlConnection" /> | ||
closed or dropped during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support). | ||
</exception> | ||
closed or dropped during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support). | ||
|
||
- or - | ||
|
||
<see cref="P:Microssoft.Data.SqlClient.SqlCommand.EnableOptimizedParameterBinding" /> | ||
is set to true and a parameter with direction Output or InputOutput has been added to the <see cref="P:Microsoft.Data.SqlClient.SqlCommand.Parameters" /> collection. | ||
</exception> | ||
<exception cref="T:System.IO.IOException"> | ||
An error occurred in a | ||
<see cref="T:System.IO.Stream" /> | ||
|
@@ -702,8 +722,13 @@ To set up this example, create a new Windows application. Put a <xref:System.Win | |
<exception cref="T:System.InvalidOperationException"> | ||
The | ||
<see cref="T:Microsoft.Data.SqlClient.SqlConnection" /> | ||
closed or dropped during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support). | ||
</exception> | ||
closed or dropped during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support). | ||
|
||
- or - | ||
|
||
<see cref="P:Microssoft.Data.SqlClient.SqlCommand.EnableOptimizedParameterBinding" /> | ||
is set to true and a parameter with direction Output or InputOutput has been added to the <see cref="P:Microsoft.Data.SqlClient.SqlCommand.Parameters" /> collection. | ||
</exception> | ||
<exception cref="T:System.IO.IOException"> | ||
An error occurred in a | ||
<see cref="T:System.IO.Stream" /> | ||
|
@@ -831,8 +856,13 @@ This example passes the `CommandBehavior.CloseConnection` value in the `behavior | |
<exception cref="T:System.InvalidOperationException"> | ||
The | ||
<see cref="T:Microsoft.Data.SqlClient.SqlConnection" /> | ||
closed or dropped during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support). | ||
</exception> | ||
closed or dropped during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support). | ||
|
||
- or - | ||
|
||
<see cref="P:Microssoft.Data.SqlClient.SqlCommand.EnableOptimizedParameterBinding" /> | ||
is set to true and a parameter with direction Output or InputOutput has been added to the <see cref="P:Microsoft.Data.SqlClient.SqlCommand.Parameters" /> collection. | ||
</exception> | ||
<exception cref="T:System.IO.IOException"> | ||
An error occurred in a | ||
<see cref="T:System.IO.Stream" /> | ||
|
@@ -940,6 +970,11 @@ The following console application starts the process of retrieving XML data asyn | |
The | ||
<see cref="T:Microsoft.Data.SqlClient.SqlConnection" /> | ||
closed or dropped during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support). | ||
|
||
- or - | ||
|
||
<see cref="P:Microssoft.Data.SqlClient.SqlCommand.EnableOptimizedParameterBinding" /> | ||
is set to true and a parameter with direction Output or InputOutput has been added to the <see cref="P:Microsoft.Data.SqlClient.SqlCommand.Parameters" /> collection. | ||
</exception> | ||
<exception cref="T:System.IO.IOException"> | ||
An error occurred in a | ||
|
@@ -1067,8 +1102,13 @@ To set up this example, create a new Windows application. Put a <xref:System.Win | |
<exception cref="T:System.InvalidOperationException"> | ||
The | ||
<see cref="T:Microsoft.Data.SqlClient.SqlConnection" /> | ||
closed or dropped during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support). | ||
</exception> | ||
closed or dropped during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support). | ||
|
||
- or - | ||
|
||
<see cref="P:Microssoft.Data.SqlClient.SqlCommand.EnableOptimizedParameterBinding" /> | ||
is set to true and a parameter with direction Output or InputOutput has been added to the <see cref="P:Microsoft.Data.SqlClient.SqlCommand.Parameters" /> collection. | ||
</exception> | ||
<exception cref="T:System.IO.IOException"> | ||
An error occurred in a | ||
<see cref="T:System.IO.Stream" /> | ||
|
@@ -1343,6 +1383,33 @@ The <xref:Microsoft.Data.SqlClient.SqlCommand.CreateParameter%2A> method is a st | |
To be added. | ||
</remarks> | ||
</Dispose> | ||
<EnableOptimizedParameterBinding> | ||
<summary> | ||
Gets or sets a value indicating whether the command object should optimize parameter performance by disabling Output and InputOutput directions when submitting the command to the SQL Server. | ||
</summary> | ||
<value> | ||
A value indicating whether the command object should optimize parameter performance by disabling Output and InputOuput parameter directions when submitting the command to the SQL Server. | ||
The default is <see langword="false" />. | ||
</value> | ||
<remarks> | ||
<format type="text/markdown"> | ||
<![CDATA[ | ||
## Remarks | ||
You must set the value for this property before the command is executed for it to take effect. | ||
|
||
When a command is submitted to the server with parameters a list of the parameter names is sent as part of the submission. The list is used on the server to match Output and InputOutput parameters to the results of the query execution so that the values can be returned to the caller. This option disables the construction and submission of the parameter name list and as a consequence disables the use of Output and InputOutput parameters. The return parameter is not affected by this option. | ||
|
||
A command sent with this option changes the way parameters are handled on the server, because there is no need to maintain an output parameter map. The result of this change is that queries with large numbers of input parameters may execute much faster. | ||
|
||
The fewest number of parameters where this will take effect depends on the individual situation and should be detected by measuring query duration with and without the option enabled. Any query with more than 24 parameters may show lower overall query duration. Queries with parameter counts lower than 24 are unlikely to show a difference. | ||
|
||
> [!NOTE] | ||
If the option is enabled and a parameter with Direction Output or InputOutput is present in the Parameters collection an InvalidOperationException will be thrown when the command is executed. | ||
Comment on lines
+1406
to
+1407
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This property is going to optimize the command execution, I think it'd better to just skip it if it doesn't commit the requirements instead of raising an exception. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So just set the value to false? It's compatible but it will mean silent performance regression if someone adds an output parameter to a query that is expected to contain only input parameters. We'd essentially be silently disabling something that the user had to explicitly tell us that they wanted and I'm sure I'd be quite annoyed by that behaviour as a consumer having to track it down as a performance problem. |
||
|
||
]]> | ||
</format> | ||
</remarks> | ||
</EnableOptimizedParameterBinding> | ||
<EndExecuteNonQuery name="IAsyncResult"> | ||
<param name="asyncResult"> | ||
The | ||
|
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.
Could you explain in which conditions it would have a negative perf effect (for input parameters)-not neutral- if there was such a condition?
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.
There isn't a negative case. The only negative consequence is disabling output parameters. For small numbers of parameters the performance will be virtually identical it's only as the numbers rise that the difference becomes meaningful. Check the numbers in #1041 (comment)
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.
If there is a match point to decide internally, my suggestion is to have this property with multiple configurations:
OptimizedParameterBinding
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.
My concern with auto is that we make a change to the way the query is run without user interaction. This leads to the kind of situation where something with 30 parameters works fine but something with 31 works in some unexpected way and since the user can't see a difference in their code it is interpreted as random. The only way to see the problem at that point is probably to use wireshark which is normally a long long way down the list of debugging tools close to last resort because of the difficulty involved.
I view this as an advanced option. I don't believe that this is a casual option that would ever just be defaulted to true. If you opt in then we try to do what you asked and if we can't we throw an exception at you.
The decision making is all down to the user and by default we continue the behaviour that people have been using for the last 20 years so we don't accidentally cause some poor maintenance programmer a really really bad week (or more).