zoukankan      html  css  js  c++  java
  • 09-SQLServer修改表的存储路径

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

    -- 1.添加文件组

    alter database HKTRADE_20211011 add filegroup HisTablespace

    --2.添加数据文件到文件组
    alter database HKTRADE_20211011 add file(
    name=N'HKTRADE_HIS',
    filename=N'D:SQLDataHKTRADE_20211011HKTRADE_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、如果一张表有除了有主键,还有其他非聚集索引,那么根据主键迁移完表的数据存储路径后,非聚集索引还是在原来的文件组中,需要重新创建非聚集索引(指定新的文件组),才能改变

  • 相关阅读:
    jupyter中添加conda环境
    MobaXterm的安装和使用(linux上传文件夹)
    CentOS7.5安装python3并设置成系统默认python环境
    Linux系统下conda的安装与使用
    手把手教你Pycharm远程连接服务器端项目进行本地开发调试!
    执行Kakfa Topic创建操作,发现无法创建提示replication factor larger than available brokers
    4. Median of Two Sorted Arrays (Solution 1: Merge Sort)
    636. Exclusive Time of Functions
    477. Total Hamming Distance
    863. All Nodes Distance K in Binary Tree
  • 原文地址:https://www.cnblogs.com/jialanyu/p/15428790.html
Copyright © 2011-2022 走看看