因为标题说我需要一个查询来查找包含数据库中大多数行的表。
我可以使用此查询显示所有表格:
select * from sys.tables
或者:
select *
from sysobjects
where xtype = 'U'
order by name
此查询的所有索引:
select *
from sys.indexes
但是如何显示整个数据库中包含大多数行的列?
亲切的问候,克里斯
答案 0 :(得分:4)
我通常使用此查询按rowcount对所有表进行排序:
USE DATABASENAME
SELECT t.NAME AS TableName, SUM(p.rows) AS RowCounts
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, i.object_id, i.index_id, i.name
ORDER BY SUM(p.rows) desc
如果您只想在TOP 1
SELECT
- 回复你的评论----
WHERE
t.NAME NOT LIKE 'dt%' AND --exclude Database Diagram tables like dtProperties
i.OBJECT_ID > 255 AND --exclude system-level tables
i.index_id <= 1 -- avoid non clustered index
答案 1 :(得分:1)
使用this question的答案,您可以运行以下命令查看行数最多的表:
CREATE TABLE #counts
(
table_name varchar(255),
row_count int
)
EXEC sp_MSForEachTable @command1='INSERT #counts (table_name, row_count) SELECT ''?'', COUNT(*) FROM ?'
SELECT TOP 1 table_name, row_count FROM #counts ORDER BY row_count DESC
DROP TABLE #counts
答案 2 :(得分:0)
您也可以使用此查询在表名中不重复:
Use[DataBase_Name]
select max(i.rows) as recordCounts,t.name as table_name,
SCHEMA_NAME(t.schema_id) as schema_name from sysindexes i
inner join sys.tables t
on i.id=object_id
group by t.name, SCHEMA_NAME(t.schema_id)