System databases fail to update user tables statistics, how to workaround ?

Miguel Gavinhos 216 Reputation points
2021-10-28T12:50:25.12+00:00

System databases fail to update user tables statistics. How to workaround?
I did :
-https://ola.hallengren.com/sql-server-index-and-statistics-maintenance.html with

EXECUTE dbo.IndexOptimize
@Databases = 'SYSTEM_DATABASES',
@FragmentationLow = NULL,
@FragmentationMedium = NULL,
@FragmentationHigh = NULL,
@UpdateStatistics = 'ALL',
@OnlyModifiedStatistics = 'N',
@StatisticsSample = 100

-maintenace plan statistics system databases.

And I get last_updated filed outdated.

The script is :

SELECT obj.name, obj.object_id, stat.name, stat.stats_id,
last_updated, modification_counter , OBJ., STAT.
FROM sys.objects AS obj
INNER JOIN sys.stats AS stat ON stat.object_id = obj.object_id
CROSS APPLY sys.dm_db_stats_properties(stat.object_id, stat.stats_id) AS sp
where type = 'U'
ORDER BY modification_counter DESC

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,606 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,546 questions
0 comments No comments
{count} votes

Accepted answer
  1. Miguel Gavinhos 216 Reputation points
    2021-10-31T13:54:54.59+00:00

    The target version is SQL SERVER 2019

    The Ola Hallengren script's

    EXECUTE dbo.IndexOptimize
    @Databases = 'SYSTEM_DATABASES',
    @FragmentationLow = NULL,
    @FragmentationMedium = NULL,
    @FragmentationHigh = NULL,
    @UpdateStatistics = 'ALL',
    @OnlyModifiedStatistics = 'N',
    @StatisticsSample = 100

    It doesn't seem to update statistics (user tables) in system databases. The workaround is to run the script :

    https://www.dbdelta.com/sql-server-system-table-statistics-update/
    USE msdb ; USE Master, and so on.

    DECLARE @alenzi nvarchar(MAX) =
    (
    SELECT
    STRING_AGG(
    N'UPDATE STATISTICS '
    + QUOTENAME(OBJECT_SCHEMA_NAME(i.object_id))
    + N'.'
    + QUOTENAME(OBJECT_NAME(i.object_id))
    ,';')
    FROM sys.indexes AS I
    JOIN sys.partitions AS p ON
    p.object_id = i.object_id
    AND p.index_id = i.index_id
    WHERE
    OBJECTPROPERTYEX(i.object_id, 'IsSystemTable') = 0
    AND i.index_id > 0
    AND p.rows > 0
    );
    EXEC sp_executesql @alenzi ;
    GO

    Just a few lines. If Ola is correct, you may consider that it isn't good to touch system databases in production. So instead, let SQL servers manage the system databases completely. However, if you find an odd behavior like a very slow DMV that you suspect is related to outdated statistics in system tables, then consider this script.


4 additional answers

Sort by: Most helpful
  1. Andreas Kreuzberg 96 Reputation points
    2021-10-28T13:54:20.367+00:00

    Hi,
    have you tried this parameter in your script:

    StatisticsModificationLevel (Specify a percentage of modified rows for when the statistics should be updated)

    Maybe there are not enough changes in your statistic, and so it never would do an update for you.

    And, if I try to use your script, it would never run. Maybe there are copy & paste error.

    Good luck,

    Andreas

    0 comments No comments

  2. Tom Phillips 17,716 Reputation points
    2021-10-28T14:03:19.007+00:00

    Very small tables do not need to be updated. This is not a problem.

    0 comments No comments

  3. CathyJi-MSFT 21,071 Reputation points Microsoft Vendor
    2021-10-29T06:37:49.13+00:00

    Hi @Miguel Gavinhos ,

    Why did you update the statistics for this table? May be your table do not need to be updated. Updating statistics ensures that queries compile with up-to-date statistics. However, updating statistics causes queries to recompile. We recommend not updating statistics too frequently because there is a performance tradeoff between improving query plans and the time it takes to recompile queries. The specific tradeoffs depend on your application.

    Consider updating statistics for the following conditions:
    •Query execution times are slow.
    •Insert operations occur on ascending or descending key columns.
    •After maintenance operations.

    Refer to MS document When to update statistics.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  4. Miguel Gavinhos 216 Reputation points
    2021-10-31T13:57:49.37+00:00

    I mean to say ..."...related to outdated statistics in system DATABASES, then consider this script."

    0 comments No comments