SQLSERVER分表流程

分表流程

创建文件组-》创建数据文件到文件组-》使用向导创建分区表(sql server 客户端跟着向导往下做就行)

分表语法

摘自:https://www.cnblogs.com/knowledgesea/p/3696912.html

--创建文件组:
alter database <数据库名> add filegroup <文件组名>
alter database test add filegroup UserLoginInfoGroup
--创建数据文件到文件组里面:
alter database <数据库名称> add file <数据标识> to filegroup <文件组名称>
alter database test add file (name='UserLoginInfo3',filename='C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\UserLoginInfo3.mdf',SIZE=5MB,FILEGROWTH=5MB) to filegroup UserLoginInfoGroup
--创建分区函数:
create partition function 分区函数名(<分区列类型>) as range [left/right] 
for values (每个分区的边界值,....) 
create PARTITION function UserLoginInfoPartitionFunction(int)
as range right for values(4000000,8000000)
--删除分区函数:
drop partition function <分区函数名>
--创建分区方案:
create partition scheme <分区方案名称> as partition <分区函数名称> [all]to (文件组名称,....) 
create partition scheme UserLoginInfoPartitionScheme as  partition UserLoginInfoPartitionFunction to (id,UserLoginInfo1,UserLoginInfo3,UserLoginInfo3)
--删除分区方案:
drop partition scheme<分区方案名称>
--创建分区表语法
create table <表名> (<列定义>)on<分区方案名>(分区列名)
create table BigOrder (
   OrderId              int                  identity,
   orderNum             varchar(30)          not null,
   OrderStatus          int                  not null default 0,
   OrderPayStatus       int                  not null default 0,
   UserId               varchar(40)          not null,
   CreateDate           datetime             null default getdate(),
   Mark                 nvarchar(300)        null
)on bgPartitionSchema(OrderId)
--创建分区索引语法
create <索引分类> index <索引名称> 
on <表名>(列名)
on <分区方案名>(分区依据列名)
--创建分区索引
CREATE CLUSTERED INDEX [ClusteredIndex_on_bgPartitionSchema_635342971076448165] ON [dbo].[BigOrder] 
(
    [OrderId]
)WITH (SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF) ON [bgPartitionSchema]([OrderId])
--查询分区依据列为10000014的数据在哪个分区上
select $partition.bgPartitionFun(2000000)  --返回值是2,表示此值存在第2个分区
--查看分区表中,每个非空分区存在的行数
select $partition.bgPartitionFun(orderid) as partitionNum,count(*) as recordCount
from bigorder
group by  $partition.bgPartitionFun(orderid)
---查看指定分区中的数据记录
select * from bigorder where $partition.bgPartitionFun(orderid)=2
--分区拆分
alter partition function bgPartitionFun()
split range(N'1500000')  --将第二个分区拆为2个分区
--合并分区
alter partition function bgPartitionFun()
merge range(N'1500000')  --将第二第三分区合并
--创建表
create table <表名> (
  <列定义>
)on <文件组名>
--将bigorder分区表中的第一分区数据复制到普通表中
alter table bigorder switch partition 1 to <普通表名>
--将普通表中的数据复制到bigorder分区表中的第一分区
alter table <普通表名> switch to bigorder partition 1 

未完,待整理…

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值