SA0089 : The option has a not recommended value SET which will cause the stored procedure to be recompiled
Introduction
Section titled “Introduction”Avoid using SET statements to configure session options within stored procedures to prevent potential performance issues.
Description
Section titled “Description”When developing stored procedures, certain session settings are crucial for consistent behavior and performance. If these settings are altered via SET statements within the procedure, it can cause unnecessary recompilations, impacting performance negatively.
For example:
-- Example of a problematic SET statement in a stored procedureCREATE PROCEDURE ExampleProcedureASBEGIN SET NUMERIC_ROUNDABORT ON; -- Not recommended SELECT * FROM ExampleTable;END;This example is problematic because changing NUMERIC_ROUNDABORT to ON inside a stored procedure is not recommended. Altering this setting can lead to recompilation every time the procedure is executed, which slows down execution.
-
Recompilation occurs each time the stored procedure is run, increasing CPU usage and execution time.
-
Inconsistent query results might occur if session settings differ from expected defaults, complicating troubleshooting.
How to fix
Section titled “How to fix”This section provides guidance on how to modify stored procedures to avoid using inappropriate SET statements, which can lead to performance issues due to unnecessary recompilations.
Follow these steps to address the issue:
1.Identify stored procedures using SET statements that alter session configurations, such as SET NUMERIC_ROUNDABORT ON .
2.Remove or replace the identified SET statements within the stored procedures to ensure the session settings align with recommended defaults outside of the procedure.
3.Refactor the code to rely on database or application-level configurations for session settings as needed, ensuring consistency and avoiding recompilations.
For example:
-- Example of corrected stored procedure without SET statementCREATE PROCEDURE ExampleProcedureASBEGIN SELECT * FROM ExampleTable; -- No alteration of session settingsEND;The rule has a Batch scope and is applied only on the SQL script.
Parameters
Section titled “Parameters”Rule has no parameters.
Remarks
Section titled “Remarks”The rule does not need Analysis Context or SQL Connection.
Effort To Fix
Section titled “Effort To Fix”1 hour per issue.
Categories
Section titled “Categories”Maintenance Rules, Bugs
Additional Information
Section titled “Additional Information”Troubleshooting stored procedure recompilation
Example Test SQL
Section titled “Example Test SQL”CREATE PROCEDURE test_recompile AS
SET QUOTED_IDENTIFIER OFFSET ANSI_NULLS OFFSET ARITHABORT OFFSET ANSI_NULL_DFLT_ON OFFSET ANSI_DEFAULTS OFFSET ANSI_WARNINGS OFFSET ANSI_PADDING OFFSET CONCAT_NULL_YIELDS_NULL OFFSET NUMERIC_ROUNDABORT ONSET NOCOUNT ONSET ROWCOUNT 100SET XACT_ABORT ONSET IMPLICIT_TRANSACTIONS ONSET ARITHIGNORE OFFSET LOCK_TIMEOUT 1SET FMTONLY ONSET NOEXEC ONSET PARSEONLY OFF
SELECT au_lname, au_fname, au_id from authorsWHERE au_lname like 'L%'Analysis Results
Section titled “Analysis Results”| Message | Line | Column | |
|---|---|---|---|
| 1 | SA0089 : The SET ARITHABORT option to OFF will cause the stored procedure to be recompiled everytime it is execued. | 5 | 4 |
| 2 | SA0089 : The SET ANSI_NULL_DFLT_ON option to OFF will cause the stored procedure to be recompiled everytime it is execued. | 6 | 4 |
| 3 | SA0089 : The SET ANSI_DEFAULTS option to OFF will cause the stored procedure to be recompiled everytime it is execued. | 7 | 4 |
| 4 | SA0089 : The SET ANSI_WARNINGS option to OFF will cause the stored procedure to be recompiled everytime it is execued. | 8 | 4 |
| 5 | SA0089 : The SET ANSI_PADDING option to OFF will cause the stored procedure to be recompiled everytime it is execued. | 9 | 4 |
| 6 | SA0089 : The SET CONCAT_NULL_YIELDS_NULL option to OFF will cause the stored procedure to be recompiled everytime it is execued. | 10 | 4 |