How to find the percentage of row modifications since last statistics update

Introduction
Why Does It Matter?
Find the percentage of row modifications with T-SQL
T-SQL description
Recommendation
Conclusion

Introduction

In the world of databases, keeping things running smoothly is key. Imagine your data as a ship sailing through the vast ocean of information. But just like real ships, databases need regular checks to ensure they're on the right course. One crucial aspect is understanding how much your data has changed recently, and that's where the concept of row modifications comes in.

Why Does It Matter?

Understanding these numbers helps us steer our data ship more effectively. Let's say we find a high percentage of modifications since the last update. That's like discovering a storm brewing ahead. If we ignore it, our queries might slow down, just like navigating through rough waters. But if we update our statistics regularly, it's like installing better navigation tools to sail through smoothly.

Find the percentage of row modifications with T-SQL

This SQL query retrieves information from the dynamic management views, providing the percentage of row modifications since last statistics update for a given database.

use dbdocs; -- Replace it with your Application database

select
	object_schema_name(i.id) object_schema,
    tbls.name as table_name,
    i.name as index_name,
	i.indid,
    i.rowmodctr as modified_rows,
	i.rowcnt,
    convert(decimal(18,8), i.rowmodctr / i.rowcnt) as modified_percent,
    stats_date(i.id, i.indid) as last_stats_update
from
    sysindexes i
    inner join
    sysobjects tbls
        on i.id = tbls.id
    inner join
    sysusers schemas
        on tbls.uid = schemas.uid
    inner join
    information_schema.tables tl
        on tbls.name = tl.table_name
        and schemas.name = tl.table_schema
        and tl.table_type = 'BASE TABLE'
where
    0 < i.indid
and
    tl.table_schema <> 'sys'
and
    i.rowmodctr <> 0
and
	i.rowcnt <> 0
order by
     modified_percent desc;

object-percent-modified

T-SQL description

Now, let's break down each component and understand its significance

Column            | Description
----------------- | ------------------------------------------------------------------
object_schema     | Object name.
table_name        | Table name.
index_name        | Index name.
indid             | Index ID.
rows_modified     | Number of rows modified.
row_count         | Total number of rows.
modified_percent  | Percentage of rows modified.
last_stats_update | Date of last statistics update.

Recommendation

If the percentage of modifications is significant, but the statistics haven't been updated recently, the query optimizer may not have accurate information for generating execution plans.
Schedule regular statistics updates for tables and indexes to ensure the query optimizer has up-to-date information for efficient query execution.

Conclusion

Understanding the percentage of row modifications in your SQL Server database is essential for maintaining optimal query performance. By analyzing this data and taking appropriate actions, such as index optimization and statistics updates, you can ensure that your database operates efficiently and delivers the best possible performance for your applications. Stay vigilant, keep monitoring, and be proactive in managing your database to unleash its full potential.

Related content



Rate Your Experience

: 89 : 1


Last updated in July, 2024

Online Tests
Read more

Cloud Technology
Read more

Oracle Database
Read more

MSSQL Database
Read more

PostGres Database
Read more

Linux
Read more

ASP/C#
Read more

Quick Access