博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQL Server 分区表补充说明
阅读量:5323 次
发布时间:2019-06-14

本文共 3096 字,大约阅读时间需要 10 分钟。

分区教程参阅:

切换分区(归档):

 

补充:

  1. 数据更新时,会根据分区依据,数据在文件组间移动
  2. 归档时,外键约束将阻止归档(同文件组的不同表间归档)

 

理想方案:正常分区,定期结转

 

USE [master]

GO

CREATE DATABASE Sales ON PRIMARY

(

NAME=N'Sales',

FILENAME=N'd:\temp\data\Primary\Sales.mdf',

SIZE=3MB,

MAXSIZE=100MB,

FILEGROWTH=10%

), FILEGROUP FG1

    (

     NAME = N'File1',

     FILENAME = N'd:\temp\data\FG1\File1.ndf',

     SIZE = 1MB,

     MAXSIZE = 100MB,

     FILEGROWTH = 10%

    ), FILEGROUP FG2

    (

     NAME = N'File2',

     FILENAME = N'd:\temp\data\FG2\File2.ndf',

     SIZE = 1MB,

     MAXSIZE = 100MB,

     FILEGROWTH = 10%

    ), FILEGROUP FG3

    (

     NAME = N'File3',

     FILENAME = N'd:\temp\data\FG3\File3.ndf',

     SIZE = 1MB,

     MAXSIZE = 100MB,

     FILEGROWTH = 10%

    ) LOG ON

    (

     NAME = N'Sales_Log',

     FILENAME = N'd:\temp\data\Primary\Sales_Log.ldf',

     SIZE = 1MB,

     MAXSIZE = 100MB,

     FILEGROWTH = 10%

    )

    GO

 

USE sales

GO

 

CREATE PARTITION FUNCTION pf_OrderDate (DATETIME)

AS RANGE RIGHT

FOR VALUES ('2003/01/01', '2004/01/01')

    GO

    

CREATE PARTITION SCHEME ps_OrderDate

AS PARTITION pf_OrderDate

TO(FG1,FG2,FG3)

    GO

      

    

CREATE TABLE Orders

(

OrderID INT IDENTITY(10000, 1) ,

OrderDate DATETIME NOT NULL ,

CustomerID INT NOT NULL ,

CONSTRAINT PK_Orders PRIMARY KEY ( OrderID, OrderDate )

)

ON ps_OrderDate(OrderDate)

    GO

CREATE TABLE OrdersHistory

(

OrderID INT IDENTITY(10000, 1) ,

OrderDate DATETIME NOT NULL ,

CustomerID INT NOT NULL ,

CONSTRAINT PK_OrdersHistory PRIMARY KEY ( OrderID, OrderDate )

)

ON ps_OrderDate(OrderDate)

    GO

    

    

INSERT INTO dbo.Orders

( OrderDate, CustomerID )

VALUES ( '2002/6/25', 1000 )

INSERT INTO dbo.Orders

( OrderDate, CustomerID )

VALUES ( '2002/8/13', 1000 )

INSERT INTO dbo.Orders

( OrderDate, CustomerID )

VALUES ( '2002/8/25', 1000 )

INSERT INTO dbo.Orders

( OrderDate, CustomerID )

VALUES ( '2002/9/23', 1000 )

    GO

 

INSERT INTO dbo.Orders

( OrderDate, CustomerID )

VALUES ( '2003/6/25', 1000 )

INSERT INTO dbo.Orders

( OrderDate, CustomerID )

VALUES ( '2003/8/13', 1000 )

INSERT INTO dbo.Orders

( OrderDate, CustomerID )

VALUES ( '2003/8/25', 1000 )

INSERT INTO dbo.Orders

( OrderDate, CustomerID )

VALUES ( '2003/9/23', 1000 )

    GO

    

SELECT *

FROM dbo.Orders

WHERE $partition.pf_orderdate(orderdate) = 1

SELECT *

FROM dbo.Orders

PRINT N'数据更新后,分区变化'

UPDATE dbo.Orders

SET OrderDate = '2004-9-8'

WHERE OrderID = 10000

    

SELECT *

FROM dbo.Orders

WHERE $partition.pf_orderdate(orderdate) = 1

SELECT *

FROM dbo.Orders

 

PRINT N'数据归档,外键阻止归档'

CREATE TABLE Customer ( id INT PRIMARY KEY )

INSERT INTO customer

VALUES ( 1000 )

ALTER TABLE orders ADD CONSTRAINT fk_orders_customer FOREIGN KEY (customerid) REFERENCES Customer (id)

 

CREATE TABLE order_detail

(

id INT ,

ORDERid INT ,

order_date DATETIME ,

CONSTRAINT PK_Orders_detail PRIMARY KEY ( ORDERid, Order_Date ) ,

CONSTRAINT fk_order FOREIGN KEY ( ORDERid, order_date ) REFERENCES dbo.Orders ( OrderID, OrderDate )

)

 

INSERT INTO order_detail

VALUES ( 1, 10000, '2004/9/8' )

 

 

ALTER TABLE orders SWITCH PARTITION 2 TO ordersHistory PARTITION 2

GO

/*

消息4967,级别16,状态1,第1

ALTER TABLE SWITCH 语句失败。由于源表'Sales.dbo.orders' 包含约束'fk_order' 的主键,因此不允许使用SWITCH

 

*/

SELECT *

FROM dbo.Orders

WHERE $partition.pf_orderdate(orderdate) = 1

SELECT *

FROM dbo.Orders

转载于:https://www.cnblogs.com/QinQouShui/p/3979616.html

你可能感兴趣的文章
Recover Binary Search Tree
查看>>
Java 实践:生产者与消费者
查看>>
[转]IOCP--Socket IO模型终结篇
查看>>
js 获取视频的第一帧
查看>>
各种正则验证
查看>>
观察者模式(Observer)
查看>>
python中numpy.r_和numpy.c_
查看>>
egret3D与2D混合开发,画布尺寸不一致的问题
查看>>
freebsd 实现 tab 命令 补全 命令 提示
查看>>
struts1和struts2的区别
查看>>
函数之匿名函数
查看>>
shell习题第16题:查用户
查看>>
实验4 [bx]和loop的使用
查看>>
Redis常用命令
查看>>
2018.11.06 bzoj1040: [ZJOI2008]骑士(树形dp)
查看>>
2019.02.15 bzoj5210: 最大连通子块和(链分治+ddp)
查看>>
redis cluster 集群资料
查看>>
微软职位内部推荐-Sr. SE - Office incubation
查看>>
微软职位内部推荐-SOFTWARE ENGINEER II
查看>>
centos系统python2.7更新到3.5
查看>>