Bring	
  your	
  SQL	
  Server	
  installa3ons	
  to	
  a	
  new	
  level	
  of	
  excellence!	
   www.SQLpassion.at	
  
Bring	
  your	
  SQL	
  Server	
  installa3ons	
  to	
  a	
  new	
  level	
  of	
  excellence!	
   www.SQLpassion.at	
  
Cardinality Estimation in SQL Server 2014
Klaus Aschenbrenner
Microsoft Certified Master SQL Server 2008
www.SQLpassion.at
Twitter: @Aschenbrenner
Bring	
  your	
  SQL	
  Server	
  installa3ons	
  to	
  a	
  new	
  level	
  of	
  excellence!	
   www.SQLpassion.at	
  
About me
• CEO & Founder SQLpassion
• International Speaker, Blogger, Author
• SQL Server 2008 MCM
• "Pro SQL Server 2008 Service Broker"
• Twitter: @Aschenbrenner
• SQLpassion Academy
– http://www.SQLpassion.at/academy
– Free Newsletter, Training Videos
Bring	
  your	
  SQL	
  Server	
  installa3ons	
  to	
  a	
  new	
  level	
  of	
  excellence!	
   www.SQLpassion.at	
  
Agenda
• Introduction to CE
• CE in SQL Server 2014
Bring	
  your	
  SQL	
  Server	
  installa3ons	
  to	
  a	
  new	
  level	
  of	
  excellence!	
   www.SQLpassion.at	
  
Agenda
• Introduction to CE
• CE in SQL Server 2014
Bring	
  your	
  SQL	
  Server	
  installa3ons	
  to	
  a	
  new	
  level	
  of	
  excellence!	
   www.SQLpassion.at	
  
What is Cardinality Estimation?
Bring	
  your	
  SQL	
  Server	
  installa3ons	
  to	
  a	
  new	
  level	
  of	
  excellence!	
   www.SQLpassion.at	
  
Predicates
• Filter Predicates
– WHERE
– HAVING
• Join Predicates
– JOIN Condition
Bring	
  your	
  SQL	
  Server	
  installa3ons	
  to	
  a	
  new	
  level	
  of	
  excellence!	
   www.SQLpassion.at	
  
Selectivity
• Defines how many rows are returned
• A number between 0.0 and 1.0
– 0.0: High Selectivity (no records)
– 1.0: Low Selectivity (all records)
Bring	
  your	
  SQL	
  Server	
  installa3ons	
  to	
  a	
  new	
  level	
  of	
  excellence!	
   www.SQLpassion.at	
  
Questions answered by CE
• Selectivity of
– Single Filter Predicates
– Multiple Filter Predicates
– Join Predicate between 2 tables
• How many distinct values do we expect
– GROUP BY
– DISTINCT
Bring	
  your	
  SQL	
  Server	
  installa3ons	
  to	
  a	
  new	
  level	
  of	
  excellence!	
   www.SQLpassion.at	
  
Agenda
• Introduction to CE
• CE in SQL Server 2014
Bring	
  your	
  SQL	
  Server	
  installa3ons	
  to	
  a	
  new	
  level	
  of	
  excellence!	
   www.SQLpassion.at	
  
CE in SQL Server 2014
• No changes since SQL Server 7.0
– Changes were controlled through Trace Flags
– No Plan-Quality Regressions!
• CE in SQL Server 2014 is the 1st major change
– Should improve workloads
– But you WILL see regressions!
Bring	
  your	
  SQL	
  Server	
  installa3ons	
  to	
  a	
  new	
  level	
  of	
  excellence!	
   www.SQLpassion.at	
  
Enabling the new CE
• Database Compatibility Level 120
• Trace Flags
– 2312: uses the new CE
– 9481: uses the old CE
– QUERYTRACEON at the statement level
Bring	
  your	
  SQL	
  Server	
  installa3ons	
  to	
  a	
  new	
  level	
  of	
  excellence!	
   www.SQLpassion.at	
  
Demo
Enabling the new CE
Bring	
  your	
  SQL	
  Server	
  installa3ons	
  to	
  a	
  new	
  level	
  of	
  excellence!	
   www.SQLpassion.at	
  
CE Model Assumptions
• Independence
– Filter Predicates are not correlated
• Uniformity
– Values in a Histogram step are evenly distributed
• Inclusion
– Comparing to a constant yields in a match
• Containment
– If something is being searched for, it is assumed that it exists
Bring	
  your	
  SQL	
  Server	
  installa3ons	
  to	
  a	
  new	
  level	
  of	
  excellence!	
   www.SQLpassion.at	
  
