zoukankan      html  css  js  c++  java
  • SQL code for the partitoned example

    --
    --Chapter 12 SQL code for the partitoned example.
    --
     
    USE AdventureWorks2008;

    -- Create the partition function
    CREATE PARTITION FUNCTION [OrderDateRangePFN](datetime)
    AS RANGE RIGHT
    FOR VALUES (N'2001-01-01 00:00:00', N'2002-01-01 00:00:00',
    N'2003-01-01 00:00:00', N'2004-01-01 00:00:00');


    -- Create the partition scheme
    CREATE PARTITION SCHEME [OrderDatePScheme]
    AS PARTITION [OrderDateRangePFN]
    TO ([Primary], [Primary], [Primary], [Primary], [Primary]);


    -- Create two tables: SalesOrderHeader is the partitioned table and SalesOrderHeaderOLD is the non-partitioned table

    CREATE TABLE [dbo].[SalesOrderHeader](
     [SalesOrderID] [int] NULL,
     [RevisionNumber] [tinyint] NOT NULL,
     [OrderDate] [datetime] NOT NULL,
     [DueDate] [datetime] NOT NULL,
     [ShipDate] [datetime] NULL,
     [Status] [tinyint] NOT NULL
    ) ON [OrderDatePScheme]([OrderDate]);

    CREATE TABLE [dbo].[SalesOrderHeaderOLD](
     [SalesOrderID] [int] NULL,
     [RevisionNumber] [tinyint] NOT NULL,
     [OrderDate] [datetime] NOT NULL  ,
     [DueDate] [datetime] NOT NULL,
     [ShipDate] [datetime] NULL,
     [Status] [tinyint] NOT NULL);

    -- Load daata into the partitioned table
    INSERT INTO SalesOrderHeader
    SELECT [SalesOrderID],[RevisionNumber], [OrderDate],[DueDate],[ShipDate],[Status] FROM SALES.[SalesOrderHeader];

    CREATE CLUSTERED INDEX SalesOrderHeaderCLInd
    ON SalesOrderHeader(OrderDate) ON OrderDatePScheme(OrderDate);

    CREATE CLUSTERED INDEX SalesOrderHeaderOLDCLInd ON SalesOrderHeaderOLD(OrderDate);

    ALTER  TABLE [DBO].[SalesOrderHeaderOLD]  WITH CHECK  ADD CONSTRAINT [CK_SalesOrderHeaderOLD_ORDERDATE] CHECK  ([ORDERDATE]>=('2003-01-01 00:00:00') AND [ORDERDATE]<('2004-01-01 00:00:00'));

    -- Verify data in the partitioned table
    SELECT $partition.OrderDateRangePFN(OrderDate) AS 'Partition Number' ,min(OrderDate) AS 'Min Order Date' , max(OrderDate) AS 'Max Order Date',count(*) AS 'Rows In Partition'
    FROM SalesOrderHeader
    GROUP BY $partition.OrderDateRangePFN(OrderDate);


    -- Switch the data from partition 4 into the SalesOrderHeaderOLD table
    ALTER TABLE SalesOrderHeader
    SWITCH PARTITION 4 TO SalesOrderHeaderOLD;


    -- Switch the data from SalesOrderHeaderOLD back to partition 4
    ALTER TABLE SalesOrderHeaderOLD   
    SWITCH TO SalesOrderHeader PARTITION 4;


    --To merge a partition range
    ALTER PARTITION FUNCTION OrderDateRangePFN()
    MERGE RANGE ('2003-01-01 00:00:00');



    -- To split a partition range
    ALTER PARTITION SCHEME OrderDatePScheme NEXT USED [Primary];
    ALTER PARTITION FUNCTION OrderDateRangePFN()
    SPLIT RANGE ('2003-01-01 00:00:00');

  • 相关阅读:
    网络编程之即时通信程序(聊天室)(一)通信流程简介及通信协议定制
    C#常用加密方法解析
    ASP.NET常用数据绑定控件优劣总结
    使用XPO开发时可以参考使用的架构
    渠道会上的体会
    如何利用第三方SDK开发MSN机器人以及实现语音视频?
    对 XPO 的一些问题的解答
    c++ 参数传递 之引用形参 GIS
    指针与引用的区别 GIS
    c++ const 修饰数组 GIS
  • 原文地址:https://www.cnblogs.com/shihao/p/2511940.html
Copyright © 2011-2022 走看看