SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 SQL Server Administration (2008)
 Database Fragmentation
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

guaro5555
Starting Member

15 Posts

Posted - 03/14/2013 :  08:30:26  Show Profile  Reply with Quote
Hi
I am new to Sql admin so go easy on me ,but I would like if possible for someone to share a good script to check fragmentation on a database to let me know in what shape it is.

Thanks a bunch gurus

russell
Pyro-ma-ni-yak

USA
5071 Posts

Posted - 03/14/2013 :  10:27:18  Show Profile  Visit russell's Homepage  Reply with Quote

declare @db int
set	@db = db_id()

SELECT	object_name(s.object_id), i.name, i.type_desc, index_depth,
	avg_fragmentation_in_percent, fragment_count, page_count
FROM	sys.dm_db_index_physical_stats (@db, null, NULL, NULL, NULL) s
join	sys.indexes i
on	i.object_id = s.object_id
And	i.index_id = s.index_id
JOIN	sys.partitions p
On	p.object_id = s.object_id
And	p.partition_number = s.partition_number
And	p.index_id = s.index_id
where	avg_fragmentation_in_percent > 29
--and	p.rows > 9999
--and	index_depth > 2
Go to Top of Page

guaro5555
Starting Member

15 Posts

Posted - 03/14/2013 :  11:06:51  Show Profile  Reply with Quote
R thanks for jumping in
I know i might get some heat for my ignorance but which parameter do I have to change to enter the database name .that will be query for this information on this script

again thanks a bunch for your help and patience

Go to Top of Page

guaro5555
Starting Member

15 Posts

Posted - 03/14/2013 :  11:07:56  Show Profile  Reply with Quote
using like "test" as a database name example
Go to Top of Page

russell
Pyro-ma-ni-yak

USA
5071 Posts

Posted - 03/14/2013 :  11:22:16  Show Profile  Visit russell's Homepage  Reply with Quote
The query I posted does it for the current database.

USE test;
then the rest of the script
Go to Top of Page

guaro5555
Starting Member

15 Posts

Posted - 03/14/2013 :  11:25:45  Show Profile  Reply with Quote
Thankssss a BUNCHHHH chief
Go to Top of Page

guaro5555
Starting Member

15 Posts

Posted - 03/14/2013 :  11:27:44  Show Profile  Reply with Quote
R one last question and thanks again

what does this do

--and p.rows > 9999
--and index_depth > 2

Thanks
Go to Top of Page

russell
Pyro-ma-ni-yak

USA
5071 Posts

Posted - 03/14/2013 :  12:13:31  Show Profile  Visit russell's Homepage  Reply with Quote
If you un-comment those two lines it will filter out small indexes that won't likely benefit from being rebuilt or defragmented.
Go to Top of Page

guaro5555
Starting Member

15 Posts

Posted - 03/14/2013 :  13:50:34  Show Profile  Reply with Quote
YOU the GURU thanks abunch
Go to Top of Page

russell
Pyro-ma-ni-yak

USA
5071 Posts

Posted - 03/14/2013 :  21:14:37  Show Profile  Visit russell's Homepage  Reply with Quote
Welcome Glad to help
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000