在 Oracle 11g 中如何将分区表从一个表空间移动到另一个表空间?
我有一个属于表空间report 的分区表。我想将其移至表空间记录。
一种可能性是删除表并在新表空间中重新创建它,但这对我来说不是一个选择,因为表中的数据需要在移动后继续存在。
我首先检查分区是否确实属于表空间报告:
SELECT * FROM user_tab_partitions WHERE table_name = 'REQUESTLOG';
然后我只是尝试:
ALTER TABLE requestLog MOVE TABLESPACE record;
但这给了我错误 ORA-145111“无法对分区对象执行操作”。
然后我发现我可以使用以下方法移动各个分区:
ALTER TABLE requestLog MOVE PARTITION "2009-12-29" TABLESPACE report;
但是由于表有 60 个分区(基于日期),并且因为我可能必须对多个系统执行此操作,所以我想循环遍历所有分区名称,将每个移动到新的表空间。我尝试过,但无法让 SQL 正常工作。
即使我将所有现有分区移动到新表空间,创建新分区时仍然存在问题。新分区仍然在旧表空间报告中创建。如何更改以便在新表空间记录中创建新分区?
I have a partitioned table that belongs to tablespace report. I want to move it to tablespace record instead.
One possibility is to drop the table and recreate it in the new tablespace, but that is not an option for me, since there is data in the table that needs to survive the move.
I started by checking that the partitions actually belong to tablespace report with:
SELECT * FROM user_tab_partitions WHERE table_name = 'REQUESTLOG';
Then I just tried:
ALTER TABLE requestLog MOVE TABLESPACE record;
But that gives me error ORA-145111 “cannot perform operation on a partitioned object”.
Then I found out that I can move individual partitions using:
ALTER TABLE requestLog MOVE PARTITION "2009-12-29" TABLESPACE report;
But since there are 60 partitions of the table (based on date), and because I may have to do this for several systems, I would like to loop over all the partition names, moving each to the new tablespace. I tried that, but couldn’t quite get the SQL to work.
Even if I move all the existing partitions to the new tablespace, there is still a problem when creating new partitions. The new partitions are still created in the old tablespace report. How do I change so that new partitions are created in the new tablespace record?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您还必须考虑可能无效的索引 - 除此之外,为了涵盖有关重置默认表空间的问题,我认为这是您想要实现的完整过程:
1)移动分区(a PL/SQL 循环按照 zürigschnäzlets 的回答)
这些是我在定义 a_tname、a_destTS、vTname 和 vTspName 的匿名块包装器中使用的过程 - 它们应该为您提供总体思路:
2)设置表默认分区表空间,以便在其中创建新分区:
3) 设置索引默认分区表空间,以便在您想要的位置创建新索引分区(如果有):
4) 重建任何分区需要重建且不在所需表空间中的分区索引:
5) 重建任何全局索引
You have to consider indexes that may be invalidated as well - to cover your question about resetting the default tablespaces in addition to this, I think this is the full process that you'll want to implement:
1) Move partitions (a PL/SQL loop as per zürigschnäzlets' answer)
These are procedures I use within an anonymous block wrapper that defines a_tname, a_destTS, vTname, and vTspName - they should give you the general idea:
2) Set table default partition tablespace so new partitions are created there:
3) Set index default partition tablespace so new index partitions (if any) are created where you want them:
4) rebuild any partitioned indexes that need rebuilding and are not in the desired tablespace:
5) Rebuild any global indexes
您可以使用 PL/SQL 执行此操作,也可以使用 sql 生成语句。我决定使用简单的 SQL 生成 alter table 语句:
您可以执行上一条语句的输出。
每个用户都有一个默认表空间。如果在创建/更改时未指定任何其他内容,则会在该默认表空间中创建新的数据库对象
You can either do it with PL/SQL or generate the statements with sql. I decided to generate the alter table statements with simple SQL:
You can execute the output from the previous statement.
Every user has a default tablespace. New database objects are created in that default tablespace if nothing else is specified on creation/alteration
在表空间内移动数据的最简单方法:
移动所有非分区表
分区表
非分区索引
分区索引
The easiest way to move the data within tablespaces:
Moving all non-partitioned tables
Partitioned tables
Non-partitioned indexes
Partitioned indexes
如果这是一个选项,最简单的方法可能是重命名表(
ALTER TABLE requestLog
),在正确的表空间中创建包含所有索引的相同表,并从旧表中复制数据:重命名为 requestLogTmp;
当一切正常运行时,您可以删除旧表。
If this is an option, the easiest way could be to rename the table (
ALTER TABLE requestLog
), create the same table with all indexes in the correct tablespace and copy the data from the old table:RENAME TO requestLogTmp;
When everything is up and running, you can drop the old table.