• Rebuilding a heap


    In 2005, rebuilding a table that was a heap (no clustered index) wasn't easy.  You could copy it to a different table, or you could add a clustered index and then drop it.

    In 2008, this is a far easier thing to do.  They have added to the ALTER TABLE command a method to rebuild the table, which is the same as rebuilding the clustered index for a clustered table, but for a HEAP, it is the only way to go. 

    In the following code sample, I create a heap, load it will "little data", then expand every row to much larger values.  The result is a little bit of fragmentation, but more than that, tons of forwarding pointers (when a row won't fit on the same heap page, it gets moved to a different page, but the pointer to the row does not change in the indexes.)  Rebuilding the heap is now really simple:

    create table heapDemo
    (  
        value varchar(1000)
    )
    GO
    set nocount on
    insert into heapDemo
    select 'hi'
    go 10000
    --Expand the values to 500 times the size they were
    update heapDemo
    set value = replicate('hi',500)

    Now, check the stats of the table (using the index stats dmv, no less)

    select index_type_desc, fragment_count, page_count, forwarded_record_count 
    from sys.dm_db_index_physical_stats(db_id(),default,default,default,'DETAILED')
    where object_id = object_id('heapDemo')

    This returns:

    index_type_desc       fragment_count       page_count           forwarded_record_count
    --------------------- -------------------- -------------------- ----------------------
    HEAP                  6                    1443                 9961

    Now, you can rebuild the heap with the command:

    alter table heapDemo rebuild

    Check the values now:

    select index_type_desc, fragment_count, page_count, forwarded_record_count
    from sys.dm_db_index_physical_stats(db_id(),default,default,default,'DETAILED')
    where object_id = object_id('heapDemo')

    This shows no that the forwarded_record_count is 0, which is the idea situation for your heap.

    index_type_desc       fragment_count       page_count           forwarded_record_count
    --------------------- -------------------- -------------------- ----------------------
    HEAP                  3                    1440                 0

    Nice new addition!

  • 相关阅读:
    购物菜单
    数据库
    增删改查
    页面交互
    计算器
    2020.9.21
    团队-团队编程项目中国象棋-项目总结
    团队-团队编程项目作业名称-最终程序
    课后作业-阅读任务-阅读提问-4
    《20171130-构建之法:现代软件工程-阅读笔记》
  • 原文地址:https://www.cnblogs.com/flysun0311/p/2932420.html
Copyright © 2020-2023  润新知