zoukankan      html  css  js  c++  java
  • 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!

  • 相关阅读:
    Windows Server 2008 R2 服务器系统安装及配置全过程图文详解
    c#多线程
    DevExpress之TreeList控件用作导航使用说明
    Windows操作系统下搭建Git服务器和客户端。
    D^3CTF 2021 8-bit-pub
    shiro-1.2.4反序列化分析踩坑
    *CTF2021 oh-my-bet
    无线网络密码破解初体验
    瑞昱rtl819x-SDK-v3.4.14b的watchdog分析
    RC4加密算法
  • 原文地址:https://www.cnblogs.com/flysun0311/p/2932420.html
Copyright © 2011-2022 走看看