sql优化-创建表分区
前言: 创建表分区就是将表中的数据按照不同的维度分开存储在不同的对象中,比如你是要在100万人中找一个人,那么你分区以后,就相当于在100个人中找一个人。直接上代码:
1. 创建分区的文件组
--1. 创建文件组 alter database Lhcloud_dev add filegroup fileGroup01 alter database Lhcloud_dev add filegroup fileGroup02 alter database Lhcloud_dev add filegroup fileGroup03 alter database Lhcloud_dev add filegroup fileGroup04 alter database Lhcloud_dev add filegroup fileGroup05 alter database Lhcloud_dev add filegroup fileGroup06 alter database Lhcloud_dev add filegroup fileGroup07 alter database Lhcloud_dev add filegroup fileGroup08 alter database Lhcloud_dev add filegroup fileGroup09 alter database Lhcloud_dev add filegroup fileGroup10
2. 为分区文件组分配对应的物理路径
alter database LHhcloud_dev add file (name='orderfile01',filename=N'D:sql_serve_dbsalesoutorder01.ndf',size=5Mb,filegrowth=5mb) to filegroup salesoutorderGroup01 alter database LHhcloud_dev add file (name='orderfile02',filename=N'D:sql_serve_dbsalesoutorder02.ndf',size=5Mb,filegrowth=5mb) to filegroup salesoutorderGroup02 alter database LHhcloud_dev add file (name='orderfile03',filename=N'D:sql_serve_dbsalesoutorder03.ndf',size=5Mb,filegrowth=5mb) to filegroup salesoutorderGroup03 alter database LHhcloud_dev add file (name='orderfile04',filename=N'D:sql_serve_dbsalesoutorder04.ndf',size=5Mb,filegrowth=5mb) to filegroup salesoutorderGroup04 alter database LHhcloud_dev add file (name='orderfile05',filename=N'D:sql_serve_dbsalesoutorder05.ndf',size=5Mb,filegrowth=5mb) to filegroup salesoutorderGroup05 alter database LHhcloud_dev add file (name='orderfile06',filename=N'D:sql_serve_dbsalesoutorder06.ndf',size=5Mb,filegrowth=5mb) to filegroup salesoutorderGroup06 alter database LHhcloud_dev add file (name='orderfile07',filename=N'D:sql_serve_dbsalesoutorder07.ndf',size=5Mb,filegrowth=5mb) to filegroup salesoutorderGroup07 alter database LHhcloud_dev add file (name='orderfile08',filename=N'D:sql_serve_dbsalesoutorder08.ndf',size=5Mb,filegrowth=5mb) to filegroup salesoutorderGroup08 alter database LHhcloud_dev add file (name='orderfile09',filename=N'D:sql_serve_dbsalesoutorder09.ndf',size=5Mb,filegrowth=5mb) to filegroup salesoutorderGroup09 alter database LHhcloud_dev add file (name='orderfile10',filename=N'D:sql_serve_dbsalesoutorder10.ndf',size=5Mb,filegrowth=5mb) to filegroup salesoutorderGroup10
3. 创建分区函数
create partition function [FQ_def] (datetime) as range right for values ('2020-12-01','2021-01-01','2021-03-01','2021-03-20','2021-04-01','2021-04-10', '2021-04-20','2021-05-01','2021-06-01','2021-07-01')
4. 创建分区方案 将分区方案和分区关联
CREATE PARTITION SCHEME [FA_Prog] AS PARTITION [FQ_def]
TO ([PRIMARY], [salesoutorderGroup01], [salesoutorderGroup02], [salesoutorderGroup03], [salesoutorderGroup04], [salesoutorderGroup05],
[salesoutorderGroup06], [salesoutorderGroup07], [salesoutorderGroup08], [salesoutorderGroup09], [salesoutorderGroup010])
5. 表示按照什么进行分区
--CREATE CLUSTERED INDEX IND_UnPartitionBizUser_last_update ON dbo.T_QM_SALESOUTORDER
(
add_time ASC
)
ON FA_Prog(add_time)
6. 基本不需要的步骤,以下步骤忽略
- 将索引放在[PRIMARY]文件组中 CREATE CLUSTERED INDEX [IX_sales_id] ON T_QM_SALESOUTORDER (Id) ON [PRIMARY] -- 由于last_update查询时经常会用到,所以在last_update上也建索引,并且也将各分区的索引放入各分区中 CREATE NONCLUSTERED INDEX [IX_last_time] ON T_QM_SALESOUTORDER (last_update) on [PS_card] (last_update)
推荐学习链接:https://www.cnblogs.com/knowledgesea/p/3696912.html
7. 额外分享:查看sql serve执行计划
set statistics io on select * from dbo.Product set statistics io off