简体   繁体   English

从过去 6 个月的所有数据库中获取所有表的大小

[英]Get all table size from all database from last 6 months

I'm trying to get size of all tables and size should be shown from last 6 months data below is the query which I'm using to get the results.我正在尝试获取所有表的大小,并且应该从最近 6 个月的数据中显示大小,下面是我用来获取结果的查询。

Note: Below query shows size of all tables since they got created注意:下面的查询显示了所有表自创建以来的大小

SELECT 
    t.NAME AS TableName,
    s.Name AS SchemaName,
    p.rows,
    SUM(a.total_pages) * 8 AS TotalSpaceKB, 
    CAST(ROUND(((SUM(a.total_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS TotalSpaceMB,
    SUM(a.used_pages) * 8 AS UsedSpaceKB, 
    CAST(ROUND(((SUM(a.used_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS UsedSpaceMB, 
    (SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB,
    CAST(ROUND(((SUM(a.total_pages) - SUM(a.used_pages)) * 8) / 1024.00, 2) AS NUMERIC(36, 2)) AS UnusedSpaceMB 
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 
    TotalSpaceMB DESC, t.Name

Below is the query which I customized and tried to get results from the last 6 months but it gives size of all tables since they got created.下面是我自定义的查询,并试图从过去 6 个月中获取结果,但它给出了自创建以来所有表的大小。 If someone can suggest me how I can modify it in a way it only shows UsedSpaceMB from last 6 months如果有人可以建议我如何以仅显示过去 6 个月的 UsedSpaceMB 的方式修改它

SELECT 
    t.NAME AS TableName,
    s.Name AS SchemaName,
    p.rows,
    CAST(ROUND(((SUM(a.total_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS TotalSpaceMB,
    CAST(ROUND(((SUM(a.used_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS UsedSpaceMB,
    CAST(ROUND(((SUM(a.total_pages) - SUM(a.used_pages)) * 8) / 1024.00, 2) AS NUMERIC(36, 2)) AS UnusedSpaceMB,
    t.modify_date AS ModifyDate
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.modify_date >= Dateadd(Month, Datediff(Month, 0, DATEADD(m, -6, current_timestamp)), 0)
    AND t.NAME NOT LIKE 'dt%' 
    AND t.is_ms_shipped = 0
    AND i.OBJECT_ID > 255 
GROUP BY 
    t.Name, s.Name, p.Rows, t.modify_date
ORDER BY 
    TotalSpaceMB DESC, t.Name, t.modify_date;

The DATEADD() function returns a date time (smalldatetime). DATEADD() function 返回日期时间 (smalldatetime)。 Its syntax is,它的语法是,

DATEDADD(interval, increment (int), expression (date))

sample query in you case:您的示例查询:

t.modify_date >= DATEADD(MONTH, -6, GETDATE())

I applied above query on sys.objects to filter objects with modified date is less than last 6 months It will return all the objects where modified date is from last 6 months even the create_date is greater.我在sys.objects上应用了上面的查询来过滤修改日期小于过去 6 个月的对象它将返回修改日期从过去 6 个月开始的所有对象,即使create_date更大。 before applying filter there was 182 records after filtering it reduced to 92.在应用过滤器之前有 182 条记录,过滤后减少到 92 条。

在此处输入图像描述

I customized and tried to get results from the last 6 months but it gives size of all tables since they got created.我自定义并尝试获取过去 6 个月的结果,但它给出了自创建以来所有表的大小。

if you want tables only created in last 6 month then you have to apply filter on create_date .如果您只想在过去 6 个月内创建表,则必须在create_date上应用过滤器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM