Row movement in Oracle (Oracle creates partitions Based on enumeration fields)

Source: Internet
Author: User
One of the relatively newer features in Oracle concerns the moving
Rows. Why wowould a row move and who or what controls that movement?
Furthermore, by "move," what exactly does move mean? Does a row move
Another table, or is row movement constrained to the row's container
(I. e., a table )? An early use of row movement was highlighted in
Oracle8i, and row movement then, as well as now, applied to moving rows
In a partitioned table. With newer releases of the Oracle RDBMS, where
Else does row movement come into play, and are there any gotcha's
Respect to row movement operations?

This article looks at three common cases or situations where row movement needs to be enabled.

Partitioned Tables
As far back as Oracle 8.1.5 documentation, row movement applied
Updatable partition keys. A list partition where the partition key is
Specific value is a good example of this. Partition partitioned table
Examples use regions, cities and states as list examples. What happens
If you use a city as a partition key and an office in that city moves
Elsewhere? Or two offices in the same city (from different groups or
Business Units within the same company) merge into one location? You
Cocould split the default partition and add the new location name. How
Wocould you move records from the old partitioned into the new one? Short
Of deleting from one partition and inserting same into a new one,
Wouldn't it be easier to be able to perform a single update?

Let's create a quick partitioned table example and see how moving a row works.

SQL> Create Table city_offices
2 (
3 office_number number not null,
4 city_id varchar2 (12) not null,
5 office_name varchar2 (30) not null
6)
7 partition by list (city_id)
8 (
9 partition p282 values ('20170901 '),
10 partition p283 values ('20140901 '),
11 partition p284 values ('20140901 '));

Table created.

SQL>
SQL> insert into city_offices values (1, '20140901', 'Denver ');

1 row created.

SQL> insert into city_offices values (2, '20140901', 'Denver tech ctr ');

1 row created.

SQL> insert into city_offices values (3, '20140901', 'Denver West ');

1 row created.

SQL> insert into city_offices values (4, '20140901', 'broomfield ');

1 row created.

SQL> commit;

Commit complete.

All of the cities are located in the Denver area (city id of 282 in
This example). broomfield is further north of the Denver metro area and
Has been slated to become part of the boulder Area Office group (using
A city id of 283). Let's try and move it by updating the partition key
Value for that office.

SQL> Update city_offices set city_id = '000000' where office_number = 4;
Update city_offices set city_id = '000000' where office_number = 4
*
Error at line 1:
ORA-14402: updating partition key column wocould cause a partition change

Why didn't the update work? The error message doesn' t come right out
And say it, but the reason is that row movement is not enabled. How
Wocould you know this right off the bat? The reason why you wowould know
This is due to the fact that row movement-by default-is not
Enabled. A simple alter table statement remedies the update problem.

SQL> ALTER TABLE city_offices enable row movement;

Table altered.

SQL> Update city_offices set city_id = '000000' where office_number = 4;

1 row updated.

SQL> commit;

Commit complete.

SQL> ALTER TABLE city_offices disable row movement;

Table altered.

Why did I disable the row movement? Well, tables are partitioned for
Reason, and to help ensure data goes where it is supposed to, you
Shocould disallow inadvertent updates or row movements. obviusly, if
Your application requires those types of updates, this guideline
Wouldn't necessarily apply to you.

Flashback
Being able to flashback DML operations has greatly forced ced
Frequency of how often you hear a DBA say, "oops." In the next example,
I'll delete a row, commit the operation, and try to recover the deleted
Record.

SQL> Delete from city_offices where office_number = 1;

1 row deleted.

SQL> commit;

Commit complete.

SQL> flashback table city_offices
2 To timestamp (effecimestamp-interval '05 'minute );
Flashback table city_offices
*
Error at line 1:
ORA-08189: cannot flashback the table because row movement is not enabled

