Maciej Pilecki Consultant Project Botticelli Ltd. DAT404.

Slides:



Advertisements
Similar presentations
© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Advertisements

© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
© 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
MIX 09 4/15/ :14 PM © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Feature: Payroll and HR Enhancements © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or.
Windows 7 Training Microsoft Confidential. Windows ® 7 Compatibility Version Checking.
Tess Ferrandez ASP.NET Escalation Engineer Microsoft Session Code: WIA402.
 Pablo Castro Software Architect Microsoft Corporation TL08.
Session 1.
Built by Developers for Developers…. © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names.
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
Feature: Assign an Item to Multiple Sites © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names.
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
Maciej Pilecki Consultant, SQL Server MVP Project Botticelli Ltd. SESSION CODE: DAT403.
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
Feature: Customer Combiner and Modifier © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are.
© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or.

customer.
demo © 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names.
Eric Carter Development Manager Microsoft Corporation OFC324.
demo Demo.
Connect with life Nauzad Kapadia Quartz Systems
demo QueryForeign KeyInstance /sm:body()/x:Order/x:Delivery/y:TrackingId1Z
Feature: Suggested Item Enhancements – Analysis and Assignment © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and.
projekt202 © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are.
Ramesh Meyyappan SQL Server Performance Tuning Consultant & Trainer SQLWorkshops.comSQLWorkshops.com / SQLIO.comSQLIO.com.
© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks.
Gail Shaw XpertEase DAT 305 Topics Background Information Query Hints Plan Cache Metadata Plan Guides Plan Freezing Monitoring Plan Guide Use.
Pinal Dave Mentor | Solid Quality India |
© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or.
Gavin Russell-Rockliff BI Technical Specialist Microsoft BIN202.
Sara Ford Program Manager Microsoft Corporation DPR301.
Warren Stevens-Baytopp Director YoungBlood Consultants Session Code BIN303.

Maciej Pilecki Project Botticelli Ltd. Session Code: DAT402.
Ben Robb MVP, SharePoint Server cScape Ltd OFC204.
Luke Hoban Senior Program Manager Microsoft Session Code: DTL319.
Mark Michaelis Chief Computer Nerd IDesign/Itron/IntelliTechture DTL313.
Scott Morrison Program Manager Microsoft Corporation Session Code: WUX308.
A deep dive into SQL Server Plan Cache Management.
6/26/2018 9:02 PM © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Tech·Ed North America /31/2018 4:35 PM
9/11/2018 5:53 PM © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Optimizing Microsoft SQL Server 2008 Applications Using Table Valued Parameters, XML, and MERGE
Peeking into the Plan Cache with SQL Server 2008
Sysinternals Tutorials
Jason Zander Unplugged
Title of Presentation 12/2/2018 3:48 PM
Brian Keller Sr. Technical Evangelist Microsoft Session Code: DEV310
12/5/2018 3:24 PM © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Ben Robb MVP, SharePoint Server cScape Ltd Session Code: OFS207
Data Driven ASP.NET Web Forms Applications Deep Dive
Tech·Ed North America /17/2019 1:47 AM
Building Silverlight Apps with RIA Services
Building SaaS Solutions on Windows Azure
8/04/2019 9:13 PM © 2006 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Виктор Хаджийски Катедра “Металургия на желязото и металолеене”
TechEd /28/2019 7:58 AM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
Hack-proofing your Clients using Windows 7 Security!
Шитманов Дархан Қаражанұлы Тарих пәнінің
Lap Around the Windows Azure Platform
Building BI applications using PowerPivot for Excel
Title of Presentation 5/24/2019 1:26 PM
Tech·Ed North America /7/2019 2:30 PM
What’s New in Visual Studio 2012 for Web Developers
Presentation transcript:

Maciej Pilecki Consultant Project Botticelli Ltd. DAT404

SELECT About FROM Speakers WHERE Name = ’Maciej Pilecki’ Over 10 years of SQL Server experience SQL Server MVP since Jan 2006 Specializing in SQL Server database development, administration, performance tuning and troubleshooting Delivering consulting services around the world (special discounted rate for TechEd attendees) Frequent speaker at many international conferences Living in Wroclaw, Poland

Agenda Query optimization and execution Plan caching and reuse What is the Procedure Cache? Managing the Procedure Cache Controlling plan reuse

Query Execution You type your query in SSMS, hit F5 and get the results......well, it is more complicated than that...

Query Execution Every query goes through a numer of steps: Parsing Algebraization Optimization Execution

Query Optimization Process of selecting one execution plan from many possible plans Cost-based – aimed at finding execution plan with the lowest (or close to lowest) estimated execution cost Source for graphics: SQL Server BOL

Can optimization go wrong? Optimizer ESTIMATES the cost for each potential plan That estimate can be wrong (lack of stats, outdated stats, query structure etc.) A WRONG (sub-optimal) plan can be selected (but it seems best) You can outsmart the optimizer with hints! (if you have to)

