Home
%3CLINGO-SUB%20id%3D%22lingo-sub-740769%22%20slang%3D%22en-US%22%3ELesson%20Learned%20%2395%3A%20Updating%20the%20auto-created%20and%20user%20defined%20statistics%20only%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-740769%22%20slang%3D%22en-US%22%3E%3CP%3EToday%2C%20I%26nbsp%3Bworked%20on%20a%20service%20request%20that%20our%20customer%20has%20implemented%20a%20process%20to%20rebuild%20the%20indexes%26nbsp%3Band%20after%20it%20they%20update%20the%20statistics.%20As%20this%20process%20is%20taking%20too%20much%20time%2C%20they%20want%20to%20reduce%20the%20time%2C%20and%2C%20I%20found%20an%20important%20thing%20about%20how%20the%20statistics%20are%20updated.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOnce%20thing%20that%20I%20saw%2C%20was%20that%20they%20are%20using%20a%20script%20that%20rebuild%20the%20indexes%20and%20after%20it%20they%20update%20all%20the%20statistics%2C%20but%2C%20without%20filtering%20if%20this%20statistic%20has%20been%20updated%20previously%20due%20to%20the%20rebuild%20process%20(we%20need%20to%20know%20that%2C%20every%20time%20that%20you%20rebuild%20an%20index%2C%20the%20statistic%20that%20is%20associated%20to%20this%20index%20will%20be%20update%20too).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%2C%20I%20suggested%20to%20use%20this%20script%2C%20leaving%20to%20our%20customer%20decides%20what%20is%20the%20better%20value%20for%20modification_counter%2C%20etc..%20where%20we%20update%20the%20statistics%20that%20have%20been%20created%20automatically%20by%20SQL%20Server%20engine%20or%20by%20the%20user.%20Remember%20that%20when%20you%20update%20the%20statistics%20SQL%20Server%20needs%20to%20sort%20the%20data%20by%20the%20field(s)%20that%20is%2Fare%20in%20the%20definition%20of%20the%20statistics%20using%20time%20and%20resources.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3ESELECT%20%20sp.rows%2C%20sp.modification_counter%2C%0A%20%20o.name%20as%20objname%2C%20s.name%2C%0A%20%20CASE%20WHEN%20(s.auto_created%3D1)%20THEN%20'AUTOSTATS'%0A%20%20%20%20%20%20%20WHEN%20(s.user_created%3D1)%20THEN%20'STATS'%0A%20%20END%20as%20type%2C%0A%20%20sp.last_updated%2C%0A%20%20s.no_recompute%2C%20%0A%20%20o.object_id%20as%20objid%2C%20%0A%20%20persisted_sample_percent%2C%0A%20%20CASE%20WHEN%20(sp.persisted_sample_percent%3D0%20OR%20sp.persisted_sample_percent%20IS%20NULL)%0A%20%20%20%20%20%20%20%20%20%20THEN%20'UPDATE%20STATISTICS%20%5B'%20%2B%20OBJECT_SCHEMA_NAME(O.object_id)%20%2B%20'%5D.%5B'%20%2B%20o.name%20%2B%20'%5D%20('%2B%20s.name%20%2B%20')%20WITH%20FULLSCAN'%20%0A%20%20%20%20%20%20%20WHEN%20(sp.persisted_sample_percent%26gt%3B0)%0A%20%20%20%20%20%20%20%20%20THEN%20'UPDATE%20STATISTICS%20%5B'%20%2B%20OBJECT_SCHEMA_NAME(O.object_id)%20%2B%20'%5D.%5B'%20%2B%20o.name%20%2B%20'%5D%20('%2B%20s.name%20%2B%20')%20WITH%20SAMPLE%20'%20%2B%20CONVERT(varchar(200)%2Csp.persisted_sample_percent)%20%2B%20'%20PERCENT'%0A%20%20END%20as%20TSQLCmd%0AFROM%20sys.stats%20s%0ACROSS%20APPLY%20sys.dm_db_stats_properties(s.object_id%2C%20s.stats_id)%20AS%20sp%20%20%0Ajoin%20%20sys.objects%20o%20%20WITH%20(nolock)%20on%20s.object_id%3Do.object_id%20%0AWHERE%20o.type%20%3D%20'U'%20and%20(s.auto_created%3D1%20or%20s.user_created%3D1)%0AAND%20sp.modification_counter%20%26gt%3B%3D%201%20or%20s.no_recompute%3D1%0AORDER%20BY%20objid%3C%2FPRE%3E%0A%3CP%3EI%20used%20these%20two%20URLs%20about%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fsystem-dynamic-management-views%2Fsys-dm-db-stats-properties-transact-sql%3Fview%3Dsql-server-2017%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Edm_db_stats_properties%3C%2FA%3E%20and%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ft-sql%2Fstatements%2Fupdate-statistics-transact-sql%3Fview%3Dsql-server-2017%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Eupdate%20statistics%3C%2FA%3E%26nbsp%3Bfor%20reference.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EEnjoy!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-740769%22%20slang%3D%22en-US%22%3E%3CP%3EToday%2C%20I%26nbsp%3Bworked%20on%20a%20service%20request%20that%20our%20customer%20has%20implemented%20a%20process%20to%20rebuild%20the%20indexes%26nbsp%3Band%20after%20it%20they%20update%20the%20statistics.%20As%20this%20process%20is%20taking%20too%20much%20time%2C%20they%20want%20to%20reduce%20the%20time%2C%20and%2C%20I%20found%20an%20important%20thing%20about%20how%20the%20statistics%20are%20updated.%3C%2FP%3E%3C%2FLINGO-TEASER%3E

