zoukankan      html  css  js  c++  java
  • 数据表分区, 普通表转分区表

    继续使用上一节的数据库.

    --填充一个事实表, 230W条记录

    if exists(select 1 from sys.objects where name='fact_SaleCar' and type='U')     
    drop table fact_SaleCar 
    create table fact_SaleCar(     
    SaleCarID nvarchar(20) not null primary key,     Salename nvarchar(50),     CheckOutDate datetime,     Attribute1 nvarchar(50),     
    Attribute2 nvarchar(50),     Attribute3 nvarchar(50),     Attribute4 nvarchar(50),     Attribute5 nvarchar(50),     
    Attribute6 nvarchar(50),     Attribute7 nvarchar(50),     Attribute8 nvarchar(50),     Attribute9 nvarchar(50),     
    Attribute10 nvarchar(50),     Attribute11 nvarchar(50),     Attribute12 nvarchar(50) 
    ) 
    go   
    
    --插入测试数据 
    begin 
    	begin tran   
    		declare @num int, @temp nvarchar(50); 
    		set @num=1; 
    --2010年导入80W 
    		while @num<=800000 
    		begin     
    			set @temp=rtrim(year(dateadd(year,-2,getdate())))+RTRIM(@num);     
    			insert into dbo.fact_SaleCar     
    			select @temp,             '商店'+@temp,             dateadd(year,-2,getdate()),             
    			'1'+@temp,'2'+@temp,'3'+@temp,'4'+@temp,'5'+@temp,'6'+@temp,'7'+@temp,             
    			'8'+@temp,'9'+@temp,'10'+@temp,'11'+@temp,'12'+@temp         
    			set @num=@num+1     
    			if @@ERROR<>0     
    			begin             
    				rollback tran             
    				return;     
    			end   
    		end   
    
    		set @num=1; 
    		--2011年导入50W 
    		while @num<=500000 
    		begin     
    			set @temp=rtrim(year(dateadd(year,-1,getdate())))+RTRIM(@num);     
    			insert into dbo.fact_SaleCar     
    					select @temp,             
    					'商店'+@temp,             
    					dateadd(year,-1,getdate()),             
    					'1'+@temp,'2'+@temp,'3'+@temp,'4'+@temp,'5'+@temp,'6'+@temp,'7'+@temp,             
    					'8'+@temp,'9'+@temp,'10'+@temp,'11'+@temp,'12'+@temp         
    					set @num=@num+1     
    			if @@ERROR<>0     
    			begin             
    				rollback tran             
    				return;    
    			end   
    		end   
    
    		set @num=1; 
    		--2011年导入100W 
    		while @num<=1000000 
    		begin     
    			set @temp=rtrim(year(dateadd(year,0,getdate())))+RTRIM(@num);         
    			insert into dbo.fact_SaleCar     
    						select @temp,             
    						'商店'+@temp,             
    						dateadd(year,0,getdate()),             
    						'1'+@temp,'2'+@temp,'3'+@temp,'4'+@temp,'5'+@temp,'6'+@temp,'7'+@temp,             
    						'8'+@temp,'9'+@temp,'10'+@temp,'11'+@temp,'12'+@temp         
    			set @num=@num+1     
    			if @@ERROR<>0     
    			begin             
    				rollback tran             
    				return;     
    			end   
    		end;   
    commit tran 
    end;

    然后新建分区函数和分区方案

    --create partition function
    --PT_Sales_Test230W_Fun_fact_SaleCar_Date(datetime) as 
    --range left for values('2011-1-1','2012-1-1')
    
    --create partition scheme 
    --PT_Sales_Test230W_Scheme_fact_SaleCar_Date as partition PT_Sales_Test230W_Fun_fact_SaleCar_Date
    --to(PT_Sales_Test230W_p1,PT_Sales_Test230W_p2,PT_Sales_Test230W_p3)

    删除已有表的主键和聚集索引.

    alter table fact_SaleCar drop constraint PK_Fact_SaleCar

    新建不带聚集索引的主键

    alter table Fact_SaleCar add constraint PK_Fact_SaleCar primary key nonclustered(

    SaleCarID Asc

    ) on [Primary]

    go

    再新建聚集索引, 不过这个时候聚集索引要指向分区方案. 这样就可以把表已有的数据分散到各个文件上.

    create clustered index CT_Fact_SaleCar on Fact_SaleCar(checkOutDate)

    on PT_Schme_Fact_SaleCar_CheckOutDate(CheckOutDate)

  • 相关阅读:
    BZOJ 3924 / Luogu P3345 [ZJOI2015]幻想乡战略游戏 (动态点分治/点分树)
    BZOJ 3065 带插入区间K小值 (替罪羊树套线段树)
    BZOJ 3217: ALOEXT (块状链表套trie)
    BZOJ 3514: Codechef MARCH14 GERALD07加强版 (LCT维护最大生成树+主席树)
    BZOJ 3932: [CQOI2015]任务查询系统 (主席树板题)
    BZOJ 3658: Jabberwocky (双向链表+BIT)
    BZOJ 1180 [CROATIAN 2009]OTOCI // BZOJ 2843 极地旅行社 // Luogu P4321 [COCI 2009] OTOCI / 极地旅行社 (LCA板题)
    BZOJ 2759 一个动态树好题 (LCT)
    BZOJ 2244: [SDOI2011]拦截导弹 (CDQ分治 三维偏序 DP)
    codefroces 612E Square Root of Permutation
  • 原文地址:https://www.cnblogs.com/jianjialin/p/2797363.html
Copyright © 2011-2022 走看看