2016-07-16 25 views
1

的在下面的代碼:差異的邏輯編號讀取組類似的數據

create table t(i int,j char(3000)) 
create table t1(i int,j char(3000)) 

create unique clustered index ixt on t(i) with (FILLFACTOR=20) 

declare @n int = 0 
while @n < 1000 
begin 
    insert into t values(@n*2,'a') 
    insert into t1 values(@n*2,'a') 
    set @n = @n+1 
end 

create unique clustered index ixt1 on t1(i) with (FILLFACTOR=20) 

上述兩個表的有相同的結構,數據類型和甚至相同的數據,但查詢他們給我不同邏輯讀取..

select * from t where i between 100 and 150 --returns 16 logical reads 
select * from t1 where i between 100 and 150 --returns 30 logical reads 

誰能告訴我爲什麼在table t1返回查詢比table t查詢更多的邏輯讀取?

回答

5

這是因爲要創建索引的方式(創造一流,爲一個表和最後一個表),並用不同的填充因子設置..因爲我們知道

填充因子,決定了有多少自由空間保留在指數的葉級頁,在這種情況下,你是要求設置20,這意味着,離開的免費80%,其餘..

強調需要:
Fill factor setting才被接受索引重建或創建時

第一種情況:
即使您創建了20的填充因子的指標,沒有數據,所以自由空間不剩,我們知道,同時插入此設置不兌現。

查詢表1頁顯示,只有500行

select object_name(object_id),index_depth,index_level,avg_fragmentation_in_percent,avg_page_space_used_in_percent,page_count from 
sys.dm_db_index_physical_stats(db_id(),0,-1,0,'Detailed') 
where object_id=object_id('t') 

enter image description here

第二種情況:
你正在創建一個索引數據插入SQL將信守設置,頁面會後重新排列以履行此設置。

表2頁數給我們1000行..

select object_name(object_id),index_depth,index_level,avg_fragmentation_in_percent,avg_page_space_used_in_percent,page_count from 
sys.dm_db_index_physical_stats(db_id(),0,-1,0,'Detailed') 
where object_id=object_id('t1') 

enter image description here

即使臺數是相同的,頁數是不同的,由於要創建index.This的辦法就是爲什麼你看到不同的原因有相同的數據和每個表邏輯讀取結構

如果在重建兩個索引後查詢,您將看到相同的邏輯讀數