What Exactly Is PLAN - TABLE in Oracle Database?
What Exactly Is PLAN - TABLE in Oracle Database?
Oracle Database?
Every user session on Oracle database will be able to access default table
PLAN_TABLE to collect execution plan of a query from EXPLAIN PLAN
command. But, how is this table available to all the users in the database and
gets empty once you disconnect? Is this table available and created for every
user logging into the database or is it a global table and available publicly to
access? I am sure that you must have got little excited with these questions
and we are going to get answers for many such questions in this blog with
the help of a step-by-step analytic approach. In our earlier articles EXPLAIN
PLAN for multiple SQL statements, Ways of generating EXECUTION PLAN in
Oracle Database we have clearly understood the usage of PLAN_TABLE to
generate execution plan for a query. In this blog, we are going to see what
exactly is this PLAN_TABLE and all the concepts around its existence.
Basics:
Earlier to 11g, consider in 10g we will have to create this default table
for EXPLAIN PLAN runningutlxplan.sql file
from $ORACLE_HOME/rdbms/admin. Let us read this file and understand
what exactly is this PLAN_TABLE.
[oracle@PT-DB ~]$ cd $ORACLE_HOME/rdbms/admin/utlxplan.sql
varchar2(30),
plan_id
number,
timestamp
date,
remarks
varchar2(4000),
.);
It has got just one DDL to create PLAN_TABLE in the schema you are
connected to when running this SQL file. So it doesnt actually give us
good details of why is this now available in 11gR2(Which I am using). Let
us start our analysis with our case study now.
Case study:
OBJECT_TYPE
-
PLAN_TABLE
SYNONYM
table.
Let us extract the DDL of PLAN_TABLE synonym to check the actual
table name.
SQL>select dbms_metadata.get_ddl(SYNONYM,PLAN_TABLE,PUBLIC)
from dual;
CREATE OR REPLACE PUBLIC SYNONYM PLAN_TABLE FOR
SYS.PLAN_TABLE$
But the question still alive that how this table contents are erased
every time session gets disconnected. Let us go further into the
analysis.
At this moment, I could recollect the concepts of GLOBAL TEMPORAY
TABLES (GTT) which have got the feature of cleaning up the data in the
table after the session disconnects. So let us check
ifSYS.PLAN_TABLE$ is a GTT.
SQL> select table_name,TEMPORARY from dba_tables where
table_name=PLAN_TABLE$';
TABLE_NAME
PLAN_TABLE$
why it is available for all the users to access and data gets erased as
soon as session disconnects.
Having understand the type of the object, let us try to find where
exactly the data is temporarily stored until the user session disconnects.
T TABLESPACE_NAME
PLAN_TABLE$
COUNT(*)
0
SQL> select USER,BLOCKS from v$tempseg_usage;
no rows selected
above.
SQL> explain plan for select * from v$session;
Explained.
SQL> select count(*) from plan_table;
COUNT(*)
6
SQL> select USER,BLOCKS from v$tempseg_usage;
USER
BLOCKS
-
SYS
128
SYS
128
SYS
128
SYS connection:
SQL> select count(*) from sys.plan_table$;
COUNT(*)
0
Session 1:
SQL> explain plan for select * from v$session;
Explained.
SQL> select count(*) from sys.plan_table$;
COUNT(*)
6
SYS connection:
SQL> select count(*) from sys.plan_table$;
COUNT(*)
0
This is how GTT tables work, data from the table is bundled to the
if so.
SQL> select index_name,index_type from dba_indexes where
TABLE_NAME=PLAN_TABLE$';
INDEX_NAME
INDEX_TYPE
SYS_IL0000005124C00027$$
LOB
Which means that this is the index created for LOB columns in the table
by default by the database. So PLAN_TABLE$ have a LOB column in it.
Will optimizer write an execution plan when you query PLAN_TABLE and
Temporary Table.
Data of PLAN_TABLE is stored into temporary tablespace segments.
Temporary tablespace is not just used for sorting; it is also used by
Users connected from application or OEM or SQL plus or any other tool
will be able to accessPLAN_TABLE.