[导入]如何在MS Sql中建立分步式分区视图(sql2000,sql2005)

一、sql2000分步式分区视图,SQL 2005方法同样,只是将其优化性能
--ServerA
Use study
go
--创建测试表
Create table customers (
  Customerid  varchar(5) not null,
  CompanyName varchar(50) not null,
  ContactName  varchar(30) null,
--建立约束,及关键字
CONSTRAINT PK_customers PRIMARY KEY CLUSTERED  (Customerid),
CONSTRAINT   CK_customerid   CHECK (Customerid  between 'AAAAA'  and  'LZZZZ')
)

--select * from customers
--Server B
use study
go
Create table customers (
  Customerid  varchar(5) not null,
  CompanyName varchar(50) not null,
  ContactName  varchar(30) null,
CONSTRAINT PK_customers PRIMARY KEY CLUSTERED  (Customerid),
CONSTRAINT   CK_customerid   CHECK (Customerid  between 'M'  and  'ZZZZZ')
)
go
--ServerA
--在serverA服务器上远程连接数据库服务器B
exec  sp_addlinkedserver    
       @server='DPVSERVER1', @srvproduct='',
       @provider='SQLOLEDB', @datasrc='ServerB'
go
exec  sp_addlinkedsrvlogin
@rmtsrvname =  'DPVSERVER1'
     , @useself =  'false'
     , @rmtuser =  'sa'
     , @rmtpassword = 'password'
go
--测试是服务器B否可以连接上
select top 10 * from DPVSERVER1.[study].dbo.customers
--ServerB
--在serverB服务器上远程连接数据库服务器A
exec  sp_addlinkedserver    
       @server='DPVSERVER2', @srvproduct='',
       @provider='SQLOLEDB', @datasrc='ServerA'
exec  sp_addlinkedsrvlogin
@rmtsrvname =  'DPVSERVER2'
     , @useself =  'false'
     , @rmtuser =  'sa'
     , @rmtpassword = 'password'
go
--测试是服务器A否可以连接上
select top 10 * from DPVSERVER2.[study].dbo.customers

--确定是否检查远程表的架构。如果为 ture,则在查询开始时跳过远程表的架构检查,可以优化查询。
--ServerA
Exec sp_serveroption 'GLOBE01', 'lazy schema validation', 'true'
--Sever B
Exec sp_serveroption 'SO', 'lazy schema validation', 'true'

--创建分步式视图
--Server A:
  Create view DPV_Customers  As
   Select *  from Customers
   Union all
   Select *  from  [192.168.42.37].study.dbo.Customers
--Server B
   Create view DPV_Customers  As
   Select *  from  DPVSERVER2.Pubs.dbo.Customers
   UNION ALL
   Select *  from Customers

--select * from DPV_Customers

set xact_abort on--这个不能少。。是必须要,因为是远程连接
/*
当 SET XACT_ABORT 为 ON 时,如果执行 Transact-SQL 语句产生运行时错误,则整个事务将终止并回滚。
当 SET XACT_ABORT 为 OFF 时,有时只回滚产生错误的 Transact-SQL 语句,而事务将继续进行处理。
如果错误很严重,那么即使 SET XACT_ABORT 为 OFF,也可能回滚整个事务。
编译错误(如语法错误)不受 SET XACT_ABORT 的影响。
*/
Insert INTO DPV_CUSTOMERS VALUES('AAMAY','FUZHOU COMPANY','MARRY')
Insert INTO DPV_CUSTOMERS VALUES('CJOHN','XIMEN COMPANY','MARRY')
Insert INTO DPV_CUSTOMERS VALUES('SMITH','SHANGHAI COMPANY','TOM')
Insert INTO DPV_CUSTOMERS VALUES('YOUNG','FUJIAN COMPANY','JANE')
Insert INTO DPV_CUSTOMERS VALUES('GTOPP','BEJING COMPANY','TOM')
Insert INTO DPV_CUSTOMERS VALUES('QUILH','BEJING COMPANY','TOM')

--可以通过查询计划知道它是如何运行及访问
--访问远程视图所有操作服务器
Select  *  FROM  DPV_Customers  order  by  customerid
--通过约束访问符合其约束上的表,在此只访问Server B ,因为它符合其约束
Select  *  FROM  DPV_Customers  Where  CustomerID= 'QUILH'
--通过约束访问符合其约束上的表,在此只访问Server A ,因为它符合其约束
Select  *  FROM  DPV_Customers  Where  CustomerID= 'AAMAY'

=================================================================================

二、sql2005分区表
--添加文件组
alter database Study add filegroup [fg1]
go
alter database Study add filegroup [fg2]
go
alter database Study add filegroup [fg3]
go
--在各自文件组中添加数据各自存储文件ndf,并放到不同的盘里
alter database Study
add file
(name='fg1',
filename='c:\fg1.ndf',
size=5mb)
to filegroup [fg1]
go
alter database Study
add file
(name='fg2',
filename='d:\fg2.ndf',
size=5mb)
to filegroup [fg2]
go
alter database Study
add file
(name='fg3',
filename='e:\fg3.ndf',
size=5mb)
to filegroup [fg3]
go
use Study
go
--创建分区函数,水平左分区
Create partition function emailPF(nvarchar(50))
as range right for values ('G','N')
go
--在文件组fg1,fg2,fg3上创建分区架构
Create partition scheme emailPS
as partition emailPF to (fg1,fg2,fg3)
go
--在分区架构上创建表
Create table customermail (custid int, email nvarchar(50)) on emailPS(email)
go
--测试效率
insert into customermail(custid,email)
select 1,'A'
union all select 2,'AA' union all select 3,'B' union all select 2,'BB'
union all select 3,'CC' union all select 5,'C' union all select 44,'DD'
union all select 23,'D' union all select 5,'EE' union all select 121,'FF'
union all select 12,'G' union all select 2,'H' union all select 33,'I'
union all select 121,'J' union all select 5,'K' union all select 5,'KK'
union all select 11,'L' union all select 5,'M' union all select 22,'N'
union all select 5,'O' union all select 5,'W' union all select 5,'Q'
union all select 5,'W' union all select 5,'R' union all select 5,'S'
union all select 5,'W' union all select 5,'T' union all select 5,'U'
union all select 5,'W' union all select 5,'T' union all select 5,'V'
union all select 5,'W' union all select 5,'T' union all select 5,'x'
union all select 5,'T' union all select 5,'y' union all select 5,'T'
union all select 5,'z'
--查询执行计划用到了分区查询
select * from customermail Where email<'F'
select * from customermail Where custid=5 and email<'F'
select * from customermail Where email <'H'
select * from customermail Where email between 'H' and 'K'
select * from customermail Where email<'W'

来源:Hopewell_Go的专栏
文章来源: http://www.nbman.cn/default.asp?id=104

你可能感兴趣的:(sql2005)