Improvements
• Multi-Column Predicates
– Conjunctions (AND)
– Disjunctions (OR)
• Ascending Key Column Problems
– Out of Range Estimations
• Join Algorithm Changes
• Troubleshooting through Extended Events
Bring	
  your	
  SQL	
  Server	
  installa3ons	
  to	
  a	
  new	
  level	
  of	
  excellence!	
   www.SQLpassion.at	
  
Multi-Column Predicates - AND
• Conjunctions – old behavior
– Independence Assumption
– Selectivity of conjunctive predicates are multiplied
– Doesn’t reflect reality
Bring	
  your	
  SQL	
  Server	
  installa3ons	
  to	
  a	
  new	
  level	
  of	
  excellence!	
   www.SQLpassion.at	
  
Multi-Column Predicates - AND
• Conjunctions – new behavior
– Predicates are sorted by their selectivity
– 4 most selective predicates are kept
– Predicates are softened by taking square roots
– Exponential back-off Algorithm
Bring	
  your	
  SQL	
  Server	
  installa3ons	
  to	
  a	
  new	
  level	
  of	
  excellence!	
   www.SQLpassion.at	
  
Demo
Exploring Conjunctions
Bring	
  your	
  SQL	
  Server	
  installa3ons	
  to	
  a	
  new	
  level	
  of	
  excellence!	
   www.SQLpassion.at	
  
Multi-Column Predicates - OR
• Disjunctions – old behavior
– Independence Assumption
– Individual selectivities are added, and the estimate of the conjunction
is subtracted
– Doesn’t reflect reality
Bring	
  your	
  SQL	
  Server	
  installa3ons	
  to	
  a	
  new	
  level	
  of	
  excellence!	
   www.SQLpassion.at	
  
Multi-Column Predicates - OR
• Disjunctions – new behavior
– Independence Assumption
– Disjunctions are converted to Conjunctions
• “De Morgan’s Law”: http://en.wikipedia.org/wiki/De_Morgan's_laws
– Same Exponential back-off Algorithm is applied to the Conjunction
Bring	
  your	
  SQL	
  Server	
  installa3ons	
  to	
  a	
  new	
  level	
  of	
  excellence!	
   www.SQLpassion.at	
  
Demo
Exploring Disjunctions
Bring	
  your	
  SQL	
  Server	
  installa3ons	
  to	
  a	
  new	
  level	
  of	
  excellence!	
   www.SQLpassion.at	
  
Summary
• Introduction to CE
• CE in SQL Server 2014
Bring	
  your	
  SQL	
  Server	
  installa3ons	
  to	
  a	
  new	
  level	
  of	
  excellence!	
   www.SQLpassion.at	
  
SQL Server Query Tuning Workshop
• 20. – 23. April 2015 in Zürich
• Inhalte
– Query Processing
– Indexing & Statistics
– Querying multiple Tables
– Temporal Data & Aggregations
– Windowing Functions
– In-Memory Technologies
• Weitere Informationen
– http://www.SQLpassion.at/academy/query-tuning-workshop
– 10% Rabatt für User Group Mitglieder!

