Optimising Queries - Series 2 Maintaining up-to-date statistics
-
Upload
dageop-ltd -
Category
Data & Analytics
-
view
51 -
download
1
Transcript of Optimising Queries - Series 2 Maintaining up-to-date statistics
www.dageop.com
Optimising Queries
®
OQ-02 Maintaining up-to-date statistics
DR. SUBRAMANI PARAMASIVAM (MANI)
About me
Dr. SubraMANI ParamasivamPhD., MCT, MCSE, MCITP, MCP, MCTS, MCSACEO, Principal Consultant & Trainer @ DAGEOP (UK) Email: [email protected] Blog: http://dataap.org/blog
Follow Us
https://www.facebook.com/pages/YOUR-SQL-MAN-LTD/http://www.youtube.com/user/YourSQLMAN
https://twitter.com/dageop
https://uk.linkedin.com/in/dageop
Proud Sponsor
• SQLBits• SQL Saturdays• MCT Summit• SQL Server Geeks
Summit• Data Awareness
Programme• Dageop’s Data Day
®
www.DataAP.org
SPEAKER
Contents• OQ-02 Maintaining up-to-date statistics• Index Vs. column• Automatic Vs. manual• Full-scan Vs. sample
www.dageop.comOptimizing Queries
Maintaining up-to-date statistics• DBA need to maintain the statistics up-to date to get full performance
over index.• Sometimes the multiple updates/delete will happen in the database
at that time index should be up-do date otherwise the benefits from the index will be very less or even index will not involve in execution process.
www.dageop.comOptimizing Queries
Maintaining up-to-date statistics• The estimated cost of a plan is based on the algorithms or operators
used, and their cardinality estimations.• Statistics divided in to 3 areas which helps the cardinality estimation
process• the histogram, • the density information, • and the string statistics
• A cardinality estimate is the estimated number of records that will be returned by filtering, JOIN predicates or GROUP BY operations.
www.dageop.comOptimizing Queries
Maintaining up-to-date statistics• Statistics are created • AUTO_CREATE_STATISTICS option• TSQL Explicit statement CREATE STATISTICS
• Synchronous update• SQL Server updates statistics before optimizing and executing the query
• Asynchronous update• AUTO_UPDATE_STATISTICS_ASYNC• Query optimizer does not wait for auto update and use the current one.
www.dageop.comOptimizing Queries
Maintaining up-to-date statistics• Maintaining Statistics• Query Optimizer updates by default• UPDATE STATISTICS statement and can be scheduled (maintenance)
• Queries need not wait for Query Optimizer • Delays in optimization
• sp_updatestats also uses UPDATE STATISTICS behind the scenes.• Rebuilding an index
• Equivalent to STATISTICS WITH FULLSCAN• Reorganizing an index
• This does not updates the statistics.
www.dageop.comOptimizing Queries
Index vs. column• One major difference between indexes and column statistics is that
indexes are permanent objects that are updated when changes to the underlying table occur, while column statistics are not.• If new index available for the table then the optimizer will consider
them for implementation.• When new or refreshed column statistics are available, the statistics
manager will interrogate immediately. Re-optimization will occur if the answers are significantly different from old refreshed statistics.
www.dageop.comOptimizing Queries
Automatic vs. Manual• An internal SQL Server process will monitor the updates to a table’s
columns to identify when the statistics should be updated.• SQL Server will keep track of the number of modifications made to a
column via column modification counter.• Based on modification counter, SQL Server will determine whether
statistics are out of date and needs to be updated.• If the statistics are defined on temporary table, there is an additional
threshold for updating statistics.
www.dageop.comOptimizing Queries
Automatic vs. Manual• When you run sp_autostats and simply supply the table name, it
displays the current setting for the table as well as the database.
Following are the settings for the mentioned table:• Exec sp_autostats 'person.contact’• You can also turn AutoStats ON or OFF for the entire database by
setting the database option in SQL Server Management Studio.• We can use the SQL scripts to update statistics.
www.dageop.comOptimizing Queries
Full-scan vs. Sample• FULL SCAN• Computes statistics by scanning all rows in the table or indexed view• FULLSCAN and SAMPLE 100 PERCENT have the same results.• UPDATE STATISTICS Sales.Customer(Customername) WITH FULLSCAN
• SAMPLE• PERCENT – 0 to 100• ROWS – 0 to total number of rows.• Recommended 0 for percent & rows. (Object is updated and does not contain
statistics data)• UPDATE STATISTICS Sales.Customer(Customername) WITH SAMPLE 30 PERCENT
www.dageop.comOptimizing Queries
ReviewMaintaining up-to-date statistics
Index Vs. columnAutomatic Vs. manualFull-scan Vs. sample
www.dageop.comOptimizing Queries
®
www.dageop.com