0% found this document useful (0 votes)
159 views5 pages

Class02 DDL DMC TCL HWM

1. DDL commands like ALTER TABLE commit data before performing the DDL operation, so a rollback will not impact the data. 2. Increasing a column size is easy, but decreasing requires checking all records which can be slow. 3. TRUNCATE resets the high water mark (HWM), making space reusable, while DELETE does not reset HWM. ROLLBACK also does not work with TRUNCATE or DROP as they are DDL.

Uploaded by

K Harish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
159 views5 pages

Class02 DDL DMC TCL HWM

1. DDL commands like ALTER TABLE commit data before performing the DDL operation, so a rollback will not impact the data. 2. Increasing a column size is easy, but decreasing requires checking all records which can be slow. 3. TRUNCATE resets the high water mark (HWM), making space reusable, while DELETE does not reset HWM. ROLLBACK also does not work with TRUNCATE or DROP as they are DDL.

Uploaded by

K Harish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

--> We discussed in last class this, but just a reminder as this is very important

question

suppose we insert data in the table and after that we fire a DDL command, so DDL will
first commit the data. This is the nature of DDL commands. then after if we do rollback,
it does not impACT ANYTHING, BECAUSE DATA IS COMMITTED.

Modify Table:

--> we can modify the type size of any column using the alter table modify command, we can
increase it easily but can not decrease it if any data exists because it will check the
existing data and if the size is greater than that then deny to decrease.
--> Increasing the length is easy, straight forward, but in case of size reduction, it
will check each and every record of the database. It will be a slow and time taking
process. Advisable in downtime only. 

==================================================================================

Delete/Truncate/DROP

1. delete is dml command and truncate is ddl command


2. delete can be rollback/commit but truncate can not be rollback or commit. it is
autocommit
3. we can add condition like where clause in delete, but there is no condition in truncate
4. drop is also a ddl command and can not be rollback and no condition is drop also
5. when we drop a table, the whole table structure is gone but when we truncate only data
is gone, table structure remain in database
6. Truncate is much faster than DELETE
7. Truncate will reset the HWM but delete will not.

--> delete -> to delete data from table


truncate --> to completely delete data from table , Reset the HWM(high water mark)

What is HWM

HWM is the last block of database where data is written.


For example assume that you have a million row table that takes 30 seconds to read.  After deleting 900,000 rows, a full
scan on the table will still take 30 seconds.  This is because the table high water mark is not re-set after delete operations.
As such, there are no easy SQL scripts that will reveal the high water mark for an Oracle table, but you can assume that it
is the last extent that was allocated to the table for estimation purposes. 

High water mark is the maximum amount of database blocks used so far by a segment. This mark cannot be reset by delete
operations. Does not matter , you delete and commit, HWM not reset. 
The high water mark level is just a line separate the used blocks and free blocks.

The blocks above the HWM level is free blocks, they are ready to use.
The blocks below the HWM level is used blocks, they are already used.

For example, if you delete some huge records from the database, that data will delete but the blocks are not ready to used,
because that blocks are still below HWM level, so delete command never reset the HWM level,

At the same time you truncate the date, then the data will delete and that used blocks will goto above the HWM level, now
its ready to used. now they consider has free blocks.

truncate is faster and delete is taking much time.


when we do detele, oracle will store the data in redolog files, so that if user want it again, it reverted back from redolog.
but in case of truncate , no such things happen, it reset HWM and completely blank the table.

why rollback is not working with truncate ?  

Truncate is a DDL command. and rollback is only for DML.

3. Can we do rollback after drop  --> NO. because drop is a ddl command. Rollback is not work for ddl only for dml

Question: Table A has 10M records, I have to delete 5M , How we have to DO?

Answer: 
1. we can not truncate here, because truncate will empty the whole table, which we do not
want
2. we can not fire the delete command, because deleting 5M rows from a table will
definitely hung the database. it will stuck and don't know when come back

3. Create table A_temp as select * from A where <condition> -- To fetch the required 5M
records
4. Now I have the required record which is needed for application in table A_temp
5. truncate main table. truncate table A ; -- this will be done in a few seconds and now
we have the application table empty and required data in temp table
6. Either we can rename the temp table to the main table and drop the main table (This is
an approach but not advisable because all the database objects like procedure, packages
and function who are using that table, will become uncompiled and we have to compile
again.)
OR
insert all records from temp table to main table

That is the one approach for deleting a high volume of data. 

Note: Prefer to insert the data from temp table to main table, rather than drop the main
table and rename the temp to main. Why? because if we drop the main table then all
database objects (procedures, functions,packages etc) become invalid, we have extra work
to compile them all.

------------------------------------------------------------------------------------------
----

You might also like