Today, I worked on a service request that our customer has implemented a process to rebuild the indexes and after it they update the statistics. As this process is taking too much time, they want to reduce the time, and, I found an important thing about how the statistics are updated.

 

Once thing that I saw, was that they are using a script that rebuild the indexes and after it they update all the statistics, but, without filtering if this statistic has been updated previously due to the rebuild process (we need to know that, every time that you rebuild an index, the statistic that is associated to this index will be update too).

 

So, I suggested to use this script, leaving to our customer decides what is the better value for modification_counter, etc.. where we update the statistics that have been created automatically by SQL Server engine or by the user. Remember that when you update the statistics SQL Server needs to sort the data by the field(s) that is/are in the definition of the statistics using time and resources.

 

SELECT  sp.rows, sp.modification_counter,
  o.name as objname, s.name,
  CASE WHEN (s.auto_created=1) THEN 'AUTOSTATS'
       WHEN (s.user_created=1) THEN 'STATS'
  END as type,
  sp.last_updated,
  s.no_recompute, 
  o.object_id as objid, 
  persisted_sample_percent,
  CASE WHEN (sp.persisted_sample_percent=0 OR sp.persisted_sample_percent IS NULL)
          THEN 'UPDATE STATISTICS [' + OBJECT_SCHEMA_NAME(O.object_id) + '].[' + o.name + '] ('+ s.name + ') WITH FULLSCAN' 
       WHEN (sp.persisted_sample_percent>0)
         THEN 'UPDATE STATISTICS [' + OBJECT_SCHEMA_NAME(O.object_id) + '].[' + o.name + '] ('+ s.name + ') WITH SAMPLE ' + CONVERT(varchar(200),sp.persisted_sample_percent) + ' PERCENT'
  END as TSQLCmd
FROM sys.stats s
CROSS APPLY sys.dm_db_stats_properties(s.object_id, s.stats_id) AS sp  
join  sys.objects o  WITH (nolock) on s.object_id=o.object_id 
WHERE o.type = 'U' and (s.auto_created=1 or s.user_created=1)
AND sp.modification_counter >= 1 or s.no_recompute=1
ORDER BY objid

I used these two URLs about dm_db_stats_properties and update statistics for reference.

 

Enjoy!