The error message in this case is much clearer as to the nature of
Failed statement. I find it handy to keep the flashback syntax around
In an easily (and quickly) identifiable location, and that is chiefly
Because I don't use timestamps that much in my applications. Time is
Still Somewhat of the essence and the quicker you can recover the table
To a good state, the better.

SQL> ALTER TABLE city_offices enable row movement;

Table altered.

SQL> flashback table city_offices
2 To timestamp (effecimestamp-interval '05 'minute );

Flashback complete.

SQL> select * From city_offices;

Office_number city_id office_name
-------------------------------------------------------
1 282 Denver
2 282 Denver tech CTR
3 282 Denver West
4 283 broomfield

If the table has row movement disabled, why is it you can drop
Table and flashback the table to before the drop without row Movement
Being enabled?

SQL> ALTER TABLE city_offices disable row movement;

Table altered.

SQL> drop table city_offices;

Table dropped.

SQL> flashback table city_offices to before drop;

Flashback complete.

Chapter 15 in the database concepts guide states the following:

For flashback table to succeed, the system must retain enough undo
Information to satisfy the specified SCN or timestamp, and
Integrity constraints specified on the tables cannot be violated. Also,
Row movement must be enabled.

The example above shows that "row movement must be enabled" is not entirely true.

Space Management
The third and final example of where row movement comes into play can
Be Found in shrink operations. If you think about it, shrinking a table
May entail moving data around within a table (handled internally
Oracle), so the idea of a row moving around makes sense. First, let's
Get a record of the current rowids for each office and then delete two
Rows.

SQL> select rowid, office_number from city_offices;

Rowid office_number
-------------------------------
Aaansfaaeaaaeanaaa 1
Aaansfaaeaaaeanaad 2
Aaansfaaeaaaeanaae 3
Aaansgaaeaaaeavaaa 4

SQL> Delete from city_offices where office_number in (2, 3 );

2 rows deleted.

SQL> commit;

Commit complete.

Even in this small table, we shocould be able to reclaim some space, so let's try shrinking the table.

SQL> ALTER TABLE city_offices shrink space;
Alter table city_offices shrink Space
*
Error at line 1:
ORA-10636: Row movement is not enabled

Notice that the error number is different from the flashback example,
But the message is pretty clear. We'll alter the table and perform
Shrink operation.

SQL> ALTER TABLE city_offices enable row movement;

Table altered.

SQL> ALTER TABLE city_offices shrink space;

Table altered.

Now that the shrink has taken place, let's examine the rowids for
Remaining two rows. Will the rowids be the same or different?

SQL> select rowid, office_number from city_offices;

Rowid office_number
-------------------------------
Aaansfaaeaaaeakaaa 1
Aaansgaaeaaaeasaaa 4

Interestingly enough, the rowids for the two remaining rows are
Different from their original ids before the delete statement. You may
Have Expected office_number 4's rowid to change, but not office_number
1's, but it too changed location after the shrink.

The moral of the story here supports what the documentation says (in more than one place ):

Before you use rowids in DML statements, they shocould be verified and
Guaranteed not to change. The intended rows shoshould be locked so they
Cannot be deleted. Under some circumstances, requesting data with
Invalid rowid cocould cause a statement to fail.

Using rowids to perform DML on records can be wicked fast, and is
Ally the fastest way to access a row. However, if there is any
Chance that someone else's operation (and even yours) can alter
Rowids of a table, you can find yourself with lots of messy data.
Further, a shrink operation (without the Compact option) can invalidate
Open cursors. That cocould spell trouble for an application.

In closing
We looked at three major operations where row movement is required:
Partition key value change, flashback and space management. Enabling
And disabling row movement is very simple to implement, and in most
Cases, has no unwanted side effects. space management operations using
The shrink option can have unintended consequences on other users and
Operations, and as we all know, good deeds (cleaning up space) shocould
Never go unpunished. Using row movement and understanding what takes
Place makes this feature an invaluable asset in your administration
Arsenal, and it is up to you to be careful so as not to be bitten by
Changed rowid.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.