Skip to Main Content

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question.

Asked: January 04, 2011 - 11:06 am UTC

Last updated: January 06, 2011 - 8:42 am UTC

Version: 9.2.0

Viewed 10K+ times! This question is

You Asked

Hi Tom,

Could you please tell us the internal working of "Truncate"? I mean when we issue a command "Truncate Table TableName", what exactly oracle does internally?

Thanks in advance.


and Tom said...

The segment you are truncating is checkpointed from the SGA to disk.

Then the dictionary is updated to reflect the fact that "no blocks have data". If we are keeping the storage - we are done (we just reset the high water mark for the segment). If we are releasing the storage - we do that as well.

That is all.

Rating

  (9 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

That is all ?

Sokrates, January 04, 2011 - 3:00 pm UTC

http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/statements_10007.htm#SQLRF01707

Shows what Happens to Indexes, partitions, ..., which Might Be Quite a Bit more ...

Question: when specifying Reuse Storage, Why are dependent Cursors invalidated As written there ?

Tom Kyte
January 04, 2011 - 3:09 pm UTC

basically - the segment is checkpointed and the dictionary updated - for each segment involved.

Yes, it might obviously have to cascade to subordinate segments - doing the same exact thing (which is part of the reason why the checkpoint is necessary) - but it is all the same.


I don't like the way they wrote that section. It makes it sound like:


o You cannot roll back a TRUNCATE TABLE statement.
o All cursors are invalidated.
o You cannot flash back to the state of the table before the truncate operation.

are specific to reuse storage when in fact they apply to drop storage as well.

it is not specific to reuse storage.


In fact now that you pointed me to that all and I've read it - I have more comments...

They wrote:

This setting is useful as an alternative to deleting all rows of a very large table—when the number of rows is very large, the table entails many thousands of extents, and when data is to be reinserted in the future. TRUNCATE TABLE with REUSE STORAGE performs several orders of magnitude faster than deleting all rows

It might also perform several orders of magnitude slower than deleting all rows.
It might perform about the same.

It might perform faster.

It depends. I don't know what the number of extents has to do with anything - unless they were awkwardly trying to say "reuse might be faster than drop storage as we don't have to deallocate the extents" - but they are in a paragraph that was comparing truncate to delete.


Alexander, January 04, 2011 - 4:03 pm UTC

Tom,

Why did you say you don't like how they worded regarding rolling back a TRUNCATE or flashing back?

TRUNCATE is DDL, DDL commits, therefore you can't rollback, right?

Nor can you flashback a table that's had it's definition change (DDL), right?
Tom Kyte
January 04, 2011 - 4:09 pm UTC

if you read it in context, it would appear they are saying that the list of three things above applies ONLY when you use reuse storage. They stuck that list in the middle of reuse storage - not in a section "hey - things to think about with truncate in general"

It (the list) belongs in the section "hey - things to think about with truncate in general"


The three things are true - but they are true for drop storage as well as reuse storage.

thanks for clarification

Sokrates, January 05, 2011 - 12:39 am UTC

indeed I understood the documentation falsely as "All cursors are invalidated." only when using "reuse storage"
Tom Kyte
January 05, 2011 - 7:35 am UTC

I've filed a documentation change against that

cursor invalidating

Sokrates, January 05, 2011 - 6:12 am UTC

what's the rational behind cursor invalidating when truncating a table ?
The logical structure of the table remains unchanged, statistics are not touched, so why are cursors invalidated ?
Tom Kyte
January 05, 2011 - 7:40 am UTC

the data object id can be changed - the underlying physical data objects might be different. So while the logical structure is the same, the physical structure might well be altered.

How Object Id Changes.

Snehasish Das, January 05, 2011 - 12:00 pm UTC

Hi Tom,
forgive my ignorance.
If we create a table and truncate it the object is still there and its object id remains the same, still I am not sure of a scenario where the object id changes when we do a truncate.

Can you please let me know a scenario where the object id changes when we do a truncate. As we have a lot of packages where we do regular truncate insert into tables.

Regards,
Snehasish Das.
Tom Kyte
January 05, 2011 - 12:19 pm UTC

the DATA_OBJECT_ID can change. Look in user objects - you'll see there is an object_id, that is the "logical" id. It never changes.

Then there is a data_object_id, that is physical, it is a pointer to the segment.

The way out

Snehasish Das, January 05, 2011 - 1:14 pm UTC

Hi Tom,

Thanks a lot.

If I can .. Can I ask you another question. As we work in DWH we have written a lot of packages where we do trucate insert loading to staging/intermediary tables. We sometime use the cursors and bulk load utility to load the tables. Are you saying that these cursors might get invalidates by our truncate. If so what is the process to stop it. Secondly the dependecy of objects is set on the Object id or both on object id and data object id.

Regards,
Snehasish Das.
Tom Kyte
January 05, 2011 - 1:27 pm UTC

that the cursors get invalid doesn't affect you - the will be implicitly validated as soon as you execute them again. There is no way to "stop it", it is a natural event in the database. Your cursors go invalid all of the time due to shared pool aging, gathering statistics, etc - it happens all of the time.


The dependency of WHAT object?

Dependency

Snehasish Das, January 05, 2011 - 1:38 pm UTC

Hi Tom,

Thanks a lot for the reply. I am clear now.

The dependency which I was talking was USER_DEPENDENCIES thing.

Secondly if query is parsed again, this would be hard parsed and hence would be a performance issue. AM i correct.

Regards,
Snehasish Das.
Tom Kyte
January 05, 2011 - 3:29 pm UTC

user dependencies is "object id" based - when you truncate a table - stored procedures do NOT go invalid.

The query would be implicitly hard parsed - yes. But you don't truncate a table every minute so it shouldn't be a big deal. And in a data warehouse environment - you don't really rely on shared sql anyway - you typically do not use bind variables, you typically do not run the same queries over and over and over again.

How about every 8 seconds?

Ian, January 06, 2011 - 8:26 am UTC

"But you don't truncate a table every minute so it shouldn't be a big deal."

Just come across a case where the developer was truncating every 8 seconds. Basically he was using a real table where he should have been using a GTT.

The interesting side-effect was that the horrendous query being used to populate the table each time never showed up in AWR due to the cursor being invalidated every time. Turned out it was the most expensive query on the entire system by a factor of 10 - but was totally hidden. We called it Mutant Nija SQL - it sneaks in - kills your database - and then disappears leaving no trace that it ever existed.
Tom Kyte
January 06, 2011 - 8:42 am UTC

... Just come across a case where the developer was truncating every 8 seconds.
Basically he was using a real table where he should have been using a GTT. ...

wow??? Can you spell "serial processing"??? You could never have more than one user running their application at a time.


... We called it Mutant
Nija SQL - it sneaks in - kills your database - and then disappears leaving no
trace that it ever existed. ...

ok, I have to admit, I laughed out loud at that.

In this case serial processing was a good thing!

Ian, January 06, 2011 - 9:22 am UTC

In this case serial processing was a good thing - because you really would not want two copies of that program running at the same time - the CPUs would melt! It was doing 2.5 BILLION consistent gets.

It made me chuckle too. OK - I fell off my chair I was laughing so hard!