Clustered Indexes

  • GilaMonster (10/5/2010)


    Craig Farrell (10/5/2010)


    I got it right, but a question regarding dedicated extents. I know page splits will happen, etc etc, but does an extent stay compact for a range of data, or will it split out of the extent and return to it with page pointers?

    Test it out and see (hint DBCC IND and DBCC PAGE)

    Think about it. What happens if all 8 pages in the dedicated extent are full and a row is inserted that has to go onto page 4 of the extent?

    Ah, DBCC IND, thank you, couldn't remember for the life of me. Stupid undocmented... mutter mutter.

    It's going to go cross extent. Gyeah. For some reason I had though the extents shifted, keeping multiple page chains intact... and I check the extent alloc status and it's all over the place. Alright, I have some understanding on extents to do. My experience with them is limited to fighting with LOB data.

    Which begs the question, why the heck are there extents in the first place for anything but LOB data...


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • Craig Farrell (10/5/2010)


    Which begs the question, why the heck are there extents in the first place for anything but LOB data...

    An extent is nothing more than a contiguous 8 pages (64kb) aligned on a 64kb boundary. It has no special structure.

    Why they exist, my guesses:

    Make allocation easier. The GAM and SGAM pages are both 1 bit per extent. DIFF and ML pages as well I believe. If allocation was on a page level, there would be 8 times more allocation pages in the database files. (PFS pages are per-page though)

    Make read-ahead more efficient. Read-ahead requires contiguous pages. So SQL can issue a read-ahead request for pages 36176 - 36208 possibly as a single IO request. To ask for pages 36208, 36210, 36215, 36218, 36220 and 36222 requires6 IO requests. (I think. I need to double check IO basics again)

    Among probably many other reasons.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Great Question.

  • Ok Robert, you made me dig into this one deeper. We see the misconceptions about physical vs. logical ordering of pages in a clustered index, but there also seems to be misconceptions about physical ordering of rows within pages.

    According to BOL

    http://msdn.microsoft.com/en-us/library/ms190639.aspx

    'A clustered index is particularly efficient on columns that are often searched for ranges of values. After the row with the first value is found using the clustered index, rows with subsequent indexed values are guaranteed to be physically adjacent.'

    Digging deeper, though, there is the concept of the row offset table _aka_ slot array at the end of each page. Using the indirection of this table, index order of rows within the page is maintained by the order of the row entries in this table. Entry 0 has the offset of the 1st ordered row, entry 1->2nd ordered row. The values of the offsets in this table don't have to be in order, they just have to point to the correct row start location in the page. As rows are inserted/deleted/updated, the offsets in this table are updated as needed to maintain the sort order.

    Ordered Row - Offset

    4 (0x4) - 177 (0xb1)

    3 (0x3) - 150 (0x96)

    2 (0x2) - 123 (0x7b)

    1 (0x1) - 96 (0x60) offset of 2nd row in sort order

    0 (0x0) - 204 (0xcc) offset of 1st row in sort order (a newly inserted record, 1st in the offset table, but physically at end)

    Paul S. Randal explains this in depth on his site http://www.sqlskills.com

    http://www.sqlskills.com/BLOGS/PAUL/post/Inside-the-Storage-Engine-Proof-that-records-are-not-always-physically-stored-in-index-key-order.aspx

  • really GOOD question..i really got my eye-brow raised while see the answer.:-D

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

  • rtelgenhoff (10/5/2010)


    Ok Robert, you made me dig into this one deeper. We see the misconceptions about physical vs. logical ordering of pages in a clustered index, but there also seems to be misconceptions about physical ordering of rows within pages.

    According to BOL

    http://msdn.microsoft.com/en-us/library/ms190639.aspx

    'A clustered index is particularly efficient on columns that are often searched for ranges of values. After the row with the first value is found using the clustered index, rows with subsequent indexed values are guaranteed to be physically adjacent.'

    Digging deeper, though, there is the concept of the row offset table _aka_ slot array at the end of each page. Using the indirection of this table, index order of rows within the page is maintained by the order of the row entries in this table. Entry 0 has the offset of the 1st ordered row, entry 1->2nd ordered row. The values of the offsets in this table don't have to be in order, they just have to point to the correct row start location in the page. As rows are inserted/deleted/updated, the offsets in this table are updated as needed to maintain the sort order.

    Ordered Row - Offset

    4 (0x4) - 177 (0xb1)

    3 (0x3) - 150 (0x96)

    2 (0x2) - 123 (0x7b)

    1 (0x1) - 96 (0x60) offset of 2nd row in sort order

    0 (0x0) - 204 (0xcc) offset of 1st row in sort order (a newly inserted record, 1st in the offset table, but physically at end)

    Paul S. Randal explains this in depth on his site http://www.sqlskills.com

    http://www.sqlskills.com/BLOGS/PAUL/post/Inside-the-Storage-Engine-Proof-that-records-are-not-always-physically-stored-in-index-key-order.aspx

    Thanks it clears my doubts. 🙂

    Thanks

  • thanks for the nice question

  • :cool::satisfied:Clustered index is newly created or rebuilt, it is physically and logically ordered as per the cluster key(s). But when data is inserted or the clustering key values are updated, DB engine preserves logical ordering of the data.

  • I've seen this question following the clarification of physical and logical ordering so i got it right. I am sure i must have answered it wrong before few days. Good one.

Viewing 9 posts - 31 through 38 (of 38 total)

You must be logged in to reply to this topic. Login to reply