|
--------------------------------------------------------------------------------
***Checked for relevance on 02-Nov-2011***
=========
Abstract:
=========
This is intended for any DBA who wishes to move tables across tablespace.
NOTE:
This article covers also information as was published in previously available Note 158162.1
This article covers also information as was published in previously available Note 158162.1
There are 2 methods we can use to do this.
"alter table X move tablespace Y" and "alter index X rebuild tablespace Y"
-- this works in Oracle8i release 8.1 and up ONLY.
The other method is an Export/Import
When deciding which method is best for your situation, please consider the following:
ALTER TABLE MOVE TABLESPACE METHOD:
===================================
===================================
We will start with the basic syntax below followed by a script that you can cut and paste:
Syntax
-----------
alter table move storage() tablespace
-----------
alter table
Example
-------------
SQL> create table ftab(x number) storage(initial 20K next 20K) tablespace users;
-------------
NOTE:
Indexes depend on the rowid information and therefore they will become unusable.
You will get an ORA-1502 unless you rebuild the associated index(es).
Attached is a script called moveall.sql. The data extracted from this must be used with caution.
It begins by moving a table and then rebuilding each of the indexes on that
table.
moving a table, this script rebuilds them right after moving a table -- before moving
the next table (to reduce downtime).
===========
Disclaimer:
===========
This script is provided for educational purposes only. It is NOT supported by
Oracle World Wide Technical Support.
to work as intended.
relying on it.
PROOFREAD THIS SCRIPT PRIOR TO USING IT! Due to differences in the way text
editors, email packages and operating systems handle text formatting (spaces,
tabs and carriage returns), this script may not be in an executable state when
you first receive it.
type are corrected.
Warning:
------------
Running the moveall.sql script is harmless as it is written. It generates the
SQL you need to run and saves the sql into yet another script file "tmp.sql".
You should edit tmp.sql, review it, modify it if you want (for example: if you have a
multi-cpu system, you could modify the index rebuilds to be "parallel N",
"unrecoverable" and add other options to make them go faster on your system),
and then run it.
------------
Running the moveall.sql script is harmless as it is written.
SQL you need to run and saves the sql into yet another script file "tmp.sql".
You should edit tmp.sql, review it, modify it if you want (for example: if you have a
multi-cpu system, you could modify the index rebuilds to be "parallel N",
"unrecoverable" and add other options to make them go faster on your system),
and then run it.
=========
Script1:
=========
----------- cut ---------------------- cut -------------- cut --------------
Script1:
=========
----------- cut ---------------------- cut -------------- cut --------------
It uses the user_segments table to generate all of the needed "alter table move"
and "alter index rebuild" statements to move a table/index into another
tablespace preserving the storage characteristics currently assigned to the
object.
SQL> @moveall
SQL> set echo off
SQL> set echo off
IMPORT/EXPORT METHOD:
=====================
=====================
Another method would be to use EXPort and IMPort.
This is a basic outline of the standard method:
Please see the examples below for more detailed instructions.
Please see the examples below for more detailed instructions.
EXAMPLES:
This section discusses three ways you can move tables from one tablespace to
another tablespace using Export/Import.
A. On a Per Table Basis
B. On a Per User Basis
C. From user A to user B
These steps require a SQL*Plus account with DBA privileges to set-up the appropriate
user privileges.
user privileges.
A. Moving Tables on a Per Table Basis
-------------------------------------
The following steps will move tables on a per table basis:
1. Check the tablespaces in use and perform the table level export.
2. If you have enough space in the database, rename the table(s) and drop the
indexes. After the table has been reimported successfully then drop the
.
3. Run import with INDEXFILE= to get the create table and create
index statements.
4. Edit the resulting file, and set the tablespace clause to indicate the new
tablespace. Delete the create index statements.
5. Grant quota on the new tablespace.
6. Run the edited create script to create the table(s).
7. Run import with IGNORE=Y to populate the new table(s) and create the index(es).
B. Moving Tables on a Per User Basis
------------------------------------
The following steps will move tables on a per user basis:
1. Perform a user level or full database export.
2. Drop or rename the table(s) you are moving.
3. For the user with the tables that are being moved, perform the following:
4. Test to make sure that the user can no longer create objects in the 'old'
5. Perform a user level import of this user.
6. Regrant the privileges that were revoked in step 3, if required.
C.
---------------------------------------
IMPORT will always import tables into a tablespace that has the same name as
the original tablespace (in the original database where the EXPORT was done
from), regardless of what is userB's default tablespace.
the original tablespace (in the original database where the EXPORT was done
from), regardless of what is userB's default tablespace.
This assumes the destination database has a tablespace with the same name as
the original tablespace from which userA's tables were EXPORTed.
the original tablespace from which userA's tables were EXPORTed.
Consider the following:
An Export has been done in database A of userA's tables, which are in tablespace
USER_A_TS. You are attempting to import into database B into userB's schema
which is in tablespace USER_B_TS.
USER_A_TS.
which is in tablespace USER_B_TS.
You do the IMPORT with FROMUSER=USERA TOUSER=USERB but userA's tables still are
being put in tablespace USER_A_TS and not in userB's default tablespace of
USER_B_TS.
being put in tablespace USER_A_TS and not in userB's default tablespace of
USER_B_TS.
The following steps will move tables from userA tablespace USER_A_TS to userB
tablespace USER_B_TS:
tablespace USER_B_TS:
1. Perform a user level export for user_a.
2. For userB, check tablespace quotas on tablespaces USER_A_TS and USER_B_TS.
3. Test to make sure that the user can no longer create objects in the 'old'
tablespace.
Create a table and specify the old tablespace:
4. Perform the import.
5. Set userB's quotas back if needed:
EXAMPLES
A. On a Per Table Basis
-----------------------
-----------------------
1. Check the tablespaces in use and perform the table level export
2. Drop or rename the table you wish to move
3. Run import with INDEXFILE= to get a file with the create table
and index statements.
4. Using an editor (like ?vi?) to make the following changes:
5. Grant quota on the new tablespace
6. Run the script to create the tables
7. Run the import with IGNORE=Y to populate the new table(s) and create the
B. On a Per User Basis
----------------------
----------------------
1. Perform a user level or full database export
2. Drop or rename the table(s) you are moving
3. Grant quota on the new tablespace
4. Test to make sure that the user can no longer create objects in the old?
tablespace. Create a table and specify the old tablespace.
SQL> CONN scott/tiger
SQL> CREATE TABLE test (a varchar2(10)) tablespace users;
*
ERROR at line 1:
ORA-01536: space quota exceeded for tablespace 'USERS'
5. Perform the import with IGNORE=YES
6. Re-grant the privileges that were revoked in step 3, if required.
C. From user A to user B
------------------------
The following steps will move tables from userA tablespace USER_A_TS
to userB tablespace USER_B_TS:
to userB tablespace USER_B_TS:
1. Perform a user level export for user_a
2. For userB check tablespace quotas on tablespaces USER_A_TS and USER_B_TS
3. Test to make sure that the user can no longer create objects in the ?old?
4. Perform the import.
5. Re-grant the privileges that were revoked in step 2, if required.