MCITP

MCITP

Tuesday, September 23, 2014

Statistics good for performance

You can add statistics on columns that don’t have statistics in order to boost query performance and also in order to create more optimal execution plans.

Statistics can be created different ways- Statistics are automatically created for each index key you create- If the database setting autocreate stats is on, then SQL Server will automatically create statistics for non-indexed columns that are used in queries  

To get Statistics  detail :
DBCC SHOW_STATISTICS('tablename','indexname') WITH HISTOGRAM

- Auto Update Statistics basically means, if there is an incoming query but statistics are stale, SQL Server will update statistics first before it generates an execution plan.
- Auto Update Statistics Asynchronously on the other hand means, if there is an incoming query but statistics are stale, SQL Server uses the stale statistics to generate the execution plan, then updates the statistics afterwards.  

How do we know statistics are being used?

One good check you can do is when you generate execution plans for your queries: check out your “Actual Number of Rows” and “Estimated Number of Rows”.  If these numbers are (consistently) fairly close, then most likely your statistics are up-to-date and used by the optimizer for the query. If
not, time for you to re-check your statistics create/update frequency.

---There may be cases when you may want to disable statistics update temporarily while you’re doing massive updates on a table, and you don’t want it to be slowed down by the autoupdate.

--- to manually update statistics, you can use either
-- sp_updatestats or  UPDATE STATISTICS  

One way to get better statistics is to manually update the statistics for the table using the

UPDATE STATISTICs commandwith the FULLSCAN option or SAMPLE option.
e.g :
USE AdventureWorks;
GO
CREATE STATISTICS Products    ON Production.Product ([Name], ProductNumber)  
WITH SAMPLE 50 PERCENT

UPDATE STATISTICS Production.Product(Products)     WITH SAMPLE 50 PERCENT;

USE AdventureWorks;
GO
UPDATE STATISTICS Production.Product(Products)    WITH FULLSCAN, NORECOMPUTE;
GO

No comments:

Post a Comment