Oracle 表移动后必须做的事

数据库
移动一张表实际上是一个重组过程,数据库会将原来的数据复制到新的地方。但是如果你发现这个表在移动后性能下降了,可能是你的索引没有重建。本文将指导您找到依赖索引并重建它们。

概述

移动一张表实际上是一个重组过程,数据库会将原来的数据复制到新的地方。但是如果你发现这个表在移动后性能下降了,可能是你的索引没有重建。本文将指导您找到依赖索引并重建它们。

将表从示例移动到用户

SQL> select tablespace_name from dba_tables where owner = 'HR' and table_name = 'EMPLOYEES';

TABLESPACE_NAME
------------------------------
EXAMPLE

SQL> alter table hr.employees move tablespace users;

Table altered.

SQL> select tablespace_name from dba_tables where owner = 'HR' and table_name = 'EMPLOYEES';

TABLESPACE_NAME
------------------------------
USERS

查看哪些索引取决于此表

SQL> column index_name format a30;
SQL> column tablespace_name format a30;
SQL> column status format a10;
SQL> select index_name, tablespace_name, status from dba_indexes where owner = 'HR' and table_name = 'EMPLOYEES';

INDEX_NAME TABLESPACE_NAME STATUS
------------------------------ ------------------------------ ----------
EMP_JOB_IX EXAMPLE UNUSABLE
EMP_DEPARTMENT_IX EXAMPLE UNUSABLE
EMP_MANAGER_IX EXAMPLE UNUSABLE
EMP_NAME_IX EXAMPLE UNUSABLE
EMP_EMAIL_UK EXAMPLE UNUSABLE
EMP_EMP_ID_PK EXAMPLE UNUSABLE

6 rows selected.

如您所见,所有依赖索引都是UNUSABLE。这意味着,数据库不会自动重建它们。你必须自己做。

编写所有重建语句,然后执行它们

SQL> select 'alter index ' || owner || '.' ||index_name || ' rebuild tablespace users;' as SQL_TO_BE_EXECUTED from dba_indexes where owner = 'HR' and table_name = 'EMPLOYEES';

SQL_TO_BE_EXECUTED
--------------------------------------------------------------------------------
alter index EMP_JOB_IX rebuild tablespace users;
alter index EMP_DEPARTMENT_IX rebuild tablespace users;
alter index EMP_MANAGER_IX rebuild tablespace users;
alter index EMP_NAME_IX rebuild tablespace users;
alter index EMP_EMAIL_UK rebuild tablespace users;
alter index EMP_EMP_ID_PK rebuild tablespace users;

6 rows selected.

或者您可以重建原始表空间的索引。

SQL> select 'alter index ' || owner || '.' ||index_name || ' rebuild tablespace ' || tablespace_name || ';' as SQL_TO_BE_EXECUTED from dba_indexes where owner = 'HR' and table_name = 'EMPLOYEES';

SQL_TO_BE_EXECUTED
--------------------------------------------------------------------------------
alter index HR.EMP_DEPARTMENT_IX rebuild tablespace EXAMPLE;
alter index HR.EMP_NAME_IX rebuild tablespace EXAMPLE;
alter index HR.EMP_MANAGER_IX rebuild tablespace EXAMPLE;
alter index HR.EMP_EMP_ID_PK rebuild tablespace EXAMPLE;
alter index HR.EMP_EMAIL_UK rebuild tablespace EXAMPLE;
alter index HR.EMP_JOB_IX rebuild tablespace EXAMPLE;

6 rows selected.

请注意,我们在新表空间USERS中重建索引。也就是说,对于索引,REBUILD相当于表中的MOVE

重建后检查状态

SQL> select index_name, tablespace_name, status from dba_indexes where owner = 'HR' and table_name = 'EMPLOYEES';

INDEX_NAME TABLESPACE_NAME STATUS
------------------------------ ------------------------------ ----------
EMP_JOB_IX USERS VALID
EMP_DEPARTMENT_IX USERS VALID
EMP_MANAGER_IX USERS VALID
EMP_NAME_IX USERS VALID
EMP_EMAIL_UK USERS VALID
EMP_EMP_ID_PK USERS VALID

6 rows selected.

所有索引都变为VALID,表明所重建的索引有效。

责任编辑:赵宁宁 来源: 今日头条
相关推荐

2011-04-27 09:22:44

Ubuntu 11.0

2013-11-08 10:42:31

Ubuntu 13.1

2013-01-14 17:11:24

2013移动开发者开发者

2010-07-27 11:29:43

Flex

2013-12-05 17:07:29

openSUSEopenSUSE 13安装

2013-07-11 10:07:46

Fedora 19

2017-11-14 07:05:26

物联网IT高管数字化

2010-03-24 09:42:12

Oracle数据库

2020-12-29 09:50:23

大数据大数据技术

2010-04-12 14:58:56

Meego开发

2010-04-26 13:38:34

Oracle dele

2011-08-04 08:54:31

2012-08-30 09:41:23

移动应用开发

2011-02-15 13:50:01

FreeBSDports

2010-08-12 14:13:01

Flex开发者

2015-11-11 09:12:47

2022-11-21 10:43:55

首席信息官IT 领导者

2012-02-23 13:01:12

JavaPlay Framew

2013-01-06 10:51:56

2011-12-16 17:05:58

点赞
收藏

51CTO技术栈公众号