Home » SQL & PL/SQL » SQL & PL/SQL » GLOBAL TEMPORARY TABLE (10.2.0.4.0 )
GLOBAL TEMPORARY TABLE [message #444284] Sat, 20 February 2010 02:57 Go to next message
rahulvb
Messages: 924
Registered: October 2009
Location: Somewhere Near Equator.
Senior Member
What is the best option for GLOBAL TEMPORARY TABLE

1)
option create GLOBAL TEMPORARY TABLE with ON COMMIT DELETE ROWS.
and wheverever this is used for calculation commit at the end of porcedure.


CREATE GLOBAL TEMPORARY TABLE gtt_test
(
	A NUMBER
)ON COMMIT DELETE ROWS;


CREATE OR REPLACE PROCEDURE my_proc ( p_in in number)
as
begin

insert into gtt_test (a) 
select rownum from dual  connect by level  < p_in ;

commit;

end;

select * from gtt_test ;




2) create GLOBAL TEMPORARY TABLE without ON COMMIT DELETE ROWS and wheverever this is used use delete from Temp table /Truncate table and then user it.

CREATE GLOBAL TEMPORARY TABLE gtt_test
(
	A NUMBER
);


CREATE OR REPLACE PROCEDURE my_proc ( p_in in number)
as
begin
delete from gtt_test ;
insert into gtt_test (a) 
select rownum from dual  connect by level  < p_in;

end;

select * from gtt_test ;


