Meta Data Cardinality Explored CSSQLUG User Group - June 2009.

Slides:



Advertisements
Similar presentations
Yukon – What is New Rajesh Gala. Yukon – What is new.NET Framework Programming Data Types Exception Handling Batches Databases Database Engine Administration.
Advertisements

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
SQL Server performance tuning basics
Using the Optimizer to Generate an Effective Regression Suite: A First Step Murali M. Krishna Presented by Harumi Kuno HP.
Understanding Parameter Sniffing Benjamin Nevarez Blog: benjaminnevarez.com 1.
Dave Ballantyne Clear Sky SQL. ›Freelance Database Developer/Designer –Specializing in SQL Server for 15+ years ›SQLLunch –Lunchtime usergroup –London.
Paper by: A. Balmin, T. Eliaz, J. Hornibrook, L. Lim, G. M. Lohman, D. Simmen, M. Wang, C. Zhang Slides and Presentation By: Justin Weaver.
Virtual techdays INDIA │ 9-11 February 2011 SQL 2008 Query Tuning Praveen Srivatsa │ Principal SME – StudyDesk91 │ Director, AsthraSoft Consulting │ Microsoft.
JavaScript, Third Edition
Chapter 9 Overview  Reasons to monitor SQL Server  Performance Monitoring and Tuning  Tools for Monitoring SQL Server  Common Monitoring and Tuning.
Introduction to Databases Chapter 6: Understanding the SQL Language.
Functions Lesson 10. Skills Matrix Function A function is a piece of code or routine that accepts parameters and stored as an object in SQL Server. The.
Module 7 Reading SQL Server® 2008 R2 Execution Plans.
Ashwani Roy Understanding Graphical Execution Plans Level 200.
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
Efficient RDF Storage and Retrieval in Jena2 Written by: Kevin Wilkinson, Craig Sayers, Harumi Kuno, Dave Reynolds Presented by: Umer Fareed 파리드.
Data Types Lesson 4. Skills Matrix Table A table stores your data. Tables are relational in that they are organized as rows and columns (a matrix). Each.
Connect with life Nauzad Kapadia Quartz Systems
Stored Procedure Optimization Preventing SP Time Out Delay Deadlocking More DiskReads By: Nix.
DAT410 SQL Server 2005 Optimizing Procedural Code Kimberly L. Tripp President/Founder, SQLskills.com.
1 Chapter 9 Tuning Table Access. 2 Overview Improve performance of access to single table Explain access methods – Full Table Scan – Index – Partition-level.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Virtual techdays INDIA │ august 2010 Filtered Indexes – The unexplored index … Vinod Kumar M │ Microsoft India Technology Evangelist – DB and BI.
Database Systems, 8 th Edition SQL Performance Tuning Evaluated from client perspective –Most current relational DBMSs perform automatic query optimization.
Common SQL Performance Issues AND HOW TO AVOID OR FIX THEM.
SQL Triggers, Functions & Stored Procedures Programming Operations.
Inside Query Processor Sort Dmitry Pilugin SQL Server Developer, MVP (RU) (EN)
SQL Server Statistics DEMO SQL Server Statistics SREENI JULAKANTI,MCTS.MCITP,MCP. SQL SERVER Database Administration.
Scott Fallen Sales Engineer, SQL Sentry Blog: scottfallen.blogspot.com.
Execution Plans Detail From Zero to Hero İsmail Adar.
SQL Server Statistics DEMO SQL Server Statistics SREENI JULAKANTI,MCTS.MCITP SQL SERVER Database Administration.
Dynamic SQL Writing Efficient Queries on the Fly ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
High Performance Functions SQLBits VI. Going backwards is faster than going forwards.
Session Name Pelin ATICI SQL Premier Field Engineer.
SQL Server Statistics and its relationship with Query Optimizer
Practical Database Design and Tuning
Parameter Sniffing in SQL Server Stored Procedures
Tuning Transact-SQL Queries
Query Optimization Techniques
Dynamic SQL Writing Efficient Queries on the Fly
15.1 – Introduction to physical-Query-plan operators
Stored Procedures – Facts and Myths
T-SQL Coding Techniques Are you playing with fire?
Parameter Sniffing in SQL Server Stored Procedures
Dynamic SQL Writing Efficient Queries on the Fly
Database Performance Tuning and Query Optimization
Introduction to Execution Plans
Query Execution Expectation-Reality Denis Reznik
Chapter 15 QUERY EXECUTION.
Now where does THAT estimate come from?
Cardinality Estimator 2014/2016
TSQL Coding Techniques
Query Optimization Techniques
JULIE McLAIN-HARPER LINKEDIN: JM HARPER
Statistics: What are they and How do I use them
Practical Database Design and Tuning
Hugo Kornelis Now where does THAT estimate come from? The nuts and bolts of cardinality estimation.
Transact SQL Performance Tips
Parameter Sniffing: the Good, the Bad, and the Ugly
Introduction to Execution Plans
Parameter Sniffing: the Good,the Bad, and the Ugly
Parameter Sniffing on SQL Server
Parameter Sniffing: the Good, the Bad, and the Ugly
Chapter 11 Database Performance Tuning and Query Optimization
Query Tuning Fundamentals
“Magic numbers”, local variable and performance
SQL Server Query Design and Optimization Recommendations
Introduction to Execution Plans
Query Optimization Techniques
Introduction to Execution Plans
Presentation transcript:

