# MS SQL Database Table Fragmentation

**URL:** <https://community.dynatrace.com/t/ms-sql-database-table-fragmentation/36818>\
**Category:** Open Q&A\
**Tags:** databases, sql, extensions\
**Created:** [October 6, 2022, 2:51pm UTC](https://community.dynatrace.com/t/ms-sql-database-table-fragmentation/36818 "2022-10-06T14:51:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![runatyr](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/runatyr/32/11396_2.png) [@runatyr](https://community.dynatrace.com/u/runatyr)\
**Post date:** [October 6, 2022, 2:51pm UTC](https://community.dynatrace.com/t/ms-sql-database-table-fragmentation/36818/1 "2022-10-06T14:51:54Z")

</div>

Hello:

Is there a way for Dynatrace to see down to the MS SQL Table level and extract fragmentation statistics?

---

<div class="post-metadata">

**Author:** ![DanielS](https://avatars.discourse-cdn.com/v4/letter/d/3ab097/32.png) [@DanielS](https://community.dynatrace.com/u/DanielS)\
**Post date:** [October 6, 2022, 3:14pm UTC](https://community.dynatrace.com/t/ms-sql-database-table-fragmentation/36818/2 "2022-10-06T15:14:28Z")

</div>

Hello, the metrics that you can see through the plugin are these &nbsp; [https://www.dynatrace.com/support/help/shortlink/ms-sql#metrics](https://www.dynatrace.com/support/help/shortlink/ms-sql#metrics)

For any other metric you can do it by ingesting them but you need to get it by other means.

---

<div class="post-metadata">

**Author:** ![runatyr](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/runatyr/32/11396_2.png) [@runatyr](https://community.dynatrace.com/u/runatyr)\
**Post date:** [October 18, 2022, 5:38pm UTC](https://community.dynatrace.com/t/ms-sql-database-table-fragmentation/36818/3 "2022-10-18T17:38:45Z")

</div>

How would one go about ingesting Table Fragmentation levels?

Or is that a metric that is supported?

---

<div class="post-metadata">

**Author:** ![DanielS](https://avatars.discourse-cdn.com/v4/letter/d/3ab097/32.png) [@DanielS](https://community.dynatrace.com/u/DanielS)\
**Post date:** [November 2, 2022, 10:18pm UTC](https://community.dynatrace.com/t/ms-sql-database-table-fragmentation/36818/4 "2022-11-02T22:18:44Z")

</div>

SELECT S.name as ‘Schema’,T.name as ‘Table’,I.name as ‘Index’,DDIPS.avg\_fragmentation\_in\_percent,DDIPS.page\_countFROM sys.dm\_db\_index\_physical\_stats (DB\_ID(), NULL, NULL, NULL, NULL) AS DDIPSINNER JOIN sys.tables T on T.object\_id = DDIPS.object\_idINNER JOIN sys.schemas S on T.schema\_id = S.schema\_idINNER JOIN sys.indexes I ON I.object\_id = DDIPS.object\_idAND DDIPS.index\_id = I.index\_idWHERE DDIPS.database\_id = DB\_ID()and I.name is not nullAND DDIPS.avg\_fragmentation\_in\_percent \> 0ORDER BY DDIPS.avg\_fragmentation\_in\_percent descand you could ingest the data creating dimensions and metrics as you need following this guidehttps://www.dynatrace.com/support/help/shortlink/local-api

---

<div class="post-metadata">

**Author:** ![avdveen](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@avdveen](https://community.dynatrace.com/u/avdveen)\
**Post date:** [January 16, 2025, 7:45am UTC](https://community.dynatrace.com/t/ms-sql-database-table-fragmentation/36818/5 "2025-01-16T07:45:59Z")

</div>

[@DanielS](https://community.dynatrace.com/user/35788)&nbsp;thanks for sharing the query!  
What I did was to set up an extension using the **Extensions Creator** app and the query you provided.

**MSSQL query**

_SELECT S.name as ‘schema’,_  
_T.name as ‘table’,_  
_I.name as ‘index’,_  
_DDIPS.avg\_fragmentation\_in\_percent as ‘fragmentation’,_  
_DDIPS.page\_count_  
_FROM sys.dm\_db\_index\_physical\_stats (NULL, NULL, NULL, NULL, NULL) AS DDIPS_  
_INNER JOIN sys.tables T on T.object\_id = DDIPS.object\_id_  
_INNER JOIN sys.schemas S on T.schema\_id = S.schema\_id_  
_INNER JOIN sys.indexes I ON I.object\_id = DDIPS.object\_id_  
_AND DDIPS.index\_id = I.index\_id_  
_WHERE DDIPS.database\_id = DB\_ID()_  
_and I.name is not null_  
_AND DDIPS.avg\_fragmentation\_in\_percent \> 0_  
_ORDER BY DDIPS.avg\_fragmentation\_in\_percent desc_

The config of the extension

 ![andre_vdveen_0-1737013074647.png](https://global.discourse-cdn.com/dynatrace/original/3X/4/a/4aa05042c8f611770703a1914c16d73330541dea.png)

The results in Data Explorer

 ![andre_vdveen_1-1737013222939.png](https://global.discourse-cdn.com/dynatrace/original/3X/e/1/e1f1ee79c8ed071d3fbd55ae777c2bf72d9940c0.png)
