Presentation is loading. Please wait.

Presentation is loading. Please wait.

EXEC and sp_executesql An Ad Hoc Rally

Similar presentations


Presentation on theme: "EXEC and sp_executesql An Ad Hoc Rally"— Presentation transcript:

1 EXEC and sp_executesql An Ad Hoc Rally
SQL Saturday #474 - Salt Lake City Jay Robinson

2 Thanks to our Sponsors! Yearly Partners Gold Sponsors

3 The Four Stages of Our Rally
Parameters and Plan Cache Bloat Data Distribution and Bad Plans Simple and Forced Parameterization Optimize for Ad Hoc Workloads

4 The Obligatory Bio Page
Jay Robinson Senior Systems Engineer, Salesforce Marketing Cloud (formerly ExactTarget) Supporting hundreds of instances of SQL Server across multiple data centers Blog: downshiftdata.wordpress.com

5 Why A Rally?

6 Pre-Race: True or False?
SQL Server caches the query plan when using EXEC. TRUE but… It might just cache a stub. EXEC allows you to parameterize the statement. FALSE It might do it for you. sp_executesql is slower than EXEC. Not by much. sp_executesql has its own transaction isolation level. FALSE and… Therefore, neither does EXEC. Calling EXEC WITH RECOMPILE will purge the old query plan out of the cache and replace it. It gives the current EXEC a once-only query plan, then purges that afterwards. MSDN: “We recommend that you use this option sparingly because it is expensive.”

7 Stage 1: Parameters and Plan Cache Bloat
DEMO

8 Stage 1: Parameters and Plan Cache Bloat
EXEC does not allow for parameterization. Vulnerable to SQL Injection May contribute to plan cache bloat Using sp_executesql addresses both of these issues. However, EXEC is slightly faster. Stage 1 Winner: sp_executesql (usually)

9 Stage 2: Data Distribution and Bad Plans
DEMO

10 Stage 2: Data Distribution and Bad Plans
When do you de-parameterize to manipulate the Query Plan Cache? Rule #1 – Don’t open yourself up to SQL injection! Rule #2 – Understand the distribution of your data and how you are querying it. Stage 2 Winner: The data-aware DBA

11 Stage 3: Simple and Forced Parameterization
DEMO

12 Stage 3: Simple and Forced Parameterization
By default, SQL Server uses SIMPLE PARAMETERIZATION. Parameterizes only the simplest of queries. Produces different plans based on data type. The alternative is FORCED PARAMETERIZATION. Parameterizes most queries. Some exceptions: INSERT...EXECUTE, XQuery, cursors, deprecated constructs. Stage 3 Winner: It Depends!

13 Stage 4: Optimize for Ad Hoc Workloads
DEMO

14 Stage 4: Optimize for Ad Hoc Workloads
This is NOT enabled by default, but should be. On the first execution of an ad hoc query, SQL Server will cache only a stub. On the second execution, SQL Server will replace this with the full query plan. Stage 4 Winner: Optimize for Ad Hoc and EXEC

15 Thank You! "Speed matters, and the longer your foot is on the brake the less it is on the accelerator." - Pat Moss and Erik Carlsson, The Art and Technique of Driving Don’t Forget The Survey! Jay Robinson Blog: downshiftdata.wordpress.com


Download ppt "EXEC and sp_executesql An Ad Hoc Rally"

Similar presentations


Ads by Google