so, i just customized glenn and brent ozar's unused/bad indexes script . Thats where i got the data
IF OBJECT_ID(N'tempdb..#tmp') IS NOT NULL
BEGIN
DROP TABLE #tmp
END
declare @a varchar(30); --machine restart date
declare @b varchar(30); --sql server restart date
declare @c varchar(30); --no of days since restart
SELECT @a=convert(varchar(30),Dateadd(s, ( ( -1 ) * ( osd.[ms_ticks] / 1000 ) ) , Getdate()),109),
@b=convert(varchar(30),osd.sqlserver_start_time,109)
FROM sys.[dm_os_sys_info] osd;
select @c =RTRIM(CONVERT(CHAR(3),DATEDIFF(second,login_time,getdate())/86400)) + ':' +
RIGHT('00'+RTRIM(CONVERT(CHAR(2),DATEDIFF(second,login_time,getdate())%86400/3600)),2) + ':' +
RIGHT('00'+RTRIM(CONVERT(CHAR(2),DATEDIFF(second,login_time,getdate())%86400%3600/60)),2) + ':' +
RIGHT('00'+RTRIM(CONVERT(CHAR(2),DATEDIFF(second,login_time,getdate())%86400%3600%60)),2)
from sys.sysprocesses --sysprocesses for SQL versions <2000
where spid = 1
-- glenn berry
-- Possible Bad NC Indexes (writes > reads) (Query 67) (Bad NC Indexes)
-- Look for indexes with high numbers of writes and zero or very low numbers of reads
-- Consider your complete workload, and how long your instance has been running
-- Investigate further before dropping an index!
-- consider rowcount > 10000
SELECT
@a AS Machinerestartdatetime,
@b as sqlserver_start_time,
@c AS [Days:Hours:Minutes:Seconds],
getdate() as CaptureDt,
@@servername as ServerName,
DB_NAME() AS DatabaseName,
SCHEMA_NAME(o.schema_id) +'.'+OBJECT_NAME(s.OBJECT_ID) AS TableName,
--SCHEMA_NAME(o.[schema_id]) AS [Schema Name],
--OBJECT_NAME(s.[object_id]) AS [Table Name],
i.index_id,
i.name AS [Index Name],
i.[type_desc],
s.user_seeks + s.user_scans + s.user_lookups AS [Total Reads],
s.user_updates AS [Total Writes],
s.user_updates - (s.user_seeks + s.user_scans + s.user_lookups) AS [Difference],
[Row Count] = (SELECT SUM(p.rows) FROM sys.partitions p WHERE p.index_id = s.index_id AND s.object_id = p.object_id)
, CASE
WHEN s.user_updates < 1 THEN 100
ELSE 1.00 * (s.user_seeks + s.user_scans + s.user_lookups) / s.user_updates
END AS reads_per_write -- The Reads_per_write should be atleast > 50%
,CASE WHEN ps.usedpages > ps.pages THEN (ps.usedpages - ps.pages) ELSE 0
END * 8 / 1024 AS IndexSizeMB
,s.last_user_seek AS LastSeek
,s.last_user_scan AS LastScan
,s.last_user_lookup AS LastLookup
,s.last_user_update AS LastUpdate
,i.is_disabled,
--i.is_hypothetical,
i.fill_factor,
i.has_filter,
'ALTER INDEX '+i.name+' ON '+SCHEMA_NAME(o.schema_id)+'.'+OBJECT_NAME(s.OBJECT_ID)+' DISABLE;' as 'disable statement',
'DROP INDEX ' + QUOTENAME(i.name)+' ON '+QUOTENAME(SCHEMA_NAME(o.schema_id))+'.'+QUOTENAME(OBJECT_NAME(s.object_id))+';' as 'drop statement'
into #tmp
FROM sys.dm_db_index_usage_stats AS s WITH (NOLOCK)
INNER JOIN sys.indexes AS i WITH (NOLOCK)
ON s.[object_id] = i.[object_id]
AND i.index_id = s.index_id
INNER JOIN sys.objects AS o WITH (NOLOCK)
ON i.[object_id] = o.[object_id]
LEFT JOIN (SELECT OBJECT_ID, index_id, SUM(used_page_count) AS usedpages,
SUM(CASE WHEN (index_id < 2)
THEN (in_row_data_page_count + lob_used_page_count + row_overflow_used_page_count)
ELSE lob_used_page_count + row_overflow_used_page_count
END) AS pages
FROM sys.dm_db_partition_stats
GROUP BY object_id, index_id) AS ps ON i.object_id = ps.object_id AND i.index_id = ps.index_id
WHERE OBJECTPROPERTY(s.[object_id],'IsUserTable') = 1
AND s.database_id = DB_ID()
AND s.user_updates > (s.user_seeks + s.user_scans + s.user_lookups)
AND i.index_id > 1 AND i.[type_desc] = N'NONCLUSTERED'
AND i.is_primary_key = 0 --do not check primary keys
AND i.is_unique_constraint = 0 --do not check unique constraints
AND i.is_unique = 0 --do not check unique indexes
--AND (ius.user_seeks+ius.user_scans+ius.user_lookups) < 1 --only return unused indexes
--AND OBJECT_NAME(i.OBJECT_ID) = 'tableName'--only check indexes on specified table
--AND i.name = 'IX_Your_Index_Name' --only check a specified index
--ORDER BY [Total Reads] ASC, [Total Writes] DESC OPTION (RECOMPILE);
select
[Machinerestartdatetime],
[sqlserver_start_time],
[Days:Hours:Minutes:Seconds] as [dd hh:mi:ss],--since how many days sql restarted
[CaptureDt],
[ServerName],
[DatabaseName],
[TableName],
[index_id],
[Index Name],
[type_desc],
[Total Reads],
[Total Writes],
[Difference],
[Row Count],
--[reads_per_write],
cast(round([reads_per_write],0,2) as numeric(36))
[IndexSizeMB],
[LastSeek],
[LastScan],
[LastLookup],
[LastUpdate],
[is_disabled],
[fill_factor],
[has_filter],
[disable statement],
[drop statement] --into sqldba_utils..cmx_ors_index_usage_stats_Aug112023
from #tmp
ORDER BY [Total Reads] ASC, [Total Writes] DESC OPTION (RECOMPILE);