Änderungen im Cardinality Estimator SQL Server 2014

  • 1.
    Bring  your  SQL  Server  installa3ons  to  a  new  level  of  excellence!   www.SQLpassion.at   Bring  your  SQL  Server  installa3ons  to  a  new  level  of  excellence!   www.SQLpassion.at   Cardinality Estimation in SQL Server 2014 Klaus Aschenbrenner Microsoft Certified Master SQL Server 2008 www.SQLpassion.at Twitter: @Aschenbrenner
  • 2.
    Bring  your  SQL  Server  installa3ons  to  a  new  level  of  excellence!   www.SQLpassion.at   About me • CEO & Founder SQLpassion • International Speaker, Blogger, Author • SQL Server 2008 MCM • "Pro SQL Server 2008 Service Broker" • Twitter: @Aschenbrenner • SQLpassion Academy – http://www.SQLpassion.at/academy – Free Newsletter, Training Videos
  • 3.
    Bring  your  SQL  Server  installa3ons  to  a  new  level  of  excellence!   www.SQLpassion.at   Agenda • Introduction to CE • CE in SQL Server 2014
  • 4.
    Bring  your  SQL  Server  installa3ons  to  a  new  level  of  excellence!   www.SQLpassion.at   Agenda • Introduction to CE • CE in SQL Server 2014
  • 5.
    Bring  your  SQL  Server  installa3ons  to  a  new  level  of  excellence!   www.SQLpassion.at   What is Cardinality Estimation?
  • 6.
    Bring  your  SQL  Server  installa3ons  to  a  new  level  of  excellence!   www.SQLpassion.at   Predicates • Filter Predicates – WHERE – HAVING • Join Predicates – JOIN Condition
  • 7.
    Bring  your  SQL  Server  installa3ons  to  a  new  level  of  excellence!   www.SQLpassion.at   Selectivity • Defines how many rows are returned • A number between 0.0 and 1.0 – 0.0: High Selectivity (no records) – 1.0: Low Selectivity (all records)
  • 8.
    Bring  your  SQL  Server  installa3ons  to  a  new  level  of  excellence!   www.SQLpassion.at   Questions answered by CE • Selectivity of – Single Filter Predicates – Multiple Filter Predicates – Join Predicate between 2 tables • How many distinct values do we expect – GROUP BY – DISTINCT
  • 9.
    Bring  your  SQL  Server  installa3ons  to  a  new  level  of  excellence!   www.SQLpassion.at   Agenda • Introduction to CE • CE in SQL Server 2014
  • 10.
    Bring  your  SQL  Server  installa3ons  to  a  new  level  of  excellence!   www.SQLpassion.at   CE in SQL Server 2014 • No changes since SQL Server 7.0 – Changes were controlled through Trace Flags – No Plan-Quality Regressions! • CE in SQL Server 2014 is the 1st major change – Should improve workloads – But you WILL see regressions!
  • 11.
    Bring  your  SQL  Server  installa3ons  to  a  new  level  of  excellence!   www.SQLpassion.at   Enabling the new CE • Database Compatibility Level 120 • Trace Flags – 2312: uses the new CE – 9481: uses the old CE – QUERYTRACEON at the statement level
  • 12.
    Bring  your  SQL  Server  installa3ons  to  a  new  level  of  excellence!   www.SQLpassion.at   Demo Enabling the new CE
  • 13.
    Bring  your  SQL  Server  installa3ons  to  a  new  level  of  excellence!   www.SQLpassion.at   CE Model Assumptions • Independence – Filter Predicates are not correlated • Uniformity – Values in a Histogram step are evenly distributed • Inclusion – Comparing to a constant yields in a match • Containment – If something is being searched for, it is assumed that it exists
  • 14.
    Bring  your  SQL  Server  installa3ons  to  a  new  level  of  excellence!   www.SQLpassion.at   Improvements • Multi-Column Predicates – Conjunctions (AND) – Disjunctions (OR) • Ascending Key Column Problems – Out of Range Estimations • Join Algorithm Changes • Troubleshooting through Extended Events
  • 15.
    Bring  your  SQL  Server  installa3ons  to  a  new  level  of  excellence!   www.SQLpassion.at   Multi-Column Predicates - AND • Conjunctions – old behavior – Independence Assumption – Selectivity of conjunctive predicates are multiplied – Doesn’t reflect reality
  • 16.
    Bring  your  SQL  Server  installa3ons  to  a  new  level  of  excellence!   www.SQLpassion.at   Multi-Column Predicates - AND • Conjunctions – new behavior – Predicates are sorted by their selectivity – 4 most selective predicates are kept – Predicates are softened by taking square roots – Exponential back-off Algorithm
  • 17.
    Bring  your  SQL  Server  installa3ons  to  a  new  level  of  excellence!   www.SQLpassion.at   Demo Exploring Conjunctions
  • 18.
    Bring  your  SQL  Server  installa3ons  to  a  new  level  of  excellence!   www.SQLpassion.at   Multi-Column Predicates - OR • Disjunctions – old behavior – Independence Assumption – Individual selectivities are added, and the estimate of the conjunction is subtracted – Doesn’t reflect reality
  • 19.
    Bring  your  SQL  Server  installa3ons  to  a  new  level  of  excellence!   www.SQLpassion.at   Multi-Column Predicates - OR • Disjunctions – new behavior – Independence Assumption – Disjunctions are converted to Conjunctions • “De Morgan’s Law”: http://en.wikipedia.org/wiki/De_Morgan's_laws – Same Exponential back-off Algorithm is applied to the Conjunction
  • 20.
    Bring  your  SQL  Server  installa3ons  to  a  new  level  of  excellence!   www.SQLpassion.at   Demo Exploring Disjunctions
  • 21.
    Bring  your  SQL  Server  installa3ons  to  a  new  level  of  excellence!   www.SQLpassion.at   Summary • Introduction to CE • CE in SQL Server 2014
  • 22.
    Bring  your  SQL  Server  installa3ons  to  a  new  level  of  excellence!   www.SQLpassion.at   SQL Server Query Tuning Workshop • 20. – 23. April 2015 in Zürich • Inhalte – Query Processing – Indexing & Statistics – Querying multiple Tables – Temporal Data & Aggregations – Windowing Functions – In-Memory Technologies • Weitere Informationen – http://www.SQLpassion.at/academy/query-tuning-workshop – 10% Rabatt für User Group Mitglieder!