With this post, I just wanted to share something I have stumbled onto when using Azure Advisor service and related to the optimization of a cloud based SQL Server database. However, it applies to any version of Microsoft SQL Server, either cloud or on-premise.
It all started when I randomly browsed the Azure portal and checked the Azure Advisor service : there I saw a recommendation marked as having a high impact. My first reaction was “wow, how can I have missed that ?!”.
The performance recommendations detail screen showed it was about non-parameterized queries.
So the recommendation is quite self-explanatory and is to use forced parameterization on the database, but let’s refresh our memories about what we are talking about here.
Each time the database receives a new T-SQL query, SQL Server engine works with various processes to send back the query result. Let’s focus on the main 2 steps which are “Relational engine processes” and “Storage engine processes”.
Relational engine processes are in charge of:
Using parameters in T-SQL queries can increase the reusability of the calculated execution plans, thus reducing the induced cost of calculation.
A SQL server database can be configured with “Simple parameterization” or “Forced parameterization”. Depending on this configuration, SQL handles incoming queries in a different way. The default setting is Simple parameterization.
If you try to execute a statement without parameters on your database, the relational engine will try to parameterize the query: it means that SQL Server will substitute any literal value inside the query with parameters. For instance, when sent to the database engine, the following query is parameterized as follows :
The intended goal is to maximize the chance of the resulting query to match one of the existing execution plans. However, the relational engine may have difficulty determining which expressions can be parameterized and therefore only a subset of queries can be parameterized with Simple Parameterization. You can’t choose which query will be and which won’t be. So, if you have complex queries, you should parameterize them before executing them against the database engine.
Refer to this link for further inputs : https://docs.microsoft.com/en-us/previous-versions/sql/sql-server-2008-r2/ms186219%28v%3dsql.105%29
By specifying forced parameterization, and under certain limitations, the engine will parameterize all the literals within the SELECT, INSERT, UPDATE and DELETE statements. The engine will have a higher chance of reusing execution plans under the forced parameterization context.
To get the limitations and more details, check this post about forced parameterization
https://docs.microsoft.com/en-us/previous-versions/sql/sql-server-2008-r2/ms175037(v=sql.105)
Please note that depending on your data and/or your queries, it might not be beneficial to setup Forced parameterization. If you profile all your queries, you may notice performance degradations due to forced parameterization on some queries. In this case, you can still define exceptions by using specific plan guides. Read more about plan guides with the following post : https://docs.microsoft.com/en-us/previous-versions/sql/sql-server-2008-r2/ms191275%28v%3dsql.105%29