0

Does setting ANSI_NULLS ON/OFF affect the performance of stored procedures?

SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[TestProcedure]
3
  • I don't know about possible issues with ansi_nulls being off, but it is deprecated, so I wouldn't recommend using it --- and there's several features that work only when ansi_nulls is on
    – James Z
    Commented Oct 8, 2015 at 14:33
  • I faced a situation where the execution time of a stored procedure was 1 second when ANSI_NULLS was set OFF and its execution time increased to 1 minute when it was set ON. Is there any specific reason for this to occur?
    – shrirams
    Commented Oct 8, 2015 at 14:37
  • Yes, different SET settings can force different plans to be used, see Slow in the Application, Fast in SSMS?, as I suggested in my answer... Commented Oct 8, 2015 at 14:50

1 Answer 1

2

This does not affect the performance of stored procedures directly, but in general it can affect performance indirectly, since SET settings used at execution time can influence the plan that is chosen. Two different users, one with the setting on, and one with the setting off, will get their own copies of the plans, and they may not even be the same plan (depending on other variables at runtime, like parameter values). See Erland Sommarskog's post, Slow in the Application, Fast in SSMS? On top of differently performing queries, this can lead to potentially different performance and will create two different plans in the cache, wasting memory.

Eventually, you won't even have the ability to set ANSI_NULLS to OFF; see the documentation:

In a future version of SQL Server, ANSI_NULLS will always be ON and any applications that explicitly set the option to OFF will generate an error. Avoid using this feature in new development work, and plan to modify applications that currently use this feature.

So, you should already be in the habit of always setting it to ON. If it's just always on, then it won't be an issue.

1
  • According to this the setting is "saved" with the stored procedure and the saved value is used when SP is called. Most other settings are not saved in which case your answer applies. Commented Feb 16, 2018 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.