Re: GLOBAL TEMPORARY TABLE [message #444285 is a reply to message #444284] Sat, 20 February 2010 03:02 Go to previous messageGo to next message
Michel Cadot
Messages: 68760
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
1/ Only YOU can know if your application needs to preserve or delete rows on commit, that is between transactions.

2/ Do not commit inside a procedure (unless it is an autonomous one) is a basic in Oracle programmation.

Regards
Michel
Re: GLOBAL TEMPORARY TABLE [message #444286 is a reply to message #444285] Sat, 20 February 2010 03:09 Go to previous messageGo to next message
rahulvb
Messages: 924
Registered: October 2009
Location: Somewhere Near Equator.
Senior Member
Michel Cadot wrote on Sat, 20 February 2010 03:02
1/ Only YOU can know if your application needs to preserve or delete rows on commit, that is between transactions.

Regards
Michel


no we do not need to preserve data for transaction.

Re: GLOBAL TEMPORARY TABLE [message #444290 is a reply to message #444286] Sat, 20 February 2010 03:40 Go to previous messageGo to next message
Frank
Messages: 7901
Registered: March 2000
Senior Member
If one of the options were always better than the other, Oracle would have phased out the other one (ok, in an ideal world).
The difference between PRESERVE ROWS and DELETE ROWS is this:
a GTT can store data that is only visible to a single session. If you store data in it that has to be available from within that session, even after a commit took place, then you use PRESERVE ROWS. If however a commit means you want to start all over with a fresh and empty GTT, use DELETE ROWS.
So, one makes the data persistent for SESSION scope, the other for TRANSACTION.

[Edit:typo]

[Updated on: Sat, 20 February 2010 03:40]

Report message to a moderator

Re: GLOBAL TEMPORARY TABLE [message #444296 is a reply to message #444284] Sat, 20 February 2010 04:25 Go to previous messageGo to next message
rahulvb
Messages: 924
Registered: October 2009
Location: Somewhere Near Equator.
Senior Member
Frank Agree with you ... But

how if there is only one java connection is active for the database ( there is no open and close connection ) for long time.

and my question was on performance side..which option would be good
Re: GLOBAL TEMPORARY TABLE [message #444310 is a reply to message #444286] Sat, 20 February 2010 09:05 Go to previous messageGo to next message
Michel Cadot
Messages: 68760
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
rahulvb wrote on Sat, 20 February 2010 10:09
Michel Cadot wrote on Sat, 20 February 2010 03:02
1/ Only YOU can know if your application needs to preserve or delete rows on commit, that is between transactions.

Regards
Michel


no we do not need to preserve data for transaction.

You say no to what?
To the fact that only YOU know it?

If you say you do not need to preserve rows across transactions then use ON COMMIT DELETE ROWS.

Regards
Michel



Re: GLOBAL TEMPORARY TABLE [message #444312 is a reply to message #444296] Sat, 20 February 2010 09:06 Go to previous messageGo to next message
Michel Cadot
Messages: 68760
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
rahulvb wrote on Sat, 20 February 2010 11:25
Frank Agree with you ... But

how if there is only one java connection is active for the database ( there is no open and close connection ) for long time.

and my question was on performance side..which option would be good

Performance is irrelevant to the question.
If you need to keep rows across transactions then use ON COMMIT PRESERVE ROWS, if you don't need them then use ON COMMIT DELETE ROWS.

Regards
Michel

Re: GLOBAL TEMPORARY TABLE [message #444349 is a reply to message #444284] Sat, 20 February 2010 14:27 Go to previous messageGo to next message
Kevin Meade
Messages: 2103
Registered: December 1999
Location: Connecticut USA
Senior Member
I suggest other.

Treat the global temp table like a regular table. Use keep rows, and when you no longer want the rows, delete them.

Do not rely on some defaulted behavior to achieve what you need. Explicitly delete the rows when you no longer want them.

Of course, no one has asked... WHY GTT? Why do you think you need global temp tables? What is your reason for using them?

Kevin
Re: GLOBAL TEMPORARY TABLE [message #444352 is a reply to message #444349] Sat, 20 February 2010 14:52 Go to previous messageGo to next message
Michel Cadot
Messages: 68760
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Quote:
Treat the global temp table like a regular table. Use keep rows, and when you no longer want the rows, delete them.

Do not rely on some defaulted behavior to achieve what you need. Explicitly delete the rows when you no longer want them.

I disagree with this.
GTT duration is not a defaulted behaviour it is a table property, like head table and iot are 2 types of permanent table or like manual or automatic segment management are 2 types of management. You can't say their behaviour is a defaulted one, you explicitly choose the behaviour knowing what it implies.
This is why I said, choose the duration for the behaviour you want.

Deleting is not the same thing as delete rows on commit. One generates undo and redo when the other one just frees space.
GTT are great when you use them appropriatly but very bad when you use them as Sybase temporary tables.

GTT purpose is to store intermediate values that require consecutive computations which can't be done in a single query, or to keep consistent results across transactions without being disturbed by other transactions and without requiring the serializable level which is very restrictive.

Regards
Michel

Re: GLOBAL TEMPORARY TABLE [message #444393 is a reply to message #444349] Sun, 21 February 2010 09:25 Go to previous messageGo to next message
rahulvb
Messages: 924
Registered: October 2009
Location: Somewhere Near Equator.
Senior Member
Kevin Meade wrote on Sat, 20 February 2010 14:27


Of course, no one has asked... WHY GTT? Why do you think you need global temp tables? What is your reason for using them?

Kevin


While porting code from Sybase/MS Sql server to Oracle this is the Option/alternative We/People look for Temp ( #tables ) in Oracle.

Michel Cadot wrote on Sat, 20 February 2010 14:52


GTT are great when you use them appropriatly but very bad when you use them as Sybase temporary tables.


You might want to suggest good alternative !! ( other then restructure the complete Application as this need long time and this luxury we don't have ) Smile

[Updated on: Sun, 21 February 2010 09:26]

Report message to a moderator

Re: GLOBAL TEMPORARY TABLE [message #444394 is a reply to message #444393] Sun, 21 February 2010 09:53 Go to previous messageGo to next message
Michel Cadot
Messages: 68760
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Quote:
You might want to suggest good alternative !! ( other then restructure the complete Application as this need long time and this luxury we don't have )

This is NOT luxury, this is MANDATORY to have a correct and efficient application.
When you CONVERT (not port) an application from Sybase to Oracle (or the opposite) you MUST rethink the whole application from its design not just translate code.

Regards
Michel

Re: GLOBAL TEMPORARY TABLE [message #444396 is a reply to message #444394] Sun, 21 February 2010 10:15 Go to previous messageGo to next message
rahulvb
Messages: 924
Registered: October 2009
Location: Somewhere Near Equator.
Senior Member
Michel Cadot wrote on Sun, 21 February 2010 09:53
you MUST rethink the whole application from its design not just translate code.


100% Agree , but some time one has to face time constraints and with legacy application so redesign Complete application is not possible.
Re: GLOBAL TEMPORARY TABLE [message #444397 is a reply to message #444396] Sun, 21 February 2010 10:34 Go to previous messageGo to next message
Michel Cadot
Messages: 68760
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
So you are lucky to do it from Sybase to Oracle, there you will just have inefficient application.
In the opposite way you'd most likely get deadlocks or inconsistent results.

Regards
Michel
Re: GLOBAL TEMPORARY TABLE [message #444400 is a reply to message #444397] Sun, 21 February 2010 10:42 Go to previous messageGo to next message
rahulvb
Messages: 924
Registered: October 2009
Location: Somewhere Near Equator.
Senior Member
Michel Cadot wrote on Sun, 21 February 2010 10:34

In the opposite way you'd most likely get deadlocks or inconsistent results.



yeah, This is what I am much worried about Sad
Re: GLOBAL TEMPORARY TABLE [message #444402 is a reply to message #444284] Sun, 21 February 2010 12:44 Go to previous messageGo to next message
Kevin Meade
Messages: 2103
Registered: December 1999
Location: Connecticut USA
Senior Member
Some good posts. And to Michel's point, GLOBAL TEMPORARY TABLES are more sophisticated than a simple "real" table so let us review a few points about the GTT. In reviewing these points it should become apparent that the GTT is not something that should be used but rarely because most people do not bother to read documentation or test their usage of special features before employing them.

Perhaps the most interesting thing about the GTT is defined in the first bullet below. Most people do not really know what ON COMMIT PRESERVE ROWS actually means. They usually either never have a problem with it or they find out after the fact when rows they look for are not in the GTT as they expected. Here are what I consider significant features of a GTT.

1) GLOBAL TEMPORARY TABLES are always is some form, temporary. ON COMMIT PRESERVE ROWS is some what misleading. When a commit is issued, rows in a GTT are not TRULY COMMITED in the traditional sense. Temporary means that by design, the data will eventually go away automatically at a predefined moment. A GTT comes in two forms:

a) transaction persistent
b) session persistent

What does really mean? The commit status of a GTT defines the lifetime of rows in the table. Thus DELETE ROWS = rows go away on commit/rollback where as PRESERVE ROWS = rows go away at the end of the session. Therefore, even specifying ON COMMIT PRESERVE ROWS does not commit the rows forever, only for the duration of your session. This can easily be demonstrated with a simple test case, or you can read the manuals for this information.

SQL> create global temporary table kev_gtt1
  2  on commit delete rows
  3  as
  4  select * from dual;

Table created.

SQL> select * from kev_gtt1;

no rows selected


Notice in the first case, that the implied commit as a result of the table create means that rows were deleted from the GTT.

SQL> drop table kev_gtt1;

Table dropped.

SQL> create global temporary table kev_gtt1
  2  on commit preserve rows
  3  as
  4  select * from dual;

Table created.

SQL> select * from kev_gtt1;

D
-
X

1 row selected.

SQL> connect ...
Enter password: *********
Connected.
SQL> select * from kev_gtt1;

no rows selected


In the second case, reconnecting to the same user ends the session and creates a new one. Note how the rows in the GTT were removed (via truncate I believe).

Thus when looking at a GLOBAL TEMPORARY TABLE, you will want to consider how long you want your rows to live before they disappear and indeed even if the temporary nature of the GTT is a needed component of your solution design. If it is not then why are you using a GTT?

2) GLOBAL TEMPORARY TABLES are SESSION ISOLATED (my term not Oracle's). As Michel points out, this has benefits under the covers. But from the perspective of code design, it means that rows is a GTT can only be seen by the session that created them. Most people understand this feature quite readily. Sessions can work knowing that what they put in a GTT will never be changed by nor be seen by other sessions.

Once again, when looking at a a GLOBAL TEMPORARY TABLE, you will want to consider if this feature of the GTT is of value to your solution design. If it is not then why are you using a GTT?

3) GLOBAL TEMPORARY TABLES manage their space differently from "real" tables. When a GTT needs to physically write its rows somewhere, it writes its rows to a segment from the TEMP tablespace. Because a GTT uses TEMP space, there is less REDO generated when using a GTT as vs. a "real" table. However, a GTT fully participates in transaction semantics and as such changing data in a GTT does generate UNDO information and UNDO information IS ALWAYS protected by REDO. Some people have done informal tests on their own (do some googles) that show REDO for a GTT is about half (50%) of normal redo in many situations.

Once again, when looking at a GLOBAL TEMPORARY TABLE, you will want to consider if this space management aids your solution design. If this savings in REDO is not beneficial to your solution design then why are you using a GTT?

There are of course other considerations. GLOBAL TEMPORARY TABLES are an advanced Oracle feature and as one who uses advanced features I can attest that not all advanced features play well together. As just one example of the nuances of GTTs, consider the use of a GTT with PRESERVE ROWS, and a transaction design that employs multiple nested AUTONOMOUS TRANSACTIONS. You may be headed for trouble.

So now that you have read the above, we get back to the real question... WHY GTT? Why do you want to use a GTT in your solution?

Which of the three characteristics of GLOBAL TEMPORARY TABLES is useful to your solution design:

Quote:
1) GLOBAL TEMPORARY TABLES are always is some form, temporary.
2) GLOBAL TEMPORARY TABLES are SESSION ISOLATED.
3) GLOBAL TEMPORARY TABLES manage their space differently.

Explain to us which of the above three is necessary and/or beneficial to your solution design? If you cannot, then why use the GTT?

Kevin

[Updated on: Mon, 22 February 2010 07:35]

Report message to a moderator

Re: GLOBAL TEMPORARY TABLE [message #444435 is a reply to message #444402] Mon, 22 February 2010 00:27 Go to previous messageGo to next message
rahulvb
Messages: 924
Registered: October 2009
Location: Somewhere Near Equator.
Senior Member
Kevin Meade wrote on Sun, 21 February 2010 12:44

Quote:

1) GLOBAL TEMPORARY TABLES are always is some form, temporary.
2) GLOBAL TEMPORARY TABLES are SESSION ISOLATED.
3) GLOBAL TEMPORARY TABLES manage their space differently.

Explain to us which of the above three is necessary and/or beneficial to your solution design? If you cannot, then why use the GTT?

Kevin



I am going with first one 1/ GLOBAL TEMPORARY TABLES are always is some form, temporary Which is of course -> 2) GLOBAL TEMPORARY TABLES are SESSION ISOLATED.

so to conclude me using GLOBAL TEMPORARY TABLES as substitute for Sybase/MS SQL #temp tables.



Re: GLOBAL TEMPORARY TABLE [message #444440 is a reply to message #444435] Mon, 22 February 2010 00:43 Go to previous messageGo to next message
Michel Cadot
Messages: 68760
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Quote:
so to conclude me using GLOBAL TEMPORARY TABLES as substitute for Sybase/MS SQL #temp tables.

So it is the case of bad use of GTT.

Regards
Michel
Re: GLOBAL TEMPORARY TABLE [message #444449 is a reply to message #444440] Mon, 22 February 2010 01:17 Go to previous messageGo to next message
rahulvb
Messages: 924
Registered: October 2009
Location: Somewhere Near Equator.
Senior Member
Michel Cadot wrote on Mon, 22 February 2010 00:43
Quote:
so to conclude me using GLOBAL TEMPORARY TABLES as substitute for Sybase/MS SQL #temp tables.

So it is the case of bad use of GTT.

Regards
Michel


Michel But I don not have any option either.
Re: GLOBAL TEMPORARY TABLE [message #444454 is a reply to message #444449] Mon, 22 February 2010 01:55 Go to previous messageGo to next message
Michel Cadot
Messages: 68760
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Yes, you don't any other option (but rewriting the queries, most often), so you will use it but this does not change the fact it is a bad use of it.

Regards
Michel

[Updated on: Mon, 22 February 2010 01:56]

Report message to a moderator

Re: GLOBAL TEMPORARY TABLE [message #444461 is a reply to message #444454] Mon, 22 February 2010 02:34 Go to previous messageGo to next message
rahulvb
Messages: 924
Registered: October 2009
Location: Somewhere Near Equator.
Senior Member
Michel Cadot wrote on Mon, 22 February 2010 01:55
but rewriting the queries, most often
Regards
Michel


Yes True,

1/ I will have to replace every #temp table join with the sql query ( which server as temp table in sybase/Ms sql).

Or

2/ I will have to get the most of the calculation done in SQL query itself.

Some time I think this is a Big bottle neck in Oracle that we can not create temp table on the fly inside store procedure.

This is where I miss the Old MS Sql days Smile

Re: GLOBAL TEMPORARY TABLE [message #444526 is a reply to message #444284] Mon, 22 February 2010 06:20 Go to previous messageGo to next message
Kevin Meade
Messages: 2103
Registered: December 1999
Location: Connecticut USA
Senior Member
People are imperfect. Business is imperfect. IT is imperfect.

The best IT solution is often not the most technically correct solution. The best IT solution must co-exist with the practicalities of time limitations and money limitations and resource limitations and mostly people limitations.

If you are satisfied that using a GTT radically simplifies your port of code from some other database to Oracle and this is the most important thing to you then your solution may be the best IT solution given your limitations, even though it may be technically imperfect and a poor overall Oracle design. Such is the IT world.

But only you can really make that call, not us. We have done our job in clarifying the situation, and in pointing out the pitfalls.

But if you are satisfied, then so am I; good luck, and good job.

Kevin
Re: GLOBAL TEMPORARY TABLE [message #444536 is a reply to message #444526] Mon, 22 February 2010 07:16 Go to previous messageGo to next message
Its_me_ved
Messages: 979
Registered: October 2009
Location: India
Senior Member
Thanks once again ..
Re: GLOBAL TEMPORARY TABLE [message #444550 is a reply to message #444526] Mon, 22 February 2010 08:10 Go to previous messageGo to next message
rahulvb
Messages: 924
Registered: October 2009
Location: Somewhere Near Equator.
Senior Member
Kevin Meade wrote on Mon, 22 February 2010 06:20
People are imperfect. Business is imperfect. IT is imperfect.


I can bet THE Last Dollar in my Pocket on this Cool

Kevin Meade wrote on Mon, 22 February 2010 06:20

We have done our job in clarifying the situation, and in pointing out the pitfalls.


Much Much Appreciated Kevin.

Kevin Meade wrote on Mon, 22 February 2010 06:20

But if you are satisfied, then so am I; good luck, and good job.
Kevin


Thanks A Lot.

Regards,
-Rahul
Re: GLOBAL TEMPORARY TABLE [message #444585 is a reply to message #444284] Mon, 22 February 2010 13:48 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
Just a couple of things to think about. GTT's are fast because they are simply blocks in the SGA or with overflow in the TEMPORARY table-space. They are not truncated so much as the temporary blocks are released. That's why you can't have a storage clause.

You can have indexes on GTT's which are also held in memory or temp space. and are just as isolated as the data.

If you are using GTT's via a web connection, you are probably in trouble. Multiple connections to the web server will use the same session and will tend to cause sharing problems with GTT's. (Session specific data)
Re: GLOBAL TEMPORARY TABLE [message #444586 is a reply to message #444284] Mon, 22 February 2010 14:39 Go to previous messageGo to next message
Kevin Meade
Messages: 2103
Registered: December 1999
Location: Connecticut USA
Senior Member
Thanks Bill. I had wondered about blocks in the SGA, but I could not find the documentation that stated same. Do you know where it is so we can add it as a reference to the post?

Kevin
Re: GLOBAL TEMPORARY TABLE [message #444587 is a reply to message #444586] Mon, 22 February 2010 14:45 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
Kevin Meade wrote on Mon, 22 February 2010 14:39
Thanks Bill. I had wondered about blocks in the SGA, but I could not find the documentation that stated same. Do you know where it is so we can add it as a reference to the post?

Kevin


I can't remember the manual (probably concepts) but Tom Kyle has a nice writeup at

http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:3329273788164
Re: GLOBAL TEMPORARY TABLE [message #444588 is a reply to message #444284] Mon, 22 February 2010 15:24 Go to previous message
Kevin Meade
Messages: 2103
Registered: December 1999
Location: Connecticut USA
Senior Member
Thanks Bill, this is nice. A quick recap from Tom's thread gives more detail on the space management of the GTT and gives a hint as to its best use (INSERT/SELECT only).

Quote:
If all you do is INSERT and SELECT from the temp tables -- very very little redo is generated. We
do not generate any redo to REDO the insert, but we do generate rollback (undo) information and
this undo is protected by redo.

Hence, if you INSERT into a temp table, a very little amount of UNDO will be generated (in the
event you rollback, we need to undo the insert as well). That UNDO will be protected by REDO. The
INSERT itself will not be.

It is only when you start massively updating or deleting this temp object that large amounts of
redo would be generated. For example, suppose you put 1,000,000 rows into the temp table. That
insert would generate little UNDO and correspondingly little REDO. Now, suppose you "delete from
temp_table" -- that will generate a massive amount of UNDO (in order to rollback the delete if you
need) and that UNDO will be protected by REDO.

Since temporary tables are used almost 100% for INSERTS and SELECTS, this is just fine. You should
rely on a COMMIT, TRUNCATE or end of SESSION to "delete" the data and that will have no effect on
the logs since the data will just be "wiped out" -- not logged.


and also

Quote:
gtt's generate undo and everything, they do the same sort of consistent read stuff "real" tables
do.

they are cacheable.


I am taking Bill at his commentary that the GTT is cacheable in the SGA and indeed will be cached until it is necessary to write actual temp space to disk (which could be never). Thus it is possible that small numbers of rows in a GTT may never cause physical I/O and hence offer an opportunity for great speed in the appropriate situations.

Thanks again Bill for just the right link. Hat off to Tom Kyte again. Kevin
Previous Topic: which Index
Next Topic: Query to store the results - Runs for long time
Goto Forum:
  


Current Time: Thu Jun 19 15:49:44 CDT 2025