Maison > Article > base de données > SQL Server 2005/2008/2012中应用分布式分区视图
SQL Server 2008中SQL 应用 系列--目录索引 自2000版本起,SQL Server企业版中引入 分布式 分区 视图 ,允许你为分布在不同的SQL 实例的两个或多个水平 分区 表创建 视图 。 简要步骤如下:根据Check约束中定义的一组把大表分割成更小的一些表。Check约束确
SQL Server 2008中SQL应用系列--目录索引
自2000版本起,SQL Server企业版中引入分布式分区视图,允许你为分布在不同的SQL 实例的两个或多个水平分区表创建视图。
简要步骤如下:根据Check约束中定义的一组值把大表分割成更小的一些表。Check约束确保每个小表保存着不能保存在其他表的唯一数据。然后使用Union All创建分布式分区视图,把所有这些小表联结成单独的结果集。
这样对性能的改善是有益的,例如,如果视图根据日期分区,并用查询来返回仅保存在一个分区表中的行,那么SQL Server会智能地只搜索一个分区而不是分布式分区视图中的所有表。
我们假设一个场景,某公司成立上海和北京分公司,分别有各自的SQL Server实例来保存网站数据,都希望用一个表跟踪网站点击。点击量非常大。此时,需要一个视图以在单个视图中引用各自的表。公司希望能查询任意一个服务器,并且返回相同的数据或各自分公司的数据。
下面我们通过实例来演示这个场景的具体应用。假定有两个实例:AP4\NET2012和AP4\NET2013(本文所有示例均在SQL Server 2008环境下通过)。
一、创建链接服务器,当然也可以通过OpenRowset(http://msdn.microsoft.com/zh-cn/library/ms190312.aspx)而不创建链接服务器,并创建测试数据库和表。
<span>/*</span><span>******** 创建一个<strong>分布式</strong><strong>分区</strong><strong>视图</strong> **************</span><span>*/</span> <span>/*</span><span>******** 3w@live.cn 邀月 **************</span><span>*/</span> <span>USE</span><span> master </span><span>GO</span> <span>EXEC</span><span> sp_addlinkedserver </span><span>'</span><span>AP4\NET2013</span><span>'</span><span>, N</span><span>'</span><span>SQL Server</span><span>'</span> <span>GO</span> <span>--</span><span> 跳过远程实例架构表的检查,以提升性能,邀月注</span> <span>EXEC</span> sp_serveroption <span>'</span><span>AP4\NET2013</span><span>'</span>, <span>'</span><span>lazy schema validation</span><span>'</span>, <span>'</span><span>true</span><span>'</span> <span>GO</span> <span>--</span><span>创建测试数据库</span> <span>IF</span> <span>NOT</span> <span>EXISTS</span> (<span>SELECT</span><span> name </span><span>FROM</span><span> sys.databases </span><span>WHERE</span> name <span>=</span> <span>'</span><span>RemoteViewTest2012</span><span>'</span><span>) </span><span>BEGIN</span> <span>CREATE</span> <span>DATABASE</span><span> RemoteViewTest2012 </span><span>END</span> <span>GO</span> <span>--</span><span>打开测试库</span> <span>Use</span><span> RemoteViewTest2012 </span><span>GO</span> <span>--</span><span>创建上海分公司的点击表</span> <span>CREATE</span> <span>TABLE</span><span> dbo.WebHits_ShangHai (WebHitID </span><span>uniqueidentifier</span> <span>NOT</span> <span>NULL</span><span>, WebSite </span><span>varchar</span>(<span>20</span>) <span>NOT</span> <span>NULL</span><span> , HitDT </span><span>datetime</span> <span>NOT</span> <span>NULL</span><span>, </span><span>CHECK</span> (WebSite <span>=</span> <span>'</span><span>ShangHai</span><span>'</span><span>), </span><span>CONSTRAINT</span> PK_WebHits <span>PRIMARY</span> <span>KEY</span> (WebHitID, WebSite))
第二个实例:
<span>/*</span><span>************** 实例 AP4\NET2013(SQL Server 2008) ********</span><span>*/</span> <span>/*</span><span>******** 3w@live.cn 邀月 **************</span><span>*/</span> <span>USE</span><span> master </span><span>GO</span> <span>EXEC</span><span> sp_addlinkedserver </span><span>'</span><span>AP4\NET2012</span><span>'</span><span>, N</span><span>'</span><span>SQL Server</span><span>'</span> <span>GO</span> <span>--</span><span> 跳过远程实例架构表的检查,以提升性能,邀月注</span> <span>EXEC</span> sp_serveroption <span>'</span><span>AP4\NET2012</span><span>'</span>, <span>'</span><span>lazy schema validation</span><span>'</span>, <span>'</span><span>true</span><span>'</span> <span>GO</span> <span>IF</span> <span>NOT</span> <span>EXISTS</span> (<span>SELECT</span><span> name </span><span>FROM</span><span> sys.databases </span><span>WHERE</span> name <span>=</span> <span>'</span><span>RemoteViewTest2012</span><span>'</span><span>) </span><span>BEGIN</span> <span>CREATE</span> <span>DATABASE</span><span> RemoteViewTest2012 </span><span>END</span> <span>GO</span> <span>--</span><span>打开测试库</span> <span>Use</span><span> RemoteViewTest2012 </span><span>GO</span> <span>--</span><span>创建北京分公司的点击表</span> <span>CREATE</span> <span>TABLE</span><span> dbo.WebHits_BeiJing (WebHitID </span><span>uniqueidentifier</span> <span>NOT</span> <span>NULL</span><span>, WebSite </span><span>varchar</span>(<span>20</span>) <span>NOT</span> <span>NULL</span><span> , HitDT </span><span>datetime</span> <span>NOT</span> <span>NULL</span><span>, </span><span>CHECK</span> (WebSite <span>=</span> <span>'</span><span>BeiJing</span><span>'</span><span>), </span><span>CONSTRAINT</span> PK_WebHits <span>PRIMARY</span> <span>KEY</span> (WebHitID, WebSite))
二、在两个实例中分别创建视图
<span>/*</span><span>************** 实例 AP4\NET2012(SQL Server 2008) ********</span><span>*/</span> <span>/*</span><span>******** 3w@live.cn 邀月 **************</span><span>*/</span> <span>--</span><span>打开测试库</span> <span>Use</span><span> RemoteViewTest2012 </span><span>GO</span> <span>--</span><span>创建<strong>分区</strong><strong>视图</strong></span> <span>CREATE</span> <span>VIEW</span> dbo.v_WebHits <span>AS</span> <span>SELECT</span><span> WebHitID, WebSite, HitDT </span><span>FROM</span><span> RemoteViewTest2012.dbo.WebHits_ShangHai </span><span>UNION</span> <span>ALL</span> <span>SELECT</span><span> WebHitID, WebSite, HitDT </span><span>FROM</span> <span>[</span><span>AP4\NET2013</span><span>]</span><span>.RemoteViewTest2012.dbo.WebHits_BeiJing </span><span>GO</span> <span>/*</span><span>************** 实例 AP4\NET2013(SQL Server 2008) ********</span><span>*/</span> <span>/*</span><span>******** 3w@live.cn 邀月 **************</span><span>*/</span> <span>--</span><span>打开测试库</span> <span>Use</span><span> RemoteViewTest2012 </span><span>GO</span> <span>--</span><span>创建<strong>分区</strong><strong>视图</strong></span> <span>CREATE</span> <span>VIEW</span> dbo.v_WebHits <span>AS</span> <span>SELECT</span><span> WebHitID, WebSite, HitDT </span><span>FROM</span><span> RemoteViewTest2012.dbo.WebHits_BeiJing </span><span>UNION</span> <span>ALL</span> <span>SELECT</span><span> WebHitID, WebSite, HitDT </span><span>FROM</span> <span>[</span><span>AP4\NET2012</span><span>]</span><span>.RemoteViewTest2012.dbo.WebHits_ShangHai </span><span>GO</span>
三、插入测试数据
我们可以选择任意一个实例中插入,下面我们选择AP4\NET2013
<span>/*</span><span>************** 实例 AP4\NET2013(SQL Server 2008) ********</span><span>*/</span> <span>/*</span><span>******** 3w@live.cn 邀月 **************</span><span>*/</span> <span>--</span><span>--要保证插入,必须打开XACT_ABORT开关,并开启<strong>分布式</strong>事务协调器,邀月注</span> <span>--</span><span>打开测试库</span> <span>Use</span><span> RemoteViewTest2012 </span><span>GO</span> <span>SET</span> XACT_ABORT <span>ON</span> <span>INSERT</span><span> dbo.v_WebHits (WebHitID, WebSite, HitDT) </span><span>VALUES</span>(<span>NEWID</span>(), <span>'</span><span>ShangHai</span><span>'</span>, <span>GETDATE</span><span>()) </span><span>INSERT</span><span> dbo.v_WebHits (WebHitID, WebSite, HitDT) </span><span>VALUES</span>(<span>NEWID</span>(), <span>'</span><span>BeiJing</span><span>'</span>, <span>GETDATE</span>())
注意,如果该实例所在的服务器上没有启用MSDTC(Microsoft 分布式事务处理协调器),会抛出一个错误:
此时在命令行中输入Net start msdtc以启用该服务。
如果还是不能正常启动MSDTC,请查阅MSDN(http://msdn.microsoft.com/zh-cn/library/aa561924%28BTS.10%29.aspx)以获取帮助。
四、进行分布式查询
此时,我们在任意一个实例查询的结果都是一致的,也正是我们想要的。
<span>/*</span><span>************** 实例 AP4\NET2013(SQL Server 2008) ********</span><span>*/</span> <span>/*</span><span>******** 3w@live.cn 邀月 **************</span><span>*/</span> <span>/*</span><span>**** <strong>分布式</strong>查询 *************</span><span>*/</span> <span>--</span><span>--AP4\NET2013上查询</span><span> --</span><span>打开测试库</span> <span>Use</span><span> RemoteViewTest2012 </span><span>GO</span> <span>SET</span> XACT_ABORT <span>ON</span> <span>SELECT</span><span> WebHitID, WebSite, HitDT </span><span>FROM</span><span> dbo.v_WebHits </span><span>SELECT</span><span> WebHitID, WebSite, HitDT </span><span>FROM</span> <span>[</span><span>AP4\NET2012</span><span>]</span>.RemoteViewTest2012.dbo.WebHits_ShangHai
<span>--</span><span>--AP4\N ET2012上查询</span><span> --</span><span>打开测试库</span> <span>Use</span><span> RemoteViewTest2012 </span><span>GO</span> <span>SET</span> XACT_ABORT <span>ON</span> <span>SELECT</span><span> WebHitID, WebSite, HitDT </span><span>FROM</span><span> dbo.v_WebHits </span><span>SELECT</span><span> WebHitID, WebSite, HitDT </span><span>FROM</span> <span>[</span><span>AP4\NET2013</span><span>]</span>.RemoteViewTest2012.dbo.WebHits_BeiJing
我们欣喜地看到,SQL Server并没有在基础分区表中插入冗余数据,而是自动分发到了Check所约定的相应的表中,这得益于MSDTC的功劳。
注意:创建分布式视图的注意事项和必要条件,请看MSDN(http://msdn.microsoft.com/zh-cn/library/ms188299.aspx)。
小结:分布式分区视图允许我们跨多个SQL Server实例划分数据。对于超大型数据库和拥有大量事务和读操作的SQL Server实例来说,这种设计让我们获益良多。根据被查询的视图,SQL Server能确定只查询本地分区表是否能满足某个查询请求,远程表是否需要查询,最终,SQL Server会最大限度地减少SQL Server实例间传输的数据总量。
查看原文请点击这里。