Does Sql Server cached compiled queries and execution plan across transactions?
-
11-10-2019 - |
Question
Folks,
I'm using the best-practice of prepared sql statements to execute many Inserts/Updates that vary by the same parameters. I have two choices in my design: 1. all of the work gets done in a single transaction. 2. break-up the work into a number of transactions (not one per statement, but something that suits the concurrency of my environment). If I opt for #2, will SQL take advantage of the cached compiled query/execution plan across transactions? Or, because the query was made within a transaction, the life of the cache will be limited to the transaction?
Solution
Plans are unrelated to transactions. Or connections for that matter
That is, a plan can be shared by many txns and/or users and/or connections. And at different times if the plan is valid and still in cache
OTHER TIPS
The query cache is independent of transactions, so your queries will get cached regardless which option you choose.