sql2000分步式分区视图,2005的分区表[有部份是来自csdn视频教材]

一、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' 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值