How to Move Table…
来源:互联网 发布:施耐德m258编程软件 编辑:程序博客网 时间:2024/06/06 03:43
--------------------------------------------------------------------------------
***Checked for relevance on 02-Nov-2011***
=========
Abstract:
=========
This is intended for any DBA who wishes to move tables acrosstablespace.
NOTE:
This article covers also information as was published in previouslyavailable Note 158162.1
This article covers also information as was published in previouslyavailable Note 158162.1
There are 2 methods we can use to do this.
"alter table X move tablespace Y" and "alter index X rebuildtablespace Y"
-- this works in Oracle8i release 8.1 and upONLY.
The other method is an Export/Import
When deciding which method is best for your situation, pleaseconsider the following:
ALTER TABLE MOVE TABLESPACE METHOD:
===================================
===================================
We will start with the basic syntax below followed by a scriptthat 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 willbecome unusable.
You will get an ORA-1502 unless you rebuild the associatedindex(es).
Attached is a script called moveall.sql. The data extracted fromthis must be used with caution.
It begins by moving a table and then rebuilding each of the indexeson that
table.
moving a table, this script rebuilds them right after moving atable -- before moving
the next table (to reduce downtime).
===========
Disclaimer:
===========
This script is provided for educational purposes only. It is NOTsupported by
Oracle World Wide Technical Support.
to work as intended.
relying on it.
PROOFREAD THIS SCRIPT PRIOR TO USING IT! Due to differences in theway text
editors, email packages and operating systems handle textformatting (spaces,
tabs and carriage returns), this script may not be in an executablestate when
you first receive it.
type are corrected.
Warning:
------------
Running the moveall.sql script is harmless as it iswritten. 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 (forexample: 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 onyour system),
and then run it.
------------
Running the moveall.sql script is harmless as it iswritten.
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 (forexample: 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 onyour 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 intoanother
tablespace preserving the storage characteristics currentlyassigned to the
object.
SQL> @moveall
SQL> set echo off
SQL> set echo off
IMPORT/EXPORT METHOD:
=====================
=====================
Another method would be to use EXPort andIMPort. This is a basic outline of the standardmethod:
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 onetablespace 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 toset-up the appropriate
user privileges.
user privileges.
A. Moving Tables on a Per Table Basis
-------------------------------------
The following steps will move tables on a per tablebasis:
1. Check the tablespaces in use and perform the table levelexport.
2. If you have enough space in the database, rename thetable(s) and drop the
indexes. After the table hasbeen reimported successfully then drop the
.
3. Run import with INDEXFILE= to get the create table andcreate
index statements.
4. Edit the resulting file, and set the tablespace clause toindicate the new
tablespace. Delete the createindex 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) andcreate the index(es).
B. Moving Tables on a Per User Basis
------------------------------------
The following steps will move tables on a per userbasis:
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, performthe following:
4. Test to make sure that the user can no longer create objects inthe 'old'
5. Perform a user level import of this user.
6. Regrant the privileges that were revoked in step 3, ifrequired.
C.
---------------------------------------
IMPORT will always import tables into a tablespace that hasthe same name as
the original tablespace (in the original database where the EXPORTwas done
from), regardless of what is userB's default tablespace.
the original tablespace (in the original database where the EXPORTwas done
from), regardless of what is userB's default tablespace.
This assumes the destination database has a tablespace withthe same name as
the original tablespace from which userA's tables wereEXPORTed.
the original tablespace from which userA's tables wereEXPORTed.
Consider the following:
An Export has been done in database A of userA's tables, whichare in tablespace
USER_A_TS. You are attempting to import intodatabase 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'stables still are
being put in tablespace USER_A_TS and not in userB's defaulttablespace of
USER_B_TS.
being put in tablespace USER_A_TS and not in userB's defaulttablespace of
USER_B_TS.
The following steps will move tables from userA tablespaceUSER_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_TSand USER_B_TS.
3. Test to make sure that the user can no longer createobjects 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 levelexport