Meta Data Cardinality Explored CSSQLUG User Group - June 2009

Outline Define Cardinality Experience Cardinality Relevance of Cardinality Other related meta data Questions

About Me Love to Golf… Why are we here? Founder of Data Realized – Consultant – Boss – Employee – Grunt Occasional speaker – Dev Connections Fall 08 – SSWUG vConference Spring 09

Cardinality - Defined Wikipedia – In mathematics, the cardinality of a set is a measure of the “number of elements of the set”. There are two approaches to cardinality – one which compares sets directly using bijections and injections, and another which uses cardinal numbers. BOL Troubleshooting Poor Query Performance: Cardinality Estimation – The first factor, cardinality, is used as an input parameter of the second factor, the cost model. Therefore, improved cardinality leads to better estimated costs and, in turn, faster execution plans.

Cardinality Defined Optimizer » Have you ever wondered why the “estimated” quantity of rows is not what you expected in an execution plan? – Cost based – Will choose the lowest estimated processing cost » Total number of rows processed at each level of a query plan. » Cost model of the algorithm dictated by the operators used in the query. – SQL Server estimates cardinalities » Sources – Histograms when indexes are created Constraint information Logical rewrites of queries

Cardinality Defined Magic? – Not quite Cases where SQL Server cannot accurately calculate cardinalities. – Queries with predicates that use comparison operators between different columns of the same table. i.e. table.col1 > table.col2 – Queries with predicates that use operators, and any one of the following are true: – There are no statistics on the columns involved on either side of the operators. – The distribution of values in the statistics is not uniform, but the query seeks a highly selective value set. This situation can be especially true if the operator is anything other than the equality (=) operator. – The predicate uses the not equal to (!=) comparison operator or the NOT logical operator. – Queries that use any of the SQL Server built-in functions or a scalar-valued, user-defined function whose argument is not a constant value. – Queries that involve joining columns through arithmetic or string concatenation operators. – Queries that compare variables whose values are not known when the query is compiled and optimized.

Cardinality Defined Enough – I’m sold! What’s my cardinality, dude? – Demo query

Cardinality Experienced Results – Takeaway Great for profiling your data without “looking” at it all. Great for ensuring that your testing sets are indicative of reality. Great for determining which columns would be potential candidates for indexing, partitioning (date) and which wouldn’t. That give me an idea… let’s look at adding more data to the result set! Demo

