The Hidden Power Of CFQUERYPARAM!
Everyone knows that you should be using CFQUERYPARAM. It offers many benefits in the areas of security, data validation, and enhanced performance for your applications. In fact, Adobe recommends that you use the CFQUERYPARAM tag within every CFQUERY, and I completely agree with this recommendation. I think you would be very hard pressed to find a valid reason NOT to use it.
Many of us know the benefit of CFQUERYPARAM from the application side, though we may not always be aware of how it can also help out the database side. In this excerpt from my session at CF Summit 2018, Faster Queries In ColdFusion & SQL Server, we will take a look at what goes on behind the scenes in SQL Server when processing queries with, and without, CFQUERYPARAM.
What happens when SQL Server processes a query?
Whenever you run a query in SQL Server, it will generate an Execution Plan which basically tells SQL Server how to gather and filter the data your query is requesting. Once an Execution Plan has been created, SQL Server stores it in the Plan Cache, which is a section of memory set aside for keeping Execution Plans. The reason for this is so that the next time that query is executed, SQL Server will be able to reuse the previously generated Execution Plan, and not have to go through the overhead of generating it again. So, for example, if we were to run the following query and then check the Plan Cache, we would see where SQL Server has generated an Execution Plan for it.
Notice how the variable that we passed to the query is actually a hard coded value when SQL Server runs the query. Now, if we were to change the value of the variable we are using in our query, then check the Plan Cache, we would see that SQL Server has generated a separate Execution Plan for this query, which would have the new variable value hard coded. In fact, if we were to loop through multiple values for our variable, SQL Server would generate an Execution Plan for each version of our query:
Why is this a problem?
When the variable in our query is rendered by ColdFusion and the query is processed, SQL Server sees each version of the query as a completely different query and therefore has to create a new Execution Plan for it. Having an Execution Plan generated for every version of this query is expensive, in both processing overhead and resource utilization. If you run that query 1,000 times per day with a different ID each time, you have 1,000 copies of that same execution plan sitting in RAM on your SQL Server. Plus, the processing overhead of actually generating an Execution Plan is expensive, that’s why SQL Server wants to cache it so that it doesn’t have to do it every time. Repeatedly generating an Execution Plan can lead to higher CPU usage, and slower queries.
How does CFQUERYPARAM help?
CFQUERYPARAM uses bind variables when sending a query to SQL Server. A bind variable will allow SQL Server to use parameters for the variables you pass in via CFQUERYPARAM, which will allow it to generate and reuse a single execution plan for the query. Execution Plan reuse is a big deal in SQL Server! If we take a look at our last example and add CFQUERYPARAM, we can see how SQL Server now creates a single parameterized Execution Plan, and reuses it for each query.
By reusing the same Execution Plan for each of these queries SQL Server is able to process the query faster, while also providing more efficient resource utilization by not having a lot of individual “one-off” Execution Plans sitting in memory. This can have a big impact on the performance of your SQL Server, especially when you consider that this applies to every query your application runs. Yes, even your UPDATE and DELETE queries!
Use CFQUERYPARAM Everywhere!
Is this overkill? Maybe for some, but as I like to say in my presentations, scalability doesn’t happen by accident, you have to plan for it and code for it. So always remember to use CFQUERYPARAM as much as possible, your SQL Server will thank you for it!