zoukankan      html  css  js  c++  java
  • sqlserver索引维护(重新组织生成索引)

      sqlserver索引的维护

    1:查看索引碎片大于百分三十以上的索引

    select object_id= object_id,indexid = index_id,partitionnum = partition_number,frag= avg_fragmentation_in_percent 
    into #work_to_do
    from sys.dm_db_index_physical_stats(db_id(), null, null , null, 'LIMITED') --dm_ph_stats join sys.dm_db_partition_stats dm_pa_st on dm_ph_stats.object_id=dm_pa_st.object_id
    where avg_fragmentation_in_percent >= 30.0 
    and index_id > 0
    and object_id in (select distinct a.object_id from sys.dm_db_partition_stats a join sys.indexes b
    on a.object_id=b.object_id and a.index_id=b.index_id
    where a.index_id>0
    and a.in_row_data_page_count>1280 )
    
    select a.object_id,a.name,a.type_desc,b.partitionnum ,b.frag,b.indexid from sys.indexes a , #work_to_do b
    where a.object_id=b.object_id and a.index_id=b.indexid-- where object_id in(select objectid from #work_to_do)
    --drop table #work_to_do

    2:查看单表的索引碎片

    SELECT a.index_id, name, avg_fragmentation_in_percent
    FROM sys.dm_db_index_physical_stats (DB_ID(N'MIAO'), OBJECT_ID(N'miao.注册信息表'), NULL, NULL, NULL) AS a
    JOIN sys.indexes AS b ON a.object_id = b.object_id AND a.index_id = b.index_id;

    看一下官网的推荐:

    avg_fragmentation_in_percent 值

    修复语句

    > 5% 且 < = 30%

    ALTER INDEX REORGANIZE

    > 30%

    ALTER INDEX REBUILD WITH (ONLINE = ON)*

     

    所以说大于百分三十的索引是要重建的.
     
    ---------------------------------------------重新组织索引----------------------------------------
    ALTER INDEX IX_Employee_OrganizationalLevel_OrganizationalNode ON HumanResources.Employee
    REORGANIZE ; 
    GO
    --------------------------------------------重新组织表中所有的索引--------------------------------------------
    ALTER INDEX ALL ON HumanResources.Employee
    REORGANIZE 
    -------------------------------------------重新生成的索引--------------------------------------------
    ALTER INDEX PK_Employee_BusinessEntityID ON HumanResources.Employee
    REBUILD;
    GO
    ---------------------------------------------重新生成表中所有的索引--------------------------------------------
    ALTER INDEX ALL ON Production.Product
    REBUILD WITH (FILLFACTOR = 80, SORT_IN_TEMPDB = ON,
                  STATISTICS_NORECOMPUTE = ON);
    热衷于学习讨论MySQL和SQL Server,NoSQL等数据库技术,欢迎加入SQL优化群:659336691
  • 相关阅读:
    CH the luckiest number 欧拉函数 同余
    bzoj1257余数求和 数论分块 暴力
    luogup1463 反素数
    CH3101 阶乘分解
    T10396 曹老板斗地主(中度模拟)
    NOIP 2015子串(DP)
    CF1205B Shortest Cycle(Floyd判最小环)
    P2055 [ZJOI2009]假期的宿舍(二分图匹配)
    灾后重建(最短路)
    CF1098A Sum in the tree(贪心)
  • 原文地址:https://www.cnblogs.com/shengdimaya/p/5341093.html
Copyright © 2011-2022 走看看