Cardinality Experienced Results – Takeaway Great for profiling your data AND indexing without “looking” at it all. Great for performance environments – Is your cardinality what you expect? » Are the indexes behaving the way that you expect? Great for determining which columns would be potential candidates for indexing (clustered and non- clustered, partitioning (date) and which wouldn’t.

Cardinality Audience QA – Performance Testing – Environment Greenfield – Developer unit tests Existing or mature environment – Prod  QA – Prod  Performance environment » Critical to accurate benchmarks – Prod  Development » Critical to writing t-sql that’s appropriate for your data, schema and cardinality

Cardinality Audience Developers Parameter Sniffing Demo – The main points that you need to remember are: If you find that the optimizer is picking different plans over time that have varying performance characteristics, consider using a parameter hint with a representative “average” value to get a good, common query plan that will work reasonably for all values. If you are running really complex queries and need the plan choice to be exact in each case, you can consider using the “recompile” hint – just know that it will compile each time it runs, so this is likely more appropriate for longer- running queries to justify that compilation cost. Moving a query into a stored procedure can put it into a separate procedural context and can be a good way to get that value visible to the optimizer (Note: this works in SQL 2000 as well) Source:

Cardinality Audience Developers Cases where SQL Server cannot accurately calculate cardinalities. – Queries with predicates that use comparison operators between different columns of the same table. » Potential resolutions include computed columns or another table – Queries with predicates that use operators, and any one of the following are true: – There are no statistics on the columns involved on either side of the operators. » Create statistics – ensure that “Auto Create Statistics” = True – The distribution of values in the statistics is not uniform, but the query seeks a highly selective value set. This situation can be especially true if the operator is anything other than the equality (=) operator. » Avoid the use of local variables. Instead, use param’s, literals or expressions. – The predicate uses the not equal to (!=) comparison operator or the NOT logical operator. » Always known as a performance killer… now you know why! – Queries that use any of the SQL Server built-in functions or a scalar-valued, user-defined function whose argument is not a constant value. – Queries that involve joining columns through arithmetic or string concatenation operators. – Queries that compare variables whose values are not known when the query is compiled and optimized. – BOL has additional descriptions

Cardinality Audience Developers -- Continued Constant Folding and Expression Evaluation During Cardinality Estimation – Foldable Expressions and Nonfoldable expressions » Foldable Arithmetic expressions, such as 1+1, 5/3*2, that contain only constants. Logical expressions, such as 1=1 and 1>2 AND 3>4, that contain only constants. Built-in functions that are considered foldable by SQL Server, including CAST and CONVERT. Nondeterministic functions are not foldable. Deterministic built-in functions are foldable, with some exceptions. Exceptions are large data types (text, image, varchar(max), varbinary) SQL Server does not fold expressions that involve user-defined functions, even if they are deterministic. » Non-Foldable Nonconstant expressions such as an expression whose result depends on the value of a column. Expressions whose results depend on a local variable or parameter, such Nondeterministic functions. User-defined functions (both Transact-SQL and CLR). Expressions whose results depend on language settings. Expressions whose results depend on SET options. Expressions whose results depend on server configuration options. BOL has additional descriptions

Cardinality Audience Developers -- Continued Constant Folding and Expression Evaluation During Cardinality Estimation – Expression Evaluation – At compile time » The following operators are also evaluated at compile time if all their inputs are known: Arithmetic operators: +, -, *, /, unary -, Logical Operators: AND, OR, NOT Comparison operators:, =, <>, LIKE, IS NULL, IS NOT NULL Quick Demo

Cardinality – Query Processor Optimizer – Things to keep in mind – Cardinality will be represented (often at 30%) by optimizer » Avoid the use of local variables in queries. Instead, use parameters, literals, or expressions in the query. » Limit the use of operators and functions embedded in a query that contains a parameter to those listed under Compile-Time Expression Evaluation for Cardinality Estimation. » Make sure that constant-only expressions in the condition of your query are either constant-foldable, or can be evaluated at compilation time. » If you have to use a local variable to evaluate an expression to be used in a query, consider evaluating it in a different scope than the query. For example, it may be helpful to perform one of the following: Pass the value of the variable to a stored procedure that contains the query you want to evaluate, and have the query use the procedure parameter instead of a local variable. Construct a string that contains a query based in part on the value of the local variable, and then execute the string by using dynamic SQL (EXEC or sp_executesql). Parameterize the query and execute it by using sp_executesql, and pass the value of the variable as a parameter to the query.

Cardinality – Query Processor Optimizer – Things to keep in mind Trace flag 2301 – Available in 2005 SP1 Enhanced modeling ability for optimizer – Increases compile times – Increases memory usage (sometimes severely) Source: –

Meta Data – Cardinality Explored Questions? Thank You! Contact Information – – – – Twitter: DataRealized – LinkedIN: References » »