Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 57 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
57
Dung lượng
598,5 KB
Nội dung
Index Fragmentation Paul Randal Dev Lead, Microsoft SQL Server Introduction Structural details of an index How these structures are used Index fragmentation SQL Server 2000 DBCC SHOWCONTIG SQL Server 2000 DBCC INDEXDEFRAG "Yukon" dm_db_index_physical_stats "Yukon" ALTER INDEX Records (1) How is data stored in SQL Server? Example record types Data Index Text Ghost data/index Records (2) ‘Inside SQL Server 2000’ has the details Things to note: Non-leaf index records contain a child page Id in every record All non-clustered index leaf records contain a base table RID “Yukon” allows INCLUDE’d columns in an index record Pages (1) How are records stored? Example page types Data page Index page Allocation maps (GAM, SGAM, IAM) pages PFS page Pages (2) Layout is the same for all page types 8k 96 byte header slot array records free space Extents How are pages grouped? Extents are: A group of 8 contiguous pages Start on an 8 page boundary Tracked in IAM, GAM, SGAM pages 0 1 2 3 54 6 7 Heap (1) Simplest storage arrangement Comprised entirely of data pages Unordered, pages unlinked Records are located using physical RID create table foo ( first char(100), last char (100), city char (100)) Clustered index (1) Alternative table structure to heap Data stored in defined order Fast lookup through B-tree Records located through logical RID create clustered index foo_c on foo (city) Clustered index (2) index tree pages P L L P L L P L L R data pages [...]... the index manually Rebuild the index - DBCC DBREINDEX Defrag the index - DBCC INDEXDEFRAG Most people rebuild or defrag Drop/create index Pros Split the operation in two – shorter rollback Can tweak schema and fillfactor Can use multiple CPUs during create Cons Non-atomic – can cause constraint problems Dropping a clustered index causes all non-clustered indexes to be rebuilt Need... Singleton lookup Range scan Allocation order scan Allows skipping of sort step in query Reduces amount of data to apply predicates to Singleton lookup Matching record Range scan Allocation-order scan 3 1 5 2 4 6 Side note: merry-go-rounds Why don’t I get the index order when I do ‘select *’? DATA scan 1 scan 2 scan 3 scan 1 starts scan 2 starts scan 3 starts Readahead Why use readahead? (1) Keep the...Non-clustered index (1) A way to provide a different ordering Define on heaps or clustered indexes Leaf records contain RID of matching record in base table create index foo_nc on foo (last) create index foo_nc on foo (last) include (first) Non-clustered index (2) R index tree pages P L P L L P L L L index leaf pages Why use an... IOs Better contiguity = bigger IOs Readahead L (2) What causes fragmentation Index leaf level of newly built index Red arrow is the allocation order Black arrows are following the logical order (1) What causes fragmentation Newly built index leaf after a single page split Red arrow is the allocation order Black arrows are following the logical order (2) What causes fragmentation Index leaf level after... table lock for non-clustered index, X table lock for clustered index DBCC DBREINDEX Pros Atomic operation No knowledge of schema or constraints required Can use multiple CPUs Rebuilds statistics – possible dramatic perf gain Cons Atomic operation – potentially long rollback on interrupt Requires creating a new index before dropping the old Offline – S table lock for non-clustered index,... mins 5.6 x faster Default 111,626,354 WITH FAST 111,626,354 382.35 mins 48.73 mins 7.8 x faster Fragmentation calculation R P L P L L P L L L (1) Fragmentation calculation Reads parent level (2) Parent pages contain child page IDs in logical order Reduces number of pages to read by a factor of the fanout Tracks multiple files simultaneously A page is fragmented if its ID is less than the logically . types Data Index Text Ghost data/index Records (2) ‘Inside SQL Server 2000’ has the details Things to note: Non-leaf index records contain a child page Id in every record All non-clustered index leaf records. lookup through B-tree Records located through logical RID create clustered index foo_c on foo (city) Clustered index (2) index tree pages P L L P L L P L L R data pages Non-clustered index. predicates to Singleton lookup Matching record Range scan Allocation-order scan 1 23 45 6 Side note: merry-go-rounds Why don’t I get the index order when I do ‘select *’? D A T A scan