Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 298 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is it safe to put an index on an Oracle Temporary Table?

#1
I have read that one should not analyze a temp table, as it screws up the table statistics for others. What about an index? If I put an index on the table for the duration of my program, can other programs using the table be affected by that index?


**Does an index affect my process, and all other processes using the table?
or Does it affect my process alone?**

None of the responses have been authoritative, so I am offering said bribe.
Reply

#2
You're asking about two different things, indexes and statistics.
For indexes, yes, you can create indexes on the temp tables, they will be maintained as per usual.

For statistics, I recommend that you explicitly set the stats of the table to represent the average size of the table when queried. If you just let oracle gather stats by itself, the stats process isn't going to find anything in the tables (since by definition, the data in the table is local to your transaction), so it will return inaccurate results.

e.g. you can do:

`exec dbms_stats.set_table_stats(user, 'my_temp_table', numrows=>10, numblks=>4)`


Another tip is that if the size of the temporary table varies greatly, and within your transaction, you know how many rows are in the temp table, you can help out the optimizer by giving it that information. I find this helps out a lot if you are joining from the temp table to regular tables.

e.g., if you know the temp table has about 100 rows in it, you can:

`SELECT /*+ CARDINALITY(my_temp_table 100) */ * FROM my_temp_table`

Reply

#3
I assume you're referring to true Oracle temporary tables and not just a regular table created temporarily and then dropped. Yes, it is safe to create indexes on the temp tables and they will be used according to the same rules as a regular tables and indexes.

[Edit]
I see you've refined your question, and here's a somewhat refined answer:

From:

Oracle® Database Administrator's Guide
10g Release 2 (10.2)
Part Number B14231-02

"Indexes can be created on temporary tables. They are also temporary **and the data in the index has the same session or transaction scope as the data in the underlying table**."

If you need the index for efficient processing during the scope of the transaction then I would imagine you'll have to explicitly hint it in the query because the statistics will show no rows for the table.

Reply

#4
Well, I tried it out and the index was visible and used by the second session. Creating a new global temporary table for your data would be safer if you really need an index.

You are also unable to create an index while any other session is accessing the table.

Here's the test case I ran:

--first session
create global temporary table index_test (val number(15))
on commit preserve rows;

create unique index idx_val on index_test(val);

--second session
insert into index_test select rownum from all_tables;
select * from index_test where val=1;

Reply

#5
> Does an index effect my process, and all other processes using the table? or Does it effect my process alone?

I'm assuming we are talking of `GLOBAL TEMPORARY` tables.

**Think of a temporary table as of multiple tables that are created and dropped by each process on the fly from a template stored in the system dictionary**.

In `Oracle`, `DML` of a `temporary table` affects all processes, while data contained in the table will affect only one process that uses them.

Data in a `temporary table` is visible only inside the session scope. It uses `TEMPORARY TABLESPACE` to store both data and possible indexes.

`DML` for a `temporary table` (i. e. its layout, including column names and indexes) is visible to everybody with sufficient privileges.

This means that **existence** of the index will affect your process as well as other processes using the table in sense that any process that modifies data in the `temporary table` will also have to modify the index.

**Data** contained in the table (and in the index too), on the contrary, will affect only the process that created them, and will not even be visible to other processes.

IF you want one process to use the index and another one not to use it, do the following:

* Create two `temporary tables` with same column layout
* Index on one of them
* Use indexed or non-indexed table depending on the process
Reply

#6
You can also use the dynamic sampling hint (10g):

select /*+ DYNAMIC_SAMPLING (3) */ val
from index_test
where val = 1;


See [Ask Tom][1]


[1]:

[To see links please register here]

Reply

#7
You cannot create an index on a temporary table while it is used by another session, so answer is: No, it cannot affect any other process, because it is not possible.

An existing Index affects only your current session, because for any other session the temporary table appears empty, so it cannot access any index values.

Session 1:

SQL> create global temporary table index_test (val number(15)) on commit preserve rows;
Table created.
SQL> insert into index_test values (1);
1 row created.
SQL> commit;
Commit complete.
SQL>

Session 2 (while session 1 is still connected):

SQL> create unique index idx_val on index_test(val);
create unique index idx_val on index_test(val)
*
ERROR at line 1:
ORA-14452: attempt to create, alter or drop an index on temporary table already in use
SQL>

Back to session 1:

SQL> delete from index_test;
1 row deleted.
SQL> commit;
Commit complete.
SQL>

Session 2:

SQL> create unique index idx_val on index_test(val);
create unique index idx_val on index_test(val)
*
ERROR at line 1:
ORA-14452: attempt to create, alter or drop an index on temporary table already in use
SQL>

still failing, you first have to disconnect session 1 or table has to be truncated.

Session 1:

SQL> truncate table index_test;
Table truncated.
SQL>

Now you can create the index in Session 2:

SQL> create unique index idx_val on index_test(val);
Index created.
SQL>

This index of course will be used by any session.

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through