达梦数据库表空间存储位置调整
达梦数据库中,表空间是存储数据的逻辑单位,由数据文件组成。常见的表空间类型包括ROLL表空间、SYSTEM表空间、MAIN表空间和TEMP表空间。调整表空间存储位置可以优化数据库性能和数据完整性。
表空间介绍及需求
ROLL表空间:用于存储事务回滚信息,确保数据库事务的原子性和持久性。
SYSTEM表空间:存储数据库的系统信息,如数据字典、系统表和视图等。
TEMP表空间:用于存储临时数据,如排序或哈希操作产生的中间结果。
MAIN表空间:主要存放业务数据,用户可以在其中创建各种数据库对象。
调整表空间存储位置的步骤
查询当前数据文件路径:使用SYSDBA账户连接到数据库,查询数据文件路径。
数据库备份:在更改数据目录路径之前,确保数据库已创建完整备份。
生成映射文件:通过dmrman工具生成全量备份的映射文件。
编辑映射文件:使用文本编辑器修改映射文件内的数据文件路径。
还原备份:使用dmrman工具将备份与映射文件一起还原和恢复。
开启数据库并检查日志:重启数据库并检查日志,确认表空间数据文件路径是否已成功修改。
注意事项
使用dmrman工具进行文件映射时,操作过程标准化且安全性较高,但在数据量大时可能导致较长的备份和恢复时间。
同步归档模式是新版本特性,老版本不支持。
ALTER_TABLE_OPT参数可以优化ALTER TABLE操作,特别是在添加列时,设置为3可以实现快速添加列,即使有默认值也不会影响性能
--------------------查看表空间使用情况
SELECT
TABLESPACE_NAME AS 表空间名,
ROUND(SUM(TOTAL_SIZE) / 1024.0 / 1024.0, 2) AS "总大小(MB)",
ROUND(SUM(USED_SIZE)/ 1024.0 / 1024.0, 2) AS "已使用大小(MB)",
CAST(ROUND(SUM(USED_SIZE) * 1.0 / SUM(TOTAL_SIZE) * 100,2) AS VARCHAR) || '%' AS 使用率
FROM
(
SELECT
UPPER(T1.TABLESPACE_NAME) AS TABLESPACE_NAME,
T1.BYTES AS TOTAL_SIZE,
T1.BYTES - T2.BYTES AS USED_SIZE
FROM
DBA_DATA_FILES T1,
DBA_FREE_SPACE T2
WHERE
T1.TABLESPACE_NAME = T2.TABLESPACE_NAME
AND T1.FILE_ID = T2.FILE_ID
)
GROUP BY
TABLESPACE_NAME;
--------------------查看表空间同数据文件对应关系
SELECT
TS.NAME AS 表空间名,
DF.PATH AS 数据文件
FROM
V$TABLESPACE AS TS,
V$DATAFILE AS DF
WHERE
TS.ID = DF.GROUP_ID
ORDER BY
1;
--------------------查看所有用户、创建时间、默认表空间、等基本信息
SELECT
USERNAME AS 用户名,
DEFAULT_TABLESPACE AS 默认数据表空间,
DEFAULT_INDEX_TABLESPACE AS 默认索引表空间,
TEMPORARY_TABLESPACE AS 临时表空间,
DECODE(ACCOUNT_STATUS,'OPEN','正常','LOCKED','锁定','未知') AS 用户状态,
CREATED AS 创建时间
FROM
DBA_USERS;
--------------------查看用户数据库限制
SELECT
T1.USERNAME AS 用户名,
DECODE(T2.AUTHENT_TYPE,1,'数据库密码认证',2,'操作系统认证',3,'远程认证','未知认证方式') AS 用户认证方式,
T2.SESS_PER_USER AS 用户最大会话数,
T2.CONN_IDLE_TIME AS "用户空闲期(分钟1-1440)",
T2.FAILED_NUM AS 用户登录失败次数限制,
T2.LIFE_TIME AS "口令有效期(天0-365)",
T2.REUSE_TIME AS "口令等待期(天0-365)",
T2.REUSE_MAX AS 口令变更次数,
T2.LOCK_TIME AS "用户锁定时间(分1-1440)",
T2.GRACE_TIME AS "口令宽限期1-30",
T2.PASSWORD AS 密码策略,
T2.RN_FLAG AS 只读,
T2.ALLOW_ADDR AS 允许访问的IP,
T2.NOT_ALLOW_ADDR AS 不允许访问的IP,
T2.ALLOW_DT AS 允许访问的时间,
T2.NOT_ALLOW_DT AS 不允许访问的时间,
T2.LAST_LOGIN_DTID AS 上次登录时间,
T2.LAST_LOGIN_IP AS 上次登录IP,
T2.FAILED_ATTEMPS AS 自上一次登录成功以来失败次数
FROM
DBA_USERS T1,
SYSUSERS T2
WHERE
T1.USER_ID = T2.ID;
--------------------查看用户对象统计信息
SELECT
S2.NAME AS 模式名,
S1.TYPE$ AS 主类型,
S1.SUBTYPE$ AS 子类型,
COUNT(*) AS 对象数量
FROM
SYSOBJECTS S1,
SYSOBJECTS S2
WHERE
S1.SCHID = S2.ID
AND S2.TYPE$ = 'SCH'
GROUP BY
S2.NAME,
S1.TYPE$,
S1.SUBTYPE$
ORDER BY
S2.NAME,
S1.TYPE$,
S1.SUBTYPE$;
--------------------查看用户占用空间大小
SELECT
USERNAME AS 用户名,
USER_USED_SPACE(USERNAME) * PAGE / 1024.0 / 1024.0 AS "用户占用空间(MB)"
FROM
DBA_USERS
ORDER BY
2 DESC;
--------------------统计所有用户表行数
SELECT
T2.NAME AS 模式名,
T1.NAME AS 表名,
TABLE_ROWCOUNT(T2.NAME,T1.NAME) AS 行数
FROM
SYSOBJECTS T1,
SYSOBJECTS T2
WHERE
T1.SCHID = T2.ID
AND T1.SUBTYPE$ = 'UTAB'
AND T2."TYPE$" = 'SCH'
ORDER BY 3 DESC;
--------------------统计所有用户表行数以及筛查某行数级别以上表行数 输入参数1:百万,千万,亿...等等,也可以共存,复制一行
SELECT
模式名,
COUNT(表名) AS 表数量,
COUNT(CASE WHEN 行数 > ? THEN 行数 ELSE NULL END) AS 百万表数量
FROM
(
SELECT
T2.NAME AS 模式名,
T1.NAME AS 表名,
TABLE_ROWCOUNT(T2.NAME,T1.NAME) AS 行数
FROM
SYSOBJECTS T1,
SYSOBJECTS T2
WHERE
T1.SCHID = T2.ID
AND T1.SUBTYPE$ = 'UTAB'
AND T2."TYPE$" = 'SCH'
--ORDER BY 3 DESC
)
GROUP BY 模式名
--------------------获取所有用户表定义
SELECT
T2.NAME AS 模式名,
T1.NAME AS 表名,
T1.CRTDATE AS 创建时间,
DBMS_LOB.SUBSTR(DBMS_METADATA.GET_DDL('TABLE',T1.NAME,T2.NAME)) AS 表定义
FROM
SYSOBJECTS T1,
SYSOBJECTS T2
WHERE
T1."SUBTYPE$" = 'UTAB'
AND T1.SCHID = T2.ID
AND T2."TYPE$" = 'SCH';
--------------------统计用户表列信息
SELECT
S1.NAME AS 表名,
S2.NAME AS 列名,
S2."TYPE$" AS 字段类型,
S2."LENGTH$" AS 字段长度
FROM
SYSOBJECTS S1,
SYSCOLUMNS S2
WHERE
S1."SUBTYPE$" = 'UTAB'
AND S1.ID = S2.ID;
--------------------查看表占用空间大小
SELECT
S2.NAME AS 模式名,
S1.NAME AS 表名,
TABLE_USED_SPACE(S2.NAME,S1.NAME) * PAGE /1024.0/1024.0 AS "表占用空间(MB)"
FROM
SYSOBJECTS S1,
SYSOBJECTS S2
WHERE
S1.SCHID = S2.ID
AND S1."SUBTYPE$" = 'UTAB'
AND S2."TYPE$" = 'SCH'
ORDER BY
3 DESC;
文章
阅读量
获赞