Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 December SQL Server: Increase Query Performance using a Forced Parameterization

SQL Server: Increase Query Performance using a Forced Parameterization

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

Since SQL Server 2005, we have a concept of Forced Parameterization of SQL Query.

It looks like a very old concept, but the use of Forced Parameterization improves the performance of Query Optimizer which responsible for the best execution plan.

SQL Server provides two types of query parameterization, one is Simple Parameterization and second is Forced Parameterization.

Why it is important and How it works?

When you are executing any query, SQL Server Query Optimizer analyzes your query and prepares the best execution plan for it.

If you are executing the same query again, previously generated execution plan may use for the same query. We can also say that It caches your query execution plan.

It is doing this using a Simple Parameterization which is default in SQL Server.

Let me add one more thing,
Your frequent SQL Query uses the cache query plan, but what If you change the value in WHERE clause.

If you are changing just column filter value in WHERE clause, It uses the cache query plan and If you are modifying your parameter list, It requires to generate a new query plan.

This is about the Simple Parameterization.

You can see in the below image:

I have executed two different queries with passing different values for TrackingEventID.

But You can see TrackingEventID = @1 in query execution plan, means It uses same query plan for both the query which increase your query performance.

sql server simple parameterization

Now check below image:

I have used ISNULL() in query and It skipped to put parameter in the query of execution plan.
You can see static date value instead of parameter values.

If you are executing this kind of queries frequently, It prepares and generates an execution plan every time which decreases the query performance.

sql server simple parameterization without parameter

Now, enable the Force Parameterization and check the same query.

You can check below image where you can see parameter values instead of static date value.

Once you enable Force Parameterization, It forcefully adds the parameter in the query of execution plan.

Which improves the query performance by reducing the cost of preparing and compiling an execution plan again and again.

But we should also take care about this because It might generate SQL Parameter Sniffing Issue so we should keep database statistics up to date.

sql server force parameterization

Script to change the database in Forced Parameterization:

1
2
3
ALTER DATABASE Database_Name
SET PARAMETERIZATION FORCED
GO

Script to change the database in Simple Parameterization:

1
2
3
ALTER DATABASE Database_Name
SET PARAMETERIZATION Simple
GO

Other related articles:

SQL Server: The Importance of Statistics and Why It is important

SQL Server Parameter Sniffing

Dec 27, 2016Anvesh Patel
Database Design: Common Practices for Database Developers and Application DevelopersSQL Server: The list of Important Trace Flags
Anvesh Patel
Anvesh Patel

Database Engineer

December 27, 2016 SQL ServerAnvesh Patel, database, database research and development, dbrnd, Execution Plan, Forced Parameterization, OUT Parameters, Parameter, parse query plan, query performance, simple parameterization, SQL Query, SQL Server, SQL Server Administrator, SQL Server Monitoring, SQL Server Performance Tunning, SQL Server Tips and Tricks, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....