Statistics

2,006 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,006
On SlideShare
0
From Embeds
0
Number of Embeds
1,623
Actions
Shares
0
Downloads
19
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Statistics

  1. 1. SQL Query Performance Analysis Statistics
  2. 2. Query OptimizerThe query optimizer in SQL Server is cost-based. It includes:1. Cost for using different resources (CPU and IO)2. Total execution timeIt determines the cost by using:• Cardinality: The total number of rows processed at each level of a query plan with the help of histograms , predicates and constraint• Cost model of the algorithm: To perform various operations like sorting, searching, comparisons etc.
  3. 3. Statistics Analysis• The query optimizer uses statistics to create query plans that improve query performance• A correct statistics will lead to high-quality query plan.• The query optimizer determines when statistics might be out- of-date by counting the number of data modifications since the last statistics update and comparing the number of modifications to a threshold.
  4. 4. Auto create statistics• Default setting of auto create statistics is ON.• It creates when:• Clustered and non clustered Index is created• Select query is executed.• Auto create and updates applies strictly to single-column statistics.
  5. 5. Why query 2 is performing better• If we perform following operations on field of any table in query predicate:1. Using any system function or user defined function2. Scalar operation like addition, multiplication etc.3. Type casting• In this situation sql server query optimizer is not able to estimate correct cardinality using statistics.
  6. 6. To improve cardinality• If possible, simplify expressions with constants in them.• If possible, dont perform any operation on the any field of a table in WHERE Clause, ON Clause, HAVING Clause• Dont use local variables in WHERE Clause, ON Clause, HAVING Clause.• If there is any cross relationship among fields or there is a complex expression in a field in a query predicates, it is better to create a computed column and then create a non-clustered index on it.
  7. 7. To improve cardinality• If possible, dont update the value of parameters of a function or stored procedure before using in sql statement• Use OPTIMIZE FOR clause when you want to optimize a sql query on the basis of specific parameter value.• If you want to update the value parameter of a stored procedure or a function create a similar procedure or function and execute it form base procedure or function by passing the updated value as a parameter.• Create user defined multi column statistics if query predicates have more than one fields of a table.
  8. 8. Tools• Sql query profiler• Database Tuning Advisor• Resource Governor
  9. 9. THANK YOU

×