zoukankan      html  css  js  c++  java
  • SQL Server 分区表补充说明

    分区教程参阅:http://database.9sssd.com/mssql/art/951

    切换分区(归档):http://technet.microsoft.com/zh-cn/library/ms191160(v=sql.105).aspx

     

    补充:

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

     

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

     

    USE [master]

    GO

    CREATE DATABASE Sales ON PRIMARY

    (

    NAME=N'Sales',

    FILENAME=N'd: empdataPrimarySales.mdf',

    SIZE=3MB,

    MAXSIZE=100MB,

    FILEGROWTH=10%

    ), FILEGROUP FG1

        (

         NAME = N'File1',

         FILENAME = N'd: empdataFG1File1.ndf',

         SIZE = 1MB,

         MAXSIZE = 100MB,

         FILEGROWTH = 10%

        ), FILEGROUP FG2

        (

         NAME = N'File2',

         FILENAME = N'd: empdataFG2File2.ndf',

         SIZE = 1MB,

         MAXSIZE = 100MB,

         FILEGROWTH = 10%

        ), FILEGROUP FG3

        (

         NAME = N'File3',

         FILENAME = N'd: empdataFG3File3.ndf',

         SIZE = 1MB,

         MAXSIZE = 100MB,

         FILEGROWTH = 10%

        ) LOG ON

        (

         NAME = N'Sales_Log',

         FILENAME = N'd: empdataPrimarySales_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

  • 相关阅读:
    同步和异步有何异同,在什么情况下分别使用他们?
    Android 缓存处理
    Android常用的设计模式概念
    Android常见的设计模式详解
    Android中的五大布局
    几种常见的引用概念
    面向对象的思想概述
    Android系统架构的简单描述
    display:none;与visibility:hidden;的区别
    块级元素和行内元素的区别
  • 原文地址:https://www.cnblogs.com/QinQouShui/p/3979616.html
Copyright © 2011-2022 走看看