WEB开发网
开发学院数据库MSSQL Server 表无索引为什么sp_spaceused 中的index_size不为0... 阅读

表无索引为什么sp_spaceused 中的index_size不为0

 2009-10-19 00:00:00 来源:WEB开发网   
核心提示:源于csdn论坛的一个提问:CREATETABLETUser(FNameCHAR(8000),FAgeINT,FSexbit)INSERTINTOTUserSELECT'张三',18,1UNIONALLSELECT'李四',20,1UNIONALLSELECT'王五',3

源于csdn论坛的一个提问:

CREATE TABLE TUser ( FName CHAR(8000), FAge INT, FSex bit )
INSERT INTO TUser
SELECT '张三',18,1 UNION ALL
SELECT '李四',20,1 UNION ALL
SELECT '王五',32,1 UNION ALL
SELECT '麻子',23,1

通过一个查询看下扫描的数据页:

SET STATISTICS IO ON
SELECT * FROM TUser
/**//*
(4 行受影响)
表 'TUser'。扫描计数 1,逻辑读取 4 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
*/

可以看到,该查询全部数据是扫了四个数据页,也就是说插入的四行数据,一行为一个page.

但是我们执行下面的sql,发现index_size为8k:

EXEC sp_spaceused N'TUser'
/**//*
name    rows        reserved    data    index_size    unused
TUser    4              40 KB        32 KB    8 KB        0 KB
*/

这是为什么,为什么没有建索引,这里却有一个index_size 8k ?

下面来看看index_size是怎么来的?

首先想到是的

sp_helptext sp_spaceused

通过查看sp_spaceused的代码,我们找到对于我们这个查询有用的信息代码:

 /**//*  
 ** Now calculate the summary data.   
 *  Note that LOB Data and Row-overflow Data are counted as Data Pages.  
 */  
 SELECT   
  @reservedpages = SUM (reserved_page_count),  
  @usedpages = SUM (used_page_count),  
  @pages = SUM (  
   CASE  
    WHEN (index_id < 2) THEN (in_row_data_page_count + lob_used_page_count + row_overflow_used_page_count)  
    ELSE lob_used_page_count + row_overflow_used_page_count  
   END  
   ),  
  @rowCount = SUM (  
   CASE  
    WHEN (index_id < 2) THEN row_count  
    ELSE 0  
   END  
   )  
 FROM sys.dm_db_partition_stats  
 WHERE object_id = @id;  
  
 /**//*  
 ** Check if table has XML Indexes or Fulltext Indexes which use internal tables tied to this table  
 */  
 IF (SELECT count(*) FROM sys.internal_tables WHERE parent_id = @id AND internal_type IN (202,204)) > 0   
 BEGIN  
  /**//*  
  **  Now calculate the summary data. Row counts in these internal tables don't   
  **  contribute towards row count of original table.    
  */  
  SELECT   
   @reservedpages = @reservedpages + sum(reserved_page_count),  
   @usedpages = @usedpages + sum(used_page_count)  
  FROM sys.dm_db_partition_stats p, sys.internal_tables it  
  WHERE it.parent_id = @id AND it.internal_type IN (202,204) AND p.object_id = it.object_id;  
 END  
  
 SELECT   
  name = OBJECT_NAME (@id),  
  rows = convert (char(11), @rowCount),  
  reserved = LTRIM (STR (@reservedpages * 8, 15, 0) + ' KB'),  
  data = LTRIM (STR (@pages * 8, 15, 0) + ' KB'),  
  index_size = LTRIM (STR ((CASE WHEN @usedpages > @pages THEN (@usedpages - @pages) ELSE 0 END) * 8, 15, 0) + ' KB'),  
  unused = LTRIM (STR ((CASE WHEN @reservedpages > @usedpages THEN (@reservedpages - @usedpages) ELSE 0 END) * 8, 15, 0) + ' KB') 

1 2  下一页

Tags:索引 为什么 sp

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接