zoukankan      html  css  js  c++  java
  • MSSQL订阅库索引对齐

      需求如下图:

      

      在原来的架构中是每台web服务器都固定访问某一台数据库服务器,所以就造成了每台数据库订阅服务器上的索引不一致。现在的需求就是要把所有的订阅库上的索引调整为一致,为了就是实现高可用+负载均衡。原因是因为订阅库出现过硬盘故障,导致部分的应用无法访问了。 

      思路比较简单粗暴

        1、把数据库中所有的索引信息提取出来

    SELECT OBJECT_NAME(i.[object_id]) tblname  
        , i.name Index_name  
        , i.index_id  
        , i.type_desc Index_Type
        , c.name ColName  
        , ic.index_column_id index_column_id  
        , ic.is_included_column is_included_column  
        , i.is_unique   
        , i.fill_factor   
        , i.filter_definition  
    INTO DBName_Index_Align_192_168_10_2_V1
    FROM sys.indexes i  
        LEFT OUTER JOIN sys.index_columns ic ON i.index_id = ic.index_id AND ic.[object_id] = i.[object_id]  
        LEFT OUTER JOIN sys.[columns] c ON c.[object_id] = i.[object_id] AND c.column_id = ic.column_id   
    ORDER BY OBJECT_NAME(i.[object_id])

        2、匹配所有的索引(这步比较耗时耗力,相当于重新把所有的库做了一次调优)

          2.1、先用192.168.10.2跟192.168.10.3做匹配,把两个库的索引调整成一致

          2.2、再用192.168.10.2跟192.168.10.4做匹配,把在192.168.10.2上的调整同时调整到192.168.10.3上。

          2.3、若是还有更多的订阅库,就一直使用192.168.10.2来作为匹配的对象,把在此数据库上做的调整同时更新更已经匹配过的订阅数据库上。

    # 使用以下的代码来匹配
    SELECT
    * FROM ( SELECT ia.tblname tblname, ia.Index_name, ia.ColName, ia.index_column_id, ia.Index_Type , ia.is_included_column, ia.filter_definition , ia2.tblname cmp_tblname, ia2.Index_name cmp_Index_name, ia2.ColName cmp_ColName, ia2.index_column_id cmp_index_column_id,ia2.Index_Type cmp_Index_Type, ia2.is_included_column cmp_is_included_column, ia2.filter_definition cmp_filter_definition FROM DBName_Index_Align_192_168_10_2_V1 ia FULL JOIN DBName_Index_Align_192_168_10_3_V1 ia2 ON ia.tblname = ia2.tblname AND ia.Index_name = ia2.Index_name AND ia.ColName = ia2.ColName AND ia.index_column_id = ia2.index_column_id AND ia.is_included_column = ia2.is_included_column ) t WHERE ISNULL(t.tblname , t.cmp_tblname) NOT IN ('索引名称')   AND ISNULL(t.Index_Type , t.cmp_Index_Type) <> 'HEAP' ORDER BY ISNULL(t.tblname , t.cmp_tblname), ISNULL(t.Index_name , t.cmp_Index_name) , ISNULL(t.index_column_id , t.cmp_index_column_id)

      注:这种方法虽然实现比较初级的高可用和负载均衡,但存在以下弊端

        1、因为索引必须对齐,所以在部分的订阅库中也建立了一些可能永远都不会访问到的索引,这样会导致空间消耗和索引更新带来的消耗;

        

      以上,如有错谬,请不吝指正。 

  • 相关阅读:
    C#中小写人民币转大写
    Oracle中按规定的字符截取字符串
    Oracle中table数据数据类型
    Oracle中case的第二种用法
    javascript跳转页面
    C#添加二维码带加密带logo
    Oracle
    Oracle中with关键字的使用
    jquery
    插入排序,希尔排序原理,代码及复杂度分析
  • 原文地址:https://www.cnblogs.com/cnzeno/p/6207392.html
Copyright © 2011-2022 走看看