Query Optimization Query Optimization is good - improves query performance But: It is expensive (memory, CPU, time) It is throttled at the server level (see Optimization section in KB ) It can timeout We need plan caching...

Plan caching Attempt to save the optimization effort by caching and reusing execution plans Execution plan consists of: Query plan (one for many users, read-only) Execution context (per-user but cached and reused as well) Plan cache is also called „Procedure Cache”

Query Execution Phases Query Cache lookup Compile query Schema Statistics Check plan Plan valid? Execute query Found Not found Yes No

How cache lookup works Finding an existing plan is based on: ID of the object (for SPs, triggers, functions etc.) Hash of the query text (for ad-hoc queries) Also, the cache-key attributes must match: SET options DBID Language and date settings user_id for non-qualified object names And other...

Inside the Procedure Cache Stores compiled execution plans There is not one ProcCache but 4: CACHESTORE_OBJCP – object plans CACHESTORE_SQLCP – SQL plans CACHESTORE_PHDR – „bound trees” CACHESTORE_XPROC – XProcs ProcCache „steals” pages from the Buffer Pool Entries aged-out based on cost of compilation and usage frequency

Inside the Procedure Cache - DMVs sys.dm_exec_cached_plans sys.dm_exec_sql_text(plan_handle) sys.dm_exec_query_plan(plan_handle) sys.dm_exec_plan_attributes(plan_handle) sys.dm_exec_query_stats You can combine and aggregate those in a number of interesting ways...

Procedure Cache Size Procedure cache size limits SQL 2000: max. 4GB SQL 2005 RTM and SP1: 75% 0-8GB + 50% 8-64GB + 25% >64GB SQL 2005 SP2 and SQL 2008: 75% 0-4GB + 10% 4-64GB + 5% >64GB Example: with 16GB on RTM limit is 10GB, on SP2: 4.2 GB Or number of items: Up to per cache store (SQLCP, OBJCP) on 32-bit Up to per cache store on 64-bit No direct control over the size of cache Workload Governor helps, if you can use it (EE only)

How is Procedure Cache Cleared? Automatically: Entries aged-out due to memory pressure Due to certain database operations: Database restore, changing DB state, etc. See KB for SQL Server 2005 list (or Logs to ERRORLOG: "SQL Server has encountered %d occurrence(s) of cachestore flush for the '%s' cachestore (part of plan cache) due to some database maintenance or reconfigure operations". Fixed in SQL 2008 (in most cases clears plans for one DB only)

How is Procedure Cache Cleared? Manually: DBCC FREEPROCCACHE Clears everything – everything needs to be recompiled Usually an overkill – serious performance effects In SQL 2008 you can be more selective: DBCC FREEPROCCACHE ( { plan_handle | sql_handle | pool_name } ) plan_handle – single plan sql_handle – all plans for particular SQL text pool_name – Resorce Governor pool

How is Procedure Cache Cleared? Manually (cont’d): DBCC FLUSHPROCINDB(dbid) Clears all plans for a single database Better but still an overkill Undocumented and unsupported DBCC FREESYSTEMCACHE('SQL Plans') Clears a particular cache store In this case, the ad-hoc plans cache store Keeps other plans intact Undocumented and unsupported

Managing plan reuse Generally, plan reuse is a good thing Less plans in cache (less memory consumed) Less time spend optimizing and compiling plans Our goal is to improve plan reuse as much as possible (but beware of drawbacks!) Depending on application, can be very easy or very hard to achieve

Controlling plan caching Application-side parameterization Stored Procedures Forced parameterization Database-level option More aggressive in parameterizing ad-hoc SQL Optimize for ad hoc workloads New server option in SQL Server 2008 Only a stub is cached on first execution Full plan cached after second execution

When is plan reuse NOT a good thing? The main downside of plan caching: Cached plan is based on the parameters used during optimization Known as „parameter sniffing” Not always the best plan for subsequent executions with a different parameter values Sometimes difficult to spot – look for queries with greatly varying execution stats Estimated number of rows << Actual number of rows

Working around the „bad plan” issue Recompiling for given execution: OPTION (RECOMPILE) EXECUTE... WITH RECOMPILE Always recompiling (for SPs) Masking parameter values through local variables Specifying value for the optimizer: OPTIMIZE FOR = val) OPTIMIZE FOR UNKNOWN (new in 2008)

Summary Query optimization and execution Plan caching and reuse What is the Procedure Cache? Managing the Procedure Cache Controlling plan reuse Additional reading:

Sessions On-Demand & Community Resources for IT Professionals Resources for Developers Microsoft Certification & Training Resources Resources Required Slide Speakers, TechEd 2009 is not producing a DVD. Please announce that attendees can access session recordings at TechEd Online. Required Slide Speakers, TechEd 2009 is not producing a DVD. Please announce that attendees can access session recordings at TechEd Online.

Complete an evaluation on CommNet and enter to win an Xbox 360 Elite!

© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION. Required Slide