Skip to content

Instantly share code, notes, and snippets.

@PRElias
Created February 19, 2019 18:36
Show Gist options
  • Save PRElias/20bc01628297347db4a38de42983a9aa to your computer and use it in GitHub Desktop.
Save PRElias/20bc01628297347db4a38de42983a9aa to your computer and use it in GitHub Desktop.
SQL Server útil
--Maiores tabelas
SELECT
t.NAME AS Entidade,
p.rows AS Registros,
SUM(a.total_pages) * 8 AS EspacoTotalKB,
SUM(a.used_pages) * 8 AS EspacoUsadoKB,
(SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS EspacoNaoUsadoKB
FROM
sys.tables t
INNER JOIN
sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN
sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN
sys.allocation_units a ON p.partition_id = a.container_id
LEFT OUTER JOIN
sys.schemas s ON t.schema_id = s.schema_id
WHERE
t.NAME NOT LIKE 'dt%'
AND t.is_ms_shipped = 0
AND i.OBJECT_ID > 255
GROUP BY
t.Name, s.Name, p.Rows
ORDER BY
Registros DESC
--Tabelas sem índices
SELECT
schemaname = OBJECT_SCHEMA_NAME(o.object_id)
,tablename = o.NAME
FROM sys.objects o
INNER JOIN sys.indexes i ON i.OBJECT_ID = o.OBJECT_ID
-- tables that are heaps without any nonclustered indexes
WHERE (
o.type = 'U'
AND o.OBJECT_ID NOT IN (
SELECT OBJECT_ID
FROM sys.indexes
WHERE index_id > 0
)
)
-- OR
-- table that have a clustered index without any nonclustered indexes
--(o.type='U'
-- AND o.OBJECT_ID NOT IN (
-- SELECT OBJECT_ID
-- FROM sys.indexes
-- WHERE index_id>1))
-- Tabelas sem FK
SELECT tbl.name
FROM sys.tables AS tbl
LEFT JOIN sys.foreign_key_columns AS fKey
ON tbl.object_id = fKey.parent_object_id
LEFT JOIN sys.foreign_key_columns AS rKey
ON tbl.object_id = rKey.referenced_object_id
WHERE fKey.parent_object_id IS NULL
AND rKey.referenced_object_id IS NULL
order by 1;
--Procura nos textos das views
SELECT
m.definition
FROM sys.views v
INNER JOIN sys.sql_modules m ON m.object_id = v.object_id
--WHERE name = 'USER_THIRD'
--Procura nos textos das functions
SELECT name, definition, type_desc
FROM sys.sql_modules m
INNER JOIN sys.objects o
ON m.object_id=o.object_id
WHERE type_desc like '%function%'
and definition like '%calculate_escalation%'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment