Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

how to List out all tables indexes having index fragmentation more than 50% of databases. Expand / Collapse
Author
Message
Posted Friday, July 12, 2013 6:14 AM
Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Today @ 3:20 AM
Points: 676, Visits: 1,332
Make sure when you run that query you change the database your in to match the one your querying information about. Otherwise you will get information that might not be correct when the object_id function runs, as object_id is unique for that database.

Joie Andrew
"Since 1982"
Post #1472970
Posted Tuesday, December 17, 2013 6:45 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, January 06, 2014 11:00 AM
Points: 5, Visits: 19
I could not get this to work if the DB is running in SQL 2000 compatibility mode. I think for that the script needs to be changed a bit. Otherwise works well.
Post #1523663
Posted Tuesday, December 17, 2013 7:41 AM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Yesterday @ 10:01 AM
Points: 741, Visits: 1,062
Try this.

SELECT
B.name AS TableName
, C.name AS IndexName
, C.fill_factor AS IndexFillFactor
, D.rows AS RowsCount
, A.avg_fragmentation_in_percent
, A.page_count
FROM sys.dm_db_index_physical_stats(null,NULL,NULL,NULL,NULL) A
INNER JOIN sys.objects B
ON A.object_id = B.object_id
INNER JOIN sys.indexes C
ON B.object_id = C.object_id AND A.index_id = C.index_id
INNER JOIN sys.partitions D
ON B.object_id = D.object_id AND A.index_id = D.index_id
WHERE C.index_id > 0
Post #1523688
Posted Tuesday, December 17, 2013 7:51 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 4:21 AM
Points: 41,507, Visits: 34,422
David Knapp (12/17/2013)
I could not get this to work if the DB is running in SQL 2000 compatibility mode. I think for that the script needs to be changed a bit. Otherwise works well.


Change FROM sys.dm_db_index_physical_stats(DB_ID(),NULL,NULL,NULL,NULL) A to use a fixed value for the first parameter rather than the function. The function in parameters is disallowed under compat mode 80.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1523696
Posted Tuesday, December 17, 2013 7:53 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 4:21 AM
Points: 41,507, Visits: 34,422
Neeraj Dwivedi (12/17/2013)
Try this.

SELECT
B.name AS TableName
, C.name AS IndexName
, C.fill_factor AS IndexFillFactor
, D.rows AS RowsCount
, A.avg_fragmentation_in_percent
, A.page_count
FROM sys.dm_db_index_physical_stats(null,NULL,NULL,NULL,NULL) A
INNER JOIN sys.objects B
ON A.object_id = B.object_id
INNER JOIN sys.indexes C
ON B.object_id = C.object_id AND A.index_id = C.index_id
INNER JOIN sys.partitions D
ON B.object_id = D.object_id AND A.index_id = D.index_id
WHERE C.index_id > 0


That won't work well. With null as the first parameter all databases' index physical stats are pulled, but the sys.objects and sys.indexes are local to the database this is run in, all the other indexes will be filtered out by the inner join or, if the objectID happens to match an object in the current database, return incorrect object names.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1523698
Posted Tuesday, December 17, 2013 8:06 AM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Yesterday @ 10:01 AM
Points: 741, Visits: 1,062
My mistake.
Get database id from from sys.sysdatabases and pass that as first parameter.
Post #1523708
Posted Wednesday, December 18, 2013 11:20 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, January 06, 2014 11:00 AM
Points: 5, Visits: 19
Thank you! I am not in the office for a few days but will try it out when I get back.
Post #1524260
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse