December 13, 2017 at 12:23 pm
I have a proc where I update stats based on rowmodctr value in sys.sysindexes. But because this view is deprecated I want to find a replacement. I found sys.dm_db_stats_properties function, but I need to get object_id() and call it in a cursor for each index/statistics so it's inconvenient to use in queries.
But is there any view or DMV with this value?
Thanks
December 13, 2017 at 12:49 pm
SQL Guy 1 - Wednesday, December 13, 2017 12:23 PMI have a proc where I update stats based on rowmodctr value in sys.sysindexes. But because this view is deprecated I want to find a replacement. I found sys.dm_db_stats_properties function, but I need to get object_id() and call it in a cursor for each index/statistics so it's inconvenient to use in queries.But is there any view or DMV with this value?
Thanks
Colmodctr is supposed to be the replacement but I don't believe it's exposed in any DMVs, system tables. Didn't used to be anyway.
The view sys.system_internals_partition_columns has some counts on the changes but I believe it's more of an approximation. Take a look at this articles as it goes into some of this and may give you some other ideas:
How are per-column modification counts tracked?
Sue
December 13, 2017 at 1:43 pm
Thanks, Sue for pointing to a sys.system_internals_partition_columns but unfortunately its modified_count does not reset after I run UPDATE STATISTICS. Besides, it is selecting much slower than outgoing sys.sysindexes.
Paul in his article refers to a hidden/undocumented sys.sysrscols which I even cannot access - I have not full DBA privileges on server, so I can't use it in applications.
Will continue searching further.
December 13, 2017 at 2:27 pm
It's the column 'modification_counter' in sys.dm_db_stats_properties.
You don't need to use a cursor for object ID and stats ids. You can use CROSS APPLY to pass them.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
December 13, 2017 at 2:38 pm
no need for cursors, you can use this function with a CROSS APPLY like so:SELECT OBJECT_SCHEMA_NAME(i.object_id, DB_ID()) AS SchemaName, OBJECT_NAME(i.object_id) AS TableName, i.index_id, i.name AS IndexName, i.is_primary_key, i.is_unique,
sp.last_updated, sp.modification_counter, sp.rows
FROM sys.indexes i
CROSS APPLY sys.dm_db_stats_properties (i.object_id, i.index_id) sp
WHERE OBJECT_SCHEMA_NAME(i.object_id, DB_ID()) <> 'sys'
AND sp.modification_counter > 0
ORDER BY SchemaName, TableName, i.index_id;
December 13, 2017 at 7:17 pm
Heh... although RowModCtr is deprecated, it's still what is used by Microsoft in the sp_updatestats sproc even in SQL Server 2016.
--Jeff Moden
Change is inevitable... Change for the better is not.
December 14, 2017 at 7:57 am
Thanks all for valuable replies. I modified the query in this way:
SELECT --t.object_id,
s.name,
t.name,
a.name,
sp.last_updated,
sp.modification_counter,
sp.rows,
modified_percent = sp.modification_counter*100.0/sp.rows
FROM sys.schemas s
join sys.tables t on s.schema_id = t.schema_id
join sys.stats a on t.object_id = a.object_id
CROSS APPLY sys.dm_db_stats_properties (t.object_id, a.stats_id) sp
WHERE t.name = 'my_table_name'
Now another question, because I have both total rows and modified rows, what would be better criteria to update statistics: modification_counter > 0 or modified_percent > 0.5% ? Or some another value?
I am writing this code not for regular nightly/weekly maintenance but for application and SSIS, and want this code to have minimal performance impact. Average daily load for some tables ~= 200 mln rows.
December 14, 2017 at 10:36 am
SQL Guy 1 - Thursday, December 14, 2017 7:57 AMThanks all for valuable replies. I modified the query in this way:
SELECT --t.object_id,
s.name,
t.name,
a.name,
sp.last_updated,
sp.modification_counter,
sp.rows,
modified_percent = sp.modification_counter*100.0/sp.rows
FROM sys.schemas s
join sys.tables t on s.schema_id = t.schema_id
join sys.stats a on t.object_id = a.object_id
CROSS APPLY sys.dm_db_stats_properties (t.object_id, a.stats_id) sp
WHERE t.name = 'my_table_name'Now another question, because I have both total rows and modified rows, what would be better criteria to update statistics: modification_counter > 0 or modified_percent > 0.5% ? Or some another value?
I am writing this code not for regular nightly/weekly maintenance but for application and SSIS, and want this code to have minimal performance impact. Average daily load for some tables ~= 200 mln rows.
If the table is truncated and reloaded every day, you probably don't have to worry about stats at all... it's likely they'll update all by themselves. In 2014 (IIRC), if the 200 million rows is more than 20% of the table, stats will automatically update unless you have auto updates turned off. Again IIRC, the stats rebuilds in either case will be based on what SQL Server wants to use for a sample. If you don't like the sample size (which can be terribly small), then consider doing a full sample because the difference between doing a full sample and even as little as a 25% sample is frequently a tie.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply