SQL Server PARTITION FUNCTION(分区)

Posted lulus

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL Server PARTITION FUNCTION(分区)相关的知识,希望对你有一定的参考价值。

分区并不影响Linq,sql查询
在MSSQL中,选中目标表,右键-存储-创建分区
根据提示完成分区,存储成sql
这里展示如何根据Id的数据范围分区
在执行前,可能需要设置日志文件大小为“无限制”,否则执行过程中可能出现事务日志已满,原因为“LOG_BACKUP”的报错

--其实就是写个生成字符串列表(数据范围)的函数
DECLARE @fenqu nvarchar(max) = 
N'CREATE PARTITION FUNCTION [fenqu](bigint) AS RANGE RIGHT FOR VALUES (';  
DECLARE @i int = 0;  
WHILE @i < 50000000  
BEGIN  
SET @fenqu += '''' + CAST(@i as nvarchar(10)) + '''' + N', ';  
SET @i = @i+500000;  
END  
SET @fenqu += '''' + CAST(@i as nvarchar(10))+ '''' + N'); ';  
EXEC sp_executesql @fenqu;  

--数量和上面分区的数量对应
CREATE PARTITION SCHEME [fenqufangan] AS PARTITION [fenqu] TO (
[PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], 
[PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY],
[PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY],
[PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY],
[PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY],
[PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY],
[PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY],
[PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY],
[PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY],
[PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY],
[PRIMARY],[PRIMARY])

分区完成后,日志文件、数据文件可能会很大,可以收缩一下

资料

CREATE PARTITION FUNCTION (Transact-SQL)
官方资料展示了根据数量、根据时间段分区的示例

以上是关于SQL Server PARTITION FUNCTION(分区)的主要内容,如果未能解决你的问题,请参考以下文章

SQL Server: Difference between PARTITION BY and GROUP BY

SQL SERVER OVER PARTITION 业务案例

SQL Server:循环遍历每个(PARTITION BY ...)元素的过程或函数

SQL Server 10.50 OVER PARTITION 无法识别

在 sql server 中选择 distinct 和 partition by

SQL Server - 带有 PARTITION 的 ROW_NUMBER(),如何获取多条记录?