SQL 2005 - 查找具有最多行的表的查询

时间:2009-08-19 13:27:01

标签: sql sql-server tsql

我搜索了一段时间但是空了......希望这里有人可以提供帮助。

我是否可以在数据库(SQL Server 2005)上运行查询,该查询将返回每个表中的行数?

7 个答案:

答案 0 :(得分:7)

您可以尝试使用this

SELECT 
    [TableName] = so.name, 
    [RowCount] = MAX(si.rows) 
FROM 
    sysobjects so, 
    sysindexes si 
WHERE 
    so.xtype = 'U' 
    AND 
    si.id = OBJECT_ID(so.name) 
GROUP BY 
    so.name 
ORDER BY 
    2 DESC

答案 1 :(得分:4)

Galwegian几乎是正确的:-)对于SQL Server 2005及以上版本,我总是建议使用“sys。*”系统视图而不是(很快被弃用)sysobjects和sysindexes表。

SELECT 
    t.NAME AS 'Table Name',
    SUM(p.[Rows]) as 'Row Count'
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
WHERE 
    t.NAME NOT LIKE 'dt%' AND
    i.OBJECT_ID > 255 AND   
    i.index_id = 1
GROUP BY 
    t.NAME 
ORDER BY 
    SUM(p.[Rows]) DESC

马克

答案 2 :(得分:1)

这是我使用dm_db_partition_stats DMV的版本:

SELECT object_name(object_id) as table_name,
    SUM (row_count) AS total_number_of_rows 
FROM sys.dm_db_partition_stats
WHERE   (index_id=0 or index_id=1)
group by object_name(object_id)
order by SUM (row_count) desc

答案 3 :(得分:0)

答案 4 :(得分:0)

我不记得我在哪里得到这个,我很确定我没有写它,这肯定是矫枉过正但它仍然很有趣。

USE master
GO

SET NOCOUNT ON

DECLARE @EnumDBs TABLE ([id] INT IDENTITY, [sql] VARCHAR(1024), PRIMARY KEY ([id]))
CREATE TABLE #EnumTables ([id] INT IDENTITY, [db] SYSNAME, [owner] SYSNAME, [table] SYSNAME, PRIMARY KEY ([db], [owner], [table], [id]))
CREATE TABLE #TableMetrics ([id] INT IDENTITY, [db] SYSNAME NULL, [table] SYSNAME, [rows] INT, [reserved] VARCHAR(256), [data] VARCHAR(256), [index_size] VARCHAR(256), [unused] VARCHAR(256), PRIMARY KEY ([table], [id]))

DECLARE @i INT
DECLARE @sql VARCHAR(1024)
DECLARE @db SYSNAME

INSERT INTO @EnumDBs
SELECT '    USE ['+name+']
        DBCC UPDATEUSAGE(0) WITH NO_INFOMSGS
        INSERT INTO #EnumTables
        SELECT  '''+name+''' AS [db]
                , u.name AS [owner]
                , o.name AS [table]
        FROM sysobjects AS o
        JOIN sysusers AS u
        ON o.uid = u.uid
        WHERE type = ''U''
        ORDER BY    u.name
                , o.name' AS [sql]
FROM master.dbo.sysdatabases WITH (NOLOCK)
WHERE dbid > 4

SELECT @i=COUNT(*)
    , @sql=''
FROM @EnumDBs
WHILE @i > 0
BEGIN

    SELECT @sql = SQL
    FROM @EnumDBs
    WHERE ID = @i

    IF @@ROWCOUNT > 0
        EXEC(@sql)

    IF (@@ERROR <> 0) GOTO ERR_HANDLER

    SET @i=@i-1
END

SELECT @i=COUNT(*)
    , @sql=''
FROM #EnumTables

WHILE @i > 0
BEGIN

    SELECT @db=[db]
        , @sql='USE ['+[db]+'] EXEC SP_SPACEUSED ''['+[db]+'].['+[owner]+'].['+[table]+']'''
    FROM #EnumTables
    WHERE ID = @i

    --PRINT @SQL
    INSERT INTO #TableMetrics
    ([table], [rows], [reserved], [data], [index_size], [unused])
    EXEC(@sql)
    IF (@@ERROR <> 0) GOTO ERR_HANDLER

    UPDATE #TableMetrics
        SET [db]=@db
    WHERE [db] IS NULL
    IF (@@ERROR <> 0) GOTO ERR_HANDLER

    SET @i=@i-1
END

SELECT * FROM #TableMetrics ORDER BY [db], CAST(REPLACE([reserved],' KB','') AS INT) DESC

ERR_HANDLER:
DROP TABLE #EnumTables
DROP TABLE #TableMetrics

答案 5 :(得分:0)

试试这个:

创建表格:

create table maxRows (tablename varchar(900) primary key,CountOf int)

运行此命令并复制输出:

EXEC sp_msforeachtable 'print ''INSERT INTO maxRows SELECT ''''?'''', COUNT(*) FROM ? WITH (NOLOCK)'''

运行复制的输出,它会在表中插入每个表的行计数

现在运行它,列出带有行数的表:

select countOf,tablename from maxRows order by CountOf DESC

答案 6 :(得分:0)

在数据库中运行时,以下内容返回表行计数和索引/总空间使用情况。

declare @tableName  varchar(100);

create table
    #table
    (
        tablename   sysname,
        row_count   int,
        reserved    varchar(50),
        data        varchar(50),
        index_size  varchar(50),
        unused      varchar(50)
    );

create table
    #table2
    (
        tableName   varchar(100),
        row_count   int,
        data_size   int,
        index_size  int,
        reserved    int,
        unused      int
    );

declare C_T cursor fast_forward for
    select      TableName = '[' + s.name + '].[' + t.name + ']'
    from        sys.tables t
                inner join sys.schemas s on t.schema_id = s.schema_id
    where       t.type = 'U'
    order by    s.name,
                t.name;

open    C_T;

fetch next from C_T into @tableName;

while   @@FETCH_STATUS = 0
    begin
        truncate table  #table;

        insert into #table
        exec        sp_spaceused @tableName, false;

        insert into #table2(tableName, row_count, data_size, index_size, reserved, unused)
        select      @tableName,
                    row_count,
                    convert(int, replace(data, ' KB', '')),
                    convert(int, replace(index_size, ' KB', '')),
                    convert(int, replace(reserved, ' KB', '')),
                    convert(int, replace(unused, ' KB', ''))
        from        #table;

        fetch next from C_T into @tableName;
    end

close       C_T;
deallocate  C_T;

select      [Table Name] = tableName,
            [Rows] = row_count,
            [Data Size (KB)] = data_size,
            [Indexes Size (KB)] = index_size,
            [Reserved Space (KB)] = reserved,
            [Unused Space (KB)] = unused
from        #table2
order by    data_size desc;

drop table      #table;
drop table      #table2;