1.獲取所有數據庫名
--SELECT Name FROM Master..SysDatabases ORDER BY Name --
2.獲取所有表名: --SELECT Name NAMEtemp,* FROM TEST..SysObjects Where XType='U' ORDER BY Name --表名 ----XType='U':表示所有用戶表; ----XType='S':表示所有系統表;
3.獲取所有字段名:
SELECT Name FROM SysColumns WHERE id=Object_Id('TableName')
4.查詢所有表行數 ----SELECT * FROM TEST..sysindexes --表明細
select a.name, b.rows --查詢所有表行數/TEST為庫名稱 from TEST..sysobjects a inner join TEST..sysindexes b on a.id = b.id where a.type = 'u' and b.indid in (0, 1) order by a.name
【 declare @N int set @N=10000
--通過查詢索引表的聚集索引查詢 (indid=1,這種是體現數據是以聚集索引方式存儲體現的查詢) select object_name(id),rowcnt from sysindexes where rowcnt>=@N and indid=1 and objectproperty(id,'isusertable')=1 --通過查詢索引表的無聚集索引查詢 (indid=0,這種是體現數據是以數據堆方式存儲體現的查詢,無聚集索引的表) select object_name(id),rowcnt from sysindexes where rowcnt>=@N and indid=0 and objectproperty(id,'isusertable')=1 有人問indid是什么意思?
select object_name(id),rowcnt from sysindexes where rowcnt>=@N and indid<=1 and objectproperty(id,'isusertable')=1 即可),非聚集索引則不一定 必須。 indid=1 查詢的就是根據聚集索引鍵值來查詢sysindexs,也就是說不存在聚集索引的表,通過上面的查詢是查不出來的。 而indid>1(如indid=2、indid=3等) 則代表查詢的是根據非聚集索引序號,但沒有聚集索引的表還是大量存在的(這不比
當然此查詢可能有誤差,在索引碎片的產生和清理過程確實會存在,只要索引定期維護、重建,那么誤差應該不大,甚至不存在誤差了
--objectproperty:
http://technet.microsoft.com/zh-cn/library/ms176105.aspx
sysindexes詳細內容
http://technet.microsoft.com/zh-cn/library/ms190283.aspx
數據庫中的每個索引和表在表中各占一行。該表存儲在每個數據庫中。
列名 | 數據類型 | 描述 |
---|---|---|
id | int | 表 ID(如果 indid = 0 或 255)。否則為索引所屬表的 ID。 |
status | int | 內部系統狀態信息。 |
first | binary(6) | 指向第一頁或根頁的指針。 |
indid | smallint | 索引 ID: 1 = 聚集索引 >1 = 非聚集 255 = 具有 text 或 image 數據的表條目 |
root | binary(6) | 如果 indid >= 1 和 < 255,root 是指向根頁的指針。如果indid = 0 或 indid = 255,root 是指向最后一頁的指針。 |
minlen | smallint | 最小行大小。 |
keycnt | smallint | 鍵的數目。 |
groupid | smallint | 在其上創建對象的文件組 ID。 |
dpages | int | 如果 indid = 0 或 indid = 1,dpages 是已用數據頁的計數。如果 indid = 255,其設置為 0。否則是已用索引頁的計數。 |
reserved | int | 如果 indid = 0 或 indid = 1,reserved 是分配給所有索引和表數據的頁計數。如果 indid = 255,reserved 是分配給text 或 image 數據的頁計數。否則是分配給索引的頁計數。 |
used | int | 如果 indid = 0 或 indid = 1,used 是用於所有索引和表數據的總頁數。如果 indid = 255,used 是用於 text 或image 數據的頁計數。否則是用於索引的頁計數。 |
rowcnt | bigint | 基於 indid = 0 和 indid = 1 的數據級行計數。如果 indid= 255,rowcnt 設置為 0。 |
rowmodctr | int | 對自上次更新表的統計后插入、刪除或更新行的總數進行計數。 |
xmaxlen | smallint | 最大行大小。 |
maxirow | smallint | 最大非葉索引行大小。 |
OrigFillFactor | tinyint | 創建索引時使用的起始填充因子值。不保留該值;然而,如果需要重新創建索引但記不住當初使用的填充因子,則該值可能很有幫助。 |
reserved1 | tinyint | 保留。 |
reserved2 | int | 保留。 |
FirstIAM | binary(6) | 保留。 |
impid | smallint | 保留。索引實現標志。 |
lockflags | smallint | 用於約束經過考慮的索引鎖粒度。例如,對於本質上是只讀的查找表,可以將其設置為僅進行表級鎖定以使鎖定成本減到最小。 |
pgmodctr | int | 保留。 |
keys | varbinary(816) | 組成索引鍵的列 ID 列表。 |
name | sysname | 表名(如果 indid = 0 或 255)。否則為索引的名稱。 |
statblob | image | 統計 BLOB。 |
maxlen | int | 保留。 |
rows | int | 基於 indid = 0 和 indid = 1的數據級行數,該值對於 indid>1 重復。如果 indid = 255,rows 設置為 0。提供該列是為了向后兼容。 |