Estou escrevendo um script para verificar rapidamente os índices em um determinado banco de dados. Acho que tenho o script escrito ok, mas para remover linhas duplicadas, fiz várias SUM
funções e não tenho certeza se isso está correto ou não:
SELECT OBJECT_NAME(i.object_id) AS 'Table Name'
, COALESCE(i.name, '') AS 'Index Name'
, i.type_desc AS 'Index Type'
, i.is_primary_key AS 'Primary Key'
, i.is_disabled AS 'Index Disabled'
, ROUND(CONVERT(FLOAT,SUM(user_seeks)) / (CONVERT(FLOAT,ISNULL(NULLIF(SUM(user_scans),0),1))),2) AS 'Seeks Per Scan'
, ROUND((CONVERT(FLOAT,SUM(user_seeks) +SUM(user_scans) + SUM(user_lookups))) / (CONVERT(FLOAT,ISNULL(NULLIF(SUM(user_updates),0),1))),2) AS 'Reads Per Write'
, (SUM(user_seeks) + SUM(user_scans) + SUM(user_lookups)) AS 'Reads'
, SUM(user_updates) AS 'Writes'
, SUM(user_seeks) AS 'Seeks'
, SUM(user_scans) AS 'Scan'
, ROUND(AVG(ps.avg_fragmentation_in_percent),2) AS 'Avg Fragmentation %'
, NULLIF((SUM(leaf_insert_count) + SUM(leaf_update_count) + SUM(leaf_delete_count)),0) / NULLIF(SUM(leaf_allocation_count),0) AS 'Leaf Page Splits Per Write'
, NULLIF((SUM(nonleaf_insert_count) + SUM(nonleaf_update_count) + SUM(nonleaf_delete_count)),0) / NULLIF(SUM(nonleaf_allocation_count),0) AS 'Non-Leaf Page Splits Per Write'
, CAST (100.0 * SUM(row_lock_wait_count) / (1 + SUM(row_lock_count)) AS NUMERIC(15,2)) AS 'Block %'
, SUM(row_lock_wait_in_ms) AS 'Row Lock Wait (ms)'
, CAST(1.0 * SUM(row_lock_wait_in_ms) / (1 + SUM(row_lock_wait_count)) AS NUMERIC(15,2)) AS 'Average Row Lock Wait'
FROM sys.indexes i
LEFT JOIN sys.objects o ON o.object_id = i.object_id
LEFT JOIN sys.dm_db_index_operational_stats(NULL, NULL, NULL, NULL) s ON o.object_id = s.object_id AND i.index_id = s.index_id
LEFT JOIN sys.dm_db_index_usage_stats u ON s.object_id = u.object_id AND u.index_id = s.index_id
LEFT JOIN sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) AS ps ON u.index_id = ps.index_id AND u.object_id = ps.object_id
WHERE OBJECTPROPERTY(o.object_id,'IsUserTable') = 1
AND NOT OBJECT_NAME(i.object_id) = 'sysdiagrams' -- exclude sysdiagrams
--AND i.is_primary_key = 0
GROUP BY OBJECT_NAME(i.object_id), COALESCE(i.name, ''),i.type_desc,i.is_primary_key,i.is_disabled
ORDER BY OBJECT_NAME(i.object_id), COALESCE(i.name, '')
tudo bem? Ou SUM
as linhas estão ruins?
Além disso, há alguma informação que você acha que seria útil para encontrar índices que não são usados/precisam de reindexação, etc.
Por que não usar apenas um dos scripts disponíveis gratuitamente. A versão reinante atual da publicada em sqlfool.com .