09-SQLServer修改表的存储路径

一、 修改表存储路径的步骤

-- 1.添加文件组

alter database HKTRADE_20211011 add filegroup HisTablespace

--2.添加数据文件到文件组
alter database HKTRADE_20211011 add file(
name=N'HKTRADE_HIS',
filename=N'D:\SQLData\HKTRADE_20211011\HKTRADE_HIS.ndf',
size=20480,
filegrowth=1024MB)
to filegroup [HisTablespace]

--3.修改表的文件组(没有主键需要创建主键转移)
--3.1.添加主键转移
alter table StkListHis2018 add constraint PK__StkListH2018 primary key (
[Date] ASC,
[AcctID] ASC,
[AcctNo] ASC,
[PositionFlag] ASC,
[ExchID] ASC,
[StkID] ASC) on HisTablespace

--3.2.查看转移后的数据文件组
SELECT
OBJECT_NAME(i.OBJECT_ID ) TableName,
OBJECT_SCHEMA_NAME(i.OBJECT_ID) SchemaName,
e.name as group_name,
f.physical_name
FROM sys.indexes AS i
INNER JOIN sys.data_spaces d on i.data_space_id = d.data_space_id
INNER JOIN sys.database_files f on d.data_space_id = f.data_space_id
INNER JOIN sys.filegroups e on d.data_space_id = e.data_space_id
WHERE i.type_desc in ('Heap','Clustered','CLUSTERED COLUMNSTORE')
AND i.OBJECT_ID IN (select OBJECT_ID from sys.tables where name='StkListHis2018')

--4.有主键的表转移存储位置
--4.1.去除原来的主键约束
alter table StkListHis2020 drop constraint PK__StkListH2020__2859310D7E8CC4B1

--4.2.添加主键约束,并指定新的文件组
alter table StkListHis2020 add constraint PK__StkListH2020__2859310D7E8CC4B1 primary key (
[Date] ASC,
[AcctID] ASC,
[AcctNo] ASC,
[PositionFlag] ASC,
[ExchID] ASC,
[StkID] ASC) on HisTablespace

--4.3.查看转移后该表所在的数据文件组
SELECT
OBJECT_NAME(i.OBJECT_ID ) TableName,
OBJECT_SCHEMA_NAME(i.OBJECT_ID) SchemaName,
e.name as group_name,
f.physical_name
FROM sys.indexes AS i
INNER JOIN sys.data_spaces d on i.data_space_id = d.data_space_id
INNER JOIN sys.database_files f on d.data_space_id = f.data_space_id
INNER JOIN sys.filegroups e on d.data_space_id = e.data_space_id
WHERE i.type_desc in ('Heap','Clustered','CLUSTERED COLUMNSTORE')
AND i.OBJECT_ID IN (select OBJECT_ID from sys.tables where name='StkListHis2020')


--5.有主键&非聚集索引的表转移存储位置
--5.1.去除原来的主键约束
alter table LogicalOrder drop constraint PK__LogicalO__3214A5653552E9B6

--5.2.添加主键约束,并指定新的文件组
alter table LogicalOrder add constraint PK__LogicalO__3214A5653552E9B6 primary key (
[Lh] ASC ) on HisTablespace

--5.3.查看转移后该表所在的数据文件组
SELECT
OBJECT_NAME(i.OBJECT_ID ) TableName,
OBJECT_SCHEMA_NAME(i.OBJECT_ID) SchemaName,
e.name as group_name,
f.physical_name
FROM sys.indexes AS i
INNER JOIN sys.data_spaces d on i.data_space_id = d.data_space_id
INNER JOIN sys.database_files f on d.data_space_id = f.data_space_id
INNER JOIN sys.filegroups e on d.data_space_id = e.data_space_id
WHERE i.type_desc in ('Heap','Clustered','CLUSTERED COLUMNSTORE')
AND i.OBJECT_ID IN (select OBJECT_ID from sys.tables where name='LogicalOrder')

--5.4.查看非聚集索引的存储位置
-- 结论:非聚集索引还保留在原来的数据文件组中,没有转移,需要重新创建指定数据文件位置
select data_space_id, * from sys.indexes where object_id='1587028935'

--重新创建非聚集索引,到指定的数据文件组
CREATE NONCLUSTERED INDEX [NonClusteredIndex-20190621-113311] ON [dbo].[LogicalOrder]
(
[ModelOrderId] ASC
)
INCLUDE([CancelQty],[OrdStatus],[StatusDesc]) ON [HisTablespace]


-- 涉及到的系统视图
select * from sys.all_objects where name='STKLIST'
SELECT * FROM sys.indexes WHERE object_id='565577053'
SELECT * FROM sys.data_spaces
select * from sys.filegroups
select * from sys.sysfiles
select * from sys.tables
select OBJECT_ID,* from sys.tables where name='StkListHis2020'

二、总结

1、修改表的物理存储路径,只能修改到文件组级别,不能修改到文件组下的数据文件级别,默认文件组里文件时等比例增长的,所以一个表会写多个文件;

2、修改表的物理存储路径,需要在创建主键约束时指定文件组(本身有主键的要先禁用主键约束),因为主键的存储和数据是在一起,不是单独存储的;

3、如果一张表有除了有主键,还有其他非聚集索引,那么根据主键迁移完表的数据存储路径后,非聚集索引还是在原来的文件组中,需要重新创建非聚集索引(指定新的文件组),才能改变

posted @ 2021-10-20 14:41  佳蓝雨  阅读(470)  评论(0编辑  收藏  举报