Skip to content

Commit f4deef6

Browse files
authored
Added Find Index Fragmentation Script
Added Find Index Fragmentation Script
1 parent f30a8e0 commit f4deef6

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Diff for: find_index_fragmentation.sql

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
/**************************************************************************
3+
Find Index Fragmentation
4+
Author: Eric Cobb - http://www.sqlnuggets.com/blog/sql-scripts-find-index-fragmentation/
5+
Source: https://github.com/ericcobb/SQL-Server-Index-Insight-Scripts
6+
Supported Versions: SQL Server 2008, SQL Server 2012, SQL Server 2014, SQL Server 2016, SQL Server 2017
7+
License:
8+
MIT License
9+
10+
Copyright (c) 2017 Eric Cobb
11+
Permission is hereby granted, free of charge, to any person obtaining a copy
12+
of this software and associated documentation files (the "Software"), to deal
13+
in the Software without restriction, including without limitation the rights
14+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15+
copies of the Software, and to permit persons to whom the Software is
16+
furnished to do so, subject to the following conditions:
17+
The above copyright notice and this permission notice shall be included in all
18+
copies or substantial portions of the Software.
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
SOFTWARE.
26+
***************************************************************************/
27+
USE YourDB --change this
28+
GO
29+
30+
SELECT [table_name] = s.[name] +'.'+t.[name]
31+
,[index_name] = i.[NAME]
32+
,index_type_desc
33+
,[avg_fragmentation_in_percent] = ROUND(avg_fragmentation_in_percent,2)
34+
,[table_record_count] = record_count
35+
FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, 'SAMPLED') ips
36+
INNER JOIN sys.tables t on t.[object_id] = ips.[object_id]
37+
INNER JOIN sys.schemas s on t.[schema_id] = s.[schema_id]
38+
INNER JOIN sys.indexes i ON (ips.object_id = i.object_id) AND (ips.index_id = i.index_id)
39+
ORDER BY avg_fragmentation_in_percent DESC
40+

0 commit comments

Comments
 (0)