0% found this document useful (0 votes)
9 views2 pages

Canivete Suiço Sqlserver

SQL server - Scripts auxiliares

Uploaded by

Maykel Hedlund
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Canivete Suiço Sqlserver

SQL server - Scripts auxiliares

Uploaded by

Maykel Hedlund
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

-->> Identificar consultas lentas <<--

SELECT
qs.creation_time,
qs.execution_count,
qs.total_elapsed_time,
qs.total_elapsed_time/qs.execution_count AS avg_elapsed_time,
SUBSTRING(st.text, (qs.statement_start_offset/2) + 1,
((CASE qs.statement_end_offset
WHEN -1 THEN DATALENGTH(st.text)
ELSE qs.statement_end_offset END
- qs.statement_start_offset)/2) + 1) AS statement_text
FROM
sys.dm_exec_query_stats AS qs
CROSS APPLY
sys.dm_exec_sql_text(qs.sql_handle) AS st
ORDER BY
total_elapsed_time DESC;

-->> Identificar índices ausentes ou não utilizados: <<--


SELECT
s.name AS SchemaName,
t.name AS TableName,
i.name AS IndexName,
i.index_id,
ius.user_seeks,
ius.user_scans,
ius.user_lookups,
ius.user_updates
FROM
sys.indexes AS i
INNER JOIN
sys.objects AS t ON t.object_id = i.object_id
INNER JOIN
sys.schemas AS s ON t.schema_id = s.schema_id
LEFT JOIN
sys.dm_db_index_usage_stats AS ius ON ius.object_id = i.object_id AND
ius.index_id = i.index_id
WHERE
i.type_desc = 'NONCLUSTERED'
AND i.is_primary_key = 0
AND i.is_unique_constraint = 0
AND (ius.user_seeks IS NULL OR ius.user_seeks = 0)
ORDER BY
TableName, IndexName;

/*ANÁLISE DE DESEMPENHO*/
-->> Utilização de espaço em disco por tabela <<--
EXEC sp_MSforeachtable 'EXEC sp_spaceused ''?'''

-->> Análise de fragmentação de índices <<--


SELECT
DB_NAME() AS DatabaseName,
t.name AS TableName,
i.name AS IndexName,
ps.avg_fragmentation_in_percent
FROM
sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, NULL) AS ps
INNER JOIN
sys.tables AS t ON ps.object_id = t.object_id
INNER JOIN
sys.indexes AS i ON ps.object_id = i.object_id AND ps.index_id = i.index_id
WHERE
ps.avg_fragmentation_in_percent > 30
ORDER BY
ps.avg_fragmentation_in_percent DESC;

/*ESTATÍSTICAS DO BANCO DE DADOS*/


-->> Espaço utilizado por cada tabela <<--
EXEC sp_MSforeachtable 'EXEC sp_spaceused ''?'''

-->> Estatísticas do banco de dados <<--


DBCC SHOW_STATISTICS (YourTableName, YourIndexName);

You might also like