Advertisement
Ruslan_Rayanov

Untitled

Oct 19th, 2022
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. -- show db tables with big row count
  2. SELECT top 10 QUOTENAME(SCHEMA_NAME(sOBJ.schema_id)) + '.' + QUOTENAME(sOBJ.name) AS [TableName]
  3. , SUM(sPTN.Rows) AS [RowCount]
  4. FROM sys.objects AS sOBJ INNER JOIN sys.partitions AS sPTN
  5. ON sOBJ.object_id = sPTN.object_id
  6. WHERE sOBJ.type = 'U'
  7. AND sOBJ.is_ms_shipped = 0x0 AND not(index_id >= 2)
  8. GROUP BY sOBJ.schema_id, sOBJ.name
  9. ORDER BY [RowCount] desc
  10.  
  11. --- db tables with MB
  12.  
  13. SELECT top 10
  14. CAST(ROUND(((SUM(a.total_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS TotalSpaceMB,
  15. t.NAME AS TableName,
  16. --s.Name AS SchemaName,
  17. p.rows,
  18. --SUM(a.total_pages) * 8 AS TotalSpaceKB,
  19. --SUM(a.used_pages) * 8 AS UsedSpaceKB,
  20. CAST(ROUND(((SUM(a.used_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS UsedSpaceMB,
  21. --(SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB,
  22. CAST(ROUND(((SUM(a.total_pages) - SUM(a.used_pages)) * 8) / 1024.00, 2) AS NUMERIC(36, 2)) AS UnusedSpaceMB
  23. FROM
  24. sys.tables t
  25. INNER JOIN
  26. sys.indexes i ON t.OBJECT_ID = i.object_id
  27. INNER JOIN
  28. sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
  29. INNER JOIN
  30. sys.allocation_units a ON p.partition_id = a.container_id
  31. LEFT OUTER JOIN
  32. sys.schemas s ON t.schema_id = s.schema_id
  33. WHERE
  34. t.NAME NOT LIKE 'dt%'
  35. AND t.is_ms_shipped = 0
  36. AND i.OBJECT_ID > 255
  37. GROUP BY
  38. t.Name, s.Name, p.Rows
  39. ORDER BY
  40. TotalSpaceMB DESC, t.Name
  41.  
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement