Home » SQL & PL/SQL » SQL & PL/SQL » Index on Global temp tables (Win XP, Oracle 10g)
Index on Global temp tables [message #461118] Wed, 16 June 2010 12:14 Go to next message
freakabhi
Messages: 74
Registered: November 2007
Location: mumbai
Member
all,

I am trying to use Global temporary tables, and index on this table to get my results faster. I can see even if I run any query on this table, it does full table scan and not Index scan.., any idea why this is the case?



create global temporary table abc_tab on commit preserve rows 
as select a,b,c from xyz;

create index lmn on abc_tab(a,b,c)





Thanksm
freakabhi

Re: Index on Global temp tables [message #461119 is a reply to message #461118] Wed, 16 June 2010 12:18 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
It would be helpful if you followed Posting Guidelines - http://www.orafaq.com/forum/t/88153/0/

WHY MY INDEX IS NOT BEING USED
http://communities.bmc.com/communities/docs/DOC-10031

http://searchoracle.techtarget.com/tip/Why-isn-t-my-index-getting-used

http://www.orafaq.com/tuningguide/not%20using%20index.html

Re: Index on Global temp tables [message #461120 is a reply to message #461118] Wed, 16 June 2010 12:21 Go to previous messageGo to next message
Michel Cadot
Messages: 68758
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Because there is no statistics on the GTT and so default is taken which is very few rows (don't currently remember the actual value) and so index is not useful (as it is estimated by the optimizer).
If you want this index to be used you have to use CARDINALITY hint on the query to give the optimizer a good estimation of the number of rows in the GTT.

Executing the query with autotrace on will show you this fact.

Regards
Michel

[Updated on: Wed, 16 June 2010 12:21]

Report message to a moderator

Re: Index on Global temp tables [message #461122 is a reply to message #461120] Wed, 16 June 2010 12:27 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
http://www.orafaq.com/forum/t/158818/136107/

Are you just rearranging the deck chairs on the Titanic?

Effective SQL tuning is not accomplished by making random changes.
Re: Index on Global temp tables [message #461123 is a reply to message #461118] Wed, 16 June 2010 12:27 Go to previous messageGo to next message
JRowbottom
Messages: 5933
Registered: June 2006
Location: Sunny North Yorkshire, ho...
Senior Member
I get index use when I try it:
create global temporary table test_030
(col_1  number, col_2 varchar2(100))
on commit preserve rows 
;

create index test_030_idx on test_030(col_1);

insert into test_030 select level,'Row '||level from dual connect by level <= 10000;

commit;

explain plan for 
select *
from   test_030
where col_1 = 345;

select * from table(dbms_xplan.display());

Plan hash value: 3106667229
 
--------------------------------------------------------------------------------------------
| Id  | Operation                   | Name         | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |              |     1 |    65 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| TEST_030     |     1 |    65 |     2   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN          | TEST_030_IDX |     1 |       |     1   (0)| 00:00:01 |
--------------------------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
   2 - access("COL_1"=345)
 
Note
-----
   - dynamic sampling used for this statement
Re: Index on Global temp tables [message #461124 is a reply to message #461120] Wed, 16 June 2010 12:32 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9105
Registered: November 2002
Location: California, USA
Senior Member
Or, you could use a dynamic_sampling hint:

http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/sql_elements006.htm#SQLRF50913
Re: Index on Global temp tables [message #461128 is a reply to message #461124] Wed, 16 June 2010 12:45 Go to previous messageGo to next message
JRowbottom
Messages: 5933
Registered: June 2006
Location: Sunny North Yorkshire, ho...
Senior Member
Or you could gather statistics on the GTT after loading it:
begin
  dbms_stats.gather_table_stats(ownname => null
                               ,tabname => 'TEST_030'
                               ,method_opt => 'FOR ALL INDEXED COLUMNS'
                               ,cascade => true);

end;
/

explain plan for 
select col_1,col_2
from   test_030
where  col_1 = 567;

select * from table(dbms_xplan.display());

Plan hash value: 3106667229
 
--------------------------------------------------------------------------------------------
| Id  | Operation                   | Name         | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |              |     1 |    12 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| TEST_030     |     1 |    12 |     2   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN          | TEST_030_IDX |     1 |       |     1   (0)| 00:00:01 |
--------------------------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
   2 - access("COL_1"=567)
Re: Index on Global temp tables [message #461130 is a reply to message #461128] Wed, 16 June 2010 12:50 Go to previous messageGo to next message
JRowbottom
Messages: 5933
Registered: June 2006
Location: Sunny North Yorkshire, ho...
Senior Member
Or you could be a traditionalist and just use an INDEX hint (I dropped and recreated the table for this example to get rid of the stats that were there previously):
explain plan for 
select /*+ dynamic_sampling(0) index */
       col_1,col_2
from   test_030
where  col_1 = 324;

select * from table(dbms_xplan.display());

Plan hash value: 3106667229
 
--------------------------------------------------------------------------------------------
| Id  | Operation                   | Name         | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |              |    82 |  5330 |     5   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| TEST_030     |    82 |  5330 |     5   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN          | TEST_030_IDX |    33 |       |     1   (0)| 00:00:01 |
--------------------------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
   2 - access("COL_1"=324)
Re: Index on Global temp tables [message #461134 is a reply to message #461130] Wed, 16 June 2010 13:01 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9105
Registered: November 2002
Location: California, USA
Senior Member
JRowBottom,

I see that your first test used the index without any statistics or hints, but that the optimizer used dynamic sampling. I see you are using 11g and the original poster is using 10g. I wonder if maybe 10g does not use dynamic sampling or at least not without a hint?
Re: Index on Global temp tables [message #461136 is a reply to message #461134] Wed, 16 June 2010 13:04 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9105
Registered: November 2002
Location: California, USA
Senior Member
Apparently you have to enable dynamic sampling in 10g for it to do it without a hint:

http://download.oracle.com/docs/cd/B19306_01/server.102/b14223/usingpe.htm#sthref2590
Re: Index on Global temp tables [message #461299 is a reply to message #461134] Thu, 17 June 2010 05:51 Go to previous message
JRowbottom
Messages: 5933
Registered: June 2006
Location: Sunny North Yorkshire, ho...
Senior Member
Quote:
I see you are using 11g and the original poster is using 10g.

Not I, Ma'am. 10g all the way for me.

Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Prod
PL/SQL Release 10.2.0.4.0 - Production
"CORE	10.2.0.4.0	Production"
TNS for 32-bit Windows: Version 10.2.0.4.0 - Production
NLSRTL Version 10.2.0.4.0 - Production


The odd thing is I've not enabled dynamic sampling myself - it came this way out of the box.

select name,value,isdefault from v$parameter where name ='optimizer_dynamic_sampling'; 
NAME                         VALUE               ISDEFAULT
---------------------------  ------------------  -------------
optimizer_dynamic_sampling   2                   TRUE  

Previous Topic: wm_concat limit on rows (splitted and merged)
Next Topic: Oracle Function working in DB and not in another
Goto Forum:
  


Current Time: Wed May 28 17:22:24 CDT 2025