Delete statement will not reduce table size

Create a testing table called test

SQL> create table test  tablespace users as select * from dba_objects;

Table created.


We look at the size of the table test, the table size is around 3MB now
SQL> select owner,segment_name,tablespace_name, sum(bytes)/1024/1024 "Size in MB
" from dba_segments
  2  where segment_name='TEST'
  3  group by owner,segment_name,tablespace_name ;

OWNER                          SEGMENT_NA TABLESPACE Size in MB
------------------------------ ---------- ---------- ----------
SYS                            TEST       USERS               3


We now delete all the rows in the table and commit the statement.
SQL> delete from test;

22906 rows deleted.

SQL> commit;

Commit complete.


If we look at the size of the table now, the table size is still 3MB. This demonstrated that the table didn’t shrink after we delete data from the table.

SQL> select owner,segment_name,tablespace_name, sum(bytes)/1024/1024 "Size in MB
" from dba_segments
  2  where segment_name='TEST'
  3  group by owner,segment_name,tablespace_name ;

OWNER                          SEGMENT_NA TABLESPACE Size in MB
------------------------------ ---------- ---------- ----------
SYS                            TEST       USERS               3


To shrink the table, what I will do now is enable the row movement feature of the table.
SQL> ALTER TABLE test ENABLE ROW MOVEMENT;

Table altered.

SQL> alter table  test shrink space;

Table altered.



SQL> select owner,segment_name,tablespace_name, sum(bytes)/1024/1024 "Size in MB
" from dba_segments
  2  where segment_name='TEST'
  3  group by owner,segment_name,tablespace_name
  4  ;

OWNER                          SEGMENT_NA TABLESPACE Size in MB
------------------------------ ---------- ---------- ----------

SYS                            TEST       USERS           .0625

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati

Quick reference link for Oracle database patching

Evaluating and rolling out the latest database patches is one of the most common tasks for database administrator. For Oracle products, you will constantly receive the notification from Oracle (at least)quarterly telling you that the patches you need to implement.However, they wont tell which patch you need for your database versions, and you have search through their slow Oracle support portal to the find patch you need.

To save time, normally I go directly to this document, Quick Reference to Patch Numbers for Database PSU, SPU(CPU), Bundle Patches and Patchsets (Doc ID 1454618.1), and in this document I can see all the patches that have been released for different versions of Oracle databases.

This Oracle document certainly worth saving in the bookmark -)

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati

Estimate index size with CREATE_INDEX_COST

Creating indexes on big table takes long time. What is even more time consuming is that you get ORA-01654: unable to extend index error while creating indexes. You have to give a emergency call to the storage guys to ask them allocating more space for you. Sometimes you have wait for hours(luckily) or days before extra space is available…and then run the create index statement again...

Therefore, capacity planning becomes essential when you are planning to create indexes.  In Oracle 10g or later, there is a procedure called CREATE_INDEX_COST in DBMS_SPACE package which can help you estimate the size of the index from your create indexes

Here is an example:
--create a testing table named t
SQL> create table t as select * from dba_objects;

Table created.

SQL> declare
begin
for i in 1..10 loop
insert /*+ APPEND */ into t select * from t;
commit;
end loop;
end; 
/

PL/SQL procedure successfully completed.

SQL> select count(*) from t;

  COUNT(*)
----------
  13405184

1 row selected.

--As told by Oracle docs, you have to have updated statistics in order to allow CREATE_INDEX_COST to produce accurate estimation
SQL> exec dbms_stats.gather_table_stats('TEST','T');

PL/SQL procedure successfully completed.

SQL> variable used_bytes number
SQL> variable allocated_bytes number
SQL> set autoprint on
SQL>


SQL> exec dbms_space.create_index_cost( 'create index t_idx on t(object_id,object_name,object_type)', :used_bytes, :allocated_bytes );

PL/SQL procedure successfully completed.


ALLOCATED_BYTES
---------------
      645922816


USED_BYTES
----------
428965888


Verification
SQL> create index t_idx on t(object_id,object_name,object_type);

Index created.


select segment_name ,sum(bytes) from dba_segments where owner='TEST' and segment_name='T_IDX' group by segment_name;

SEGMENT_NAME                                                                      SUM(BYTES)
--------------------------------------------------------------------------------- ----------
T_IDX                                                                              637534208

1 row selected.

We see that the CREATE_INDEX_COST says that the estimated index size is 616MB and the actual size of the index is 608MB.

Conclusions
We are happy that this procedure produce accurate result for our simple b-tree index. However,  I doubt that if procedure can procedure good result for other types of index such as bitmap or function-based index. I tried other types of indexes, but I found I cannot get a sensible from this procedure, my best guess is since this procedure produces result by the computation of average column length and the columns appeared in the create index statement…Anyways, I believe CREATE_INDEX_COST is good enough for the  rough estimation of index size ;-p

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati

Copy Schema from one machine to another machine using EXP/IMP

Though Oracle 10g provides new tools expdp and impdp for exporting and importing data between databases.
We still see lots of companies using old exp/imp to copy and move data. However, using exp/imp implies that DBA have to do more manual work in order to export and import data successfully. For example, tablespaces must be created before one can import dump to another database, privileges must be granted explicitly after the import and DBA have to create synonyms manually etc. All these are necessary to ensure users can use data on the new database without errors.

Here is a list of high level steps to copy schemas from one database to another.
In the target database (new database):
1).  Create any roles for the user .Grant system/object/role privileges that the user currently have in the source database
2).  Create any database links will be required in the schema.
3).  Create tablespaces. These would be the same tablespace names that the user currently have in the source database.
4). Create profiles that the user currently using in the source database.
5). Create the user who own the schema. Grant the privs/roles to those user as required.
6). Set the correct tablespace quota limit for users
7). Perform schema exports from the source database.
8). Perform schema import into the target database.
9). Create synonym in other schemas that need to reference to objects in the new schema.
10). Create constraints in other schema that need to reference to objects in the new schema. e.g. Foreign key constraints
11).Recompile invalid objects
12). Check if there are still invalid objects and fix them. 

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati

Determine the environment variables for a running Oracle instance in Solaris and Linux

One of the important steps in troubleshooting Oracle problem is to find out whether environment variables are set correctly. For a running instance, we can use the following methods to determine the environment variables.

1. Find the process id , PID, of the Oracle process
ps -ef |grep smon

2. Get the environment variables of the Oracle process

In Solaris,
pargs -e | grep ORACLE

In Linux,
cat /proc//environ

Example in Solaris
root# ps -ef |grep -i smon |grep -v grep|awk '{print $2}'|xargs pargs -e
2362:   ora_smon_TESTDB
envp[0]: SKGP_HIDDEN_ARGS=
envp[1]: ORACLE_SPAWNED_PROCESS=1
envp[2]: _=/usr/bin/perl
envp[3]: PATH=
envp[4]: LOGNAME=oracle
envp[5]: SHELL=/usr/bin/sh
envp[6]: HOME=/home/oracle
envp[7]: PWD=/home/oracle
envp[8]: TZ=Hongkong
envp[9]: ORAENV_ASK=NO
envp[10]: ORACLE_HOME=/u01/app/oracle/product/11.1.0.7
envp[11]: LD_LIBRARY_PATH=/u01/app/oracle/product/11.1.0.7/lib
envp[12]: LD_LIBRARY_PATH_64=/u01/app/oracle/product/11.1.0.7/lib
envp[13]: LIBPATH=/u01/app/oracle/product/11.1.0.7/lib
envp[14]: TNS_ADMIN=/u01/app/oracle/product/11.1.0.7/network/admin
envp[15]: ORACLE_SID=TESTDB
envp[16]: ORA_NLS10=/u01/app/oracle/product/11.1.0.7/nls/u01/app
envp[17]: DSM_DIR=/opt/tivoli/tsm/client/ba/bin
envp[18]: DSM_CONFIG=/opt/tivoli/tsm/client/ba/bin/dsm.opt
envp[19]: DSM_LOG=/u01/app/oracle/TESTDB/backup/u01/appbase
envp[20]: NLS_LANG=AMERICAN_AMERICA.AL32UTF8
envp[21]: ORA_NET2_DESC=11,14


Example in Linux
[root@test1 ~]# cat /proc/`ps -ef |grep -i smon |grep -v grep|awk '{print $2}'`/environ | tr '\0' '\n'
HOSTNAME=test1
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=192.168.1.200 60578 22
SSH_TTY=/dev/pts/5
USER=oracle
LS_COLORS=no=00:fi=00:di=00;34:ln=00;36:pi=40;33:so=00;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=00;32:*.cmd=00;32:*.exe=00;32:*.com=00;32:*.btm=00;32:*.bat=00;32:*.sh=00;32:*.csh=00;32:*.tar=00;31:*.tgz=00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31:*.zip=00;31:*.z=00;31:*.Z=00;31:*.gz=00;31:*.bz2=00;31:*.bz=00;31:*.tz=00;31:*.rpm=00;31:*.cpio=00;31:*.jpg=00;35:*.gif=00;35:*.bmp=00;35:*.xbm=00;35:*.xpm=00;35:*.png=00;35:*.tif=00;35:
ORACLE_SID=testdb2
MAIL=/var/spool/mail/oracle
PATH=
INPUTRC=/etc/inputrc
PWD=/u01/app/oracle/oradata
LANG=en_US.UTF-8
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
SHLVL=1
HOME=/home/oracle
LOGNAME=oracle
CVS_RSH=ssh
SSH_CONNECTION=192.168.1.200 60578 192.168.1.101 22
LESSOPEN=|/usr/bin/lesspipe.sh %s
ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
G_BROKEN_FILENAMES=1
_=/u01/app/oracle/product/11.2.0/dbhome_1/bin/sqlplus
OLDPWD=/home/oracle
ORA_NET2_DESC=9,12
ORACLE_SPAWNED_PROCESS=1
SKGP_SPAWN_DIAG_PRE_FORK_TS=1305737174
SKGP_SPAWN_DIAG_POST_FORK_TS=1305737174
SKGP_HIDDEN_ARGS=
0
SKGP_SPAWN_DIAG_PRE_EXEC_TS=1305737174

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati

Space is not freed after removing the file in Unix

This situation usually occurs when a file is deleted while it is still opened by some other processes. This is because when open a file opened by a process, the reference count of the file will increment by 1. Removing the file only remove the directory point to that file and it does not remove the underlying file opened by the process.

Only until the offending processes close the file, either explicitly close the file with close() or manually kill the process, the reference to the file returns to zero and space occupied by the file is freed. A common example is Oracle writing to a trace files and these trace files grows to be large. When a DBA try to free the space by removing the trace files, he may found df command does not reflect the correct result. Under most circumstances, the most easiest way to free the space from the trace files is bounce the Oracle instance.

Below is an example performed 0n Solaris 10 to demonstrate how the space allocated to a file is not freed after removing the file. 

First we check the disk usage of the tmp diretory.
testsvr$ df -h|grep -i /tmp
swap                    78G   488K    78G     1%    /tmp

We run dd command to create a file named testfile in /tmp in the background. Since we are creating a 30GB file, it takes some time complete and the background process will keep the file open.
testsvr$ nohup dd if=/dev/zero of=/tmp/testfile bs=1048576 count=307200 &
[3]     17460
testsvr$ Sending output to nohup.out
testsvr$ ls -al /tmp/testfile
-rw-r--r--   1 oracle   dba      1144528896 May 18 23:42 /tmp/testfile
testsvr$ df -h|grep -i /tmp
swap                    78G   1.9G    77G     3%    /tmp

Here we can see that the dd command is still running and the size of the /tmp/testfile is growing
testsvr$ ps -ef |grep dd
  oracle 17841 12384   0 23:42:15 pts/7       0:00 grep dd
  oracle 17460 12384   2 23:42:00 pts/7       0:15 dd if=/dev/zero of=/tmp/testfile bs=1048576 count=307200

Then we delete the file.
testsvr$ rm /tmp/testfile
testsvr$
testsvr$ ls -al /tmp/testfile
/tmp/testfile: No such file or directory

We spotted the process id 17460 is opening a file with no named filename linked to it.
testsvr$ find /proc/*/fd -type f -links 0 \! -size 0 -ls

   67    8 --w-------   0 oracle   dba           639 Dec  9 05:16 /proc/1328/fd/5
427559811 6944224 --w-------   0 oracle   dba      7110885376 May 18 23:42 /proc/17460/fd/4
   67    8 --w-------   0 oracle   dba           639 Dec  9 05:16 /proc/25269/fd/5
   31    8 --w-------   0 oracle   dba          1418 Apr 16 11:03 /proc/25280/fd/2
   67    8 --w-------   0 oracle   dba           639 Dec  9 05:16 /proc/25280/fd/5
   67    8 --w-------   0 oracle   dba           639 Dec  9 05:16 /proc/25286/fd/5
   67    8 --w-------   0 oracle   dba           639 Dec  9 05:16 /proc/25297/fd/5
   67    8 --w-------   0 oracle   dba           639 Dec  9 05:16 /proc/25310/fd/5
   67    8 --w-------   0 oracle   dba           639 Dec  9 05:16 /proc/25322/fd/5
   67    8 --w-------   0 oracle   dba           639 Dec  9 05:16 /proc/25326/fd/5
  102    8 --w-------   0 oracle   dba          5015 May 13 14:25 /proc/25332/fd/2
 
testsvr$ df -h|grep -i /tmp
swap                    78G    10G    68G    14%    /tmp

Then we kill the process 17460.
testsvr$ kill -9 17460
testsvr$ find /proc/*/fd -type f -links 0 \! -size 0 -ls

   67    8 --w-------   0 oracle   dba           639 Dec  9 05:16 /proc/1328/fd/5
   67    8 --w-------   0 oracle   dba           639 Dec  9 05:16 /proc/25269/fd/5
   31    8 --w-------   0 oracle   dba          1418 Apr 16 11:03 /proc/25280/fd/2
   67    8 --w-------   0 oracle   dba           639 Dec  9 05:16 /proc/25280/fd/5
   67    8 --w-------   0 oracle   dba           639 Dec  9 05:16 /proc/25286/fd/5
   67    8 --w-------   0 oracle   dba           639 Dec  9 05:16 /proc/25297/fd/5
   67    8 --w-------   0 oracle   dba           639 Dec  9 05:16 /proc/25310/fd/5
   67    8 --w-------   0 oracle   dba           639 Dec  9 05:16 /proc/25322/fd/5
   67    8 --w-------   0 oracle   dba           639 Dec  9 05:16 /proc/25326/fd/5
  102    8 --w-------   0 oracle   dba          5015 May 13 14:25 /proc/25332/fd/2

After killing the dd command process, the space allocated to file /tmp/testfile is freed.
testsvr$  df -h |grep -i /tmp
swap                    84G   488K    84G     1%    /tmp

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati

Oracle Learning Library

Though I've knew Oracle has a good learning tutorial named Oracle By Example for a long time, only until few days ago I realized they have a cool page called  Oracle Learning Library which is more fruitful than OBE. This library provides an access to different learning learning information such as, tutorial, OBE and demo etc for different Oracle products and, above all, a good documents searching page. Like!

http://apex.oracle.com/pls/apex/f?p=44785:1:0::NO

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati

Oracle 11g Enterprise Edition and Standard edition features comparison

Knowing the Oracle features are important for people who are constantly working with Oracle database. From time to time, I have been asking about the functions that Oracle can provide and compare the different features between Oracle editions during different phases of database project.

Here is a short lists of important Oracle features that are only available in Oracle 11g Enterprise edition but not Standard/Standard one. Most of this features are used to provide high availability and performance which are useful especially when you are administrating a large and mission-critical databases

1. All dataguard features
-Physical,Logical and Active dataguard(needs extra license) are only available in Enterprise editiion.

2. Online maintenance
-online index rebuild, online IOT rebuild, online table redefinition.

3. RMAN features
-Block tracking incremental backup, block-level media recovery,parallel backup and recovery, tablespace point-in-time recovery, duplexed backup sets, unused block compression.

4. Flashback features
-Flashback Table, Flashback Database, Flashback Transaction, Flashback Transaction Query.

5. Query cache
-Client Side query cache, Query Results cache, PL/SQL Function Result cache

6. Fine-grained auditing

7. Oracle Database Vault (Requires extra license)

8. Advanced Security option(Requires extra license)

9. Partitioning
-Need to pay extra license even under Enterprise Edition

10. Compression
-Basic Table Compression
-Advanced Compression(requires separate license):DML, RMAN, Datadump, SecureFiles and redo log(Dataguard) transport compression.

11. Parallelism
-Parallel query/DML, Parallel statistics gathering, Parallel index build/scans,Parallel DataPump, Parallel in-memory execution.

12. Export Transportable tablespaces
-Standard edition can only perform transportable tablespaces import, not export.

13. Updateable MV
-Standard versions only support readonly materialized view.

14. Multi-master replication

15. Oracle Streams
-Standard cannot perform redo capture

16. Oracle RAC One Node

17. Oracle Diagnostic Pack(Requires extra license)

18. Oracle Tuning Pack(Requires extra license, also requires the Diagnostic Pack

19. Database Resource Manager

20. SQL Plan Management

21. Oracle OLAP(Requires extra license)

22. Oracle Data Mining(Requires extra license)

23. Bitmapped index, bitmapped join index, and bitmap plan conversions


To determine what Oracle features you have used in a Oracle Enterprise Edition databae, you can query dba_feature_usage_statistics


Set linesize 300
Col name             format a50   
Col version          format a10    
Col currently_used   format a10   
Col first_used format a20    
Col last_used  format a20    

Select name, version, detected_usages, currently_used,
       to_char(first_usage_date,'HH24:Mi DD/MM/YYYY') first_used,
       to_char(last_usage_date,'HH24:Mi DD/MM/YYYY') last_used
from dba_feature_usage_statistics
order by 1, 2

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati

Using ADRCI to view or monitor the content of alert log

In Oracle 11g,  ADRCI tool is the recommended way to view or monitor the alert log. 

To successfully use ADRCI to view the alert log, we need to make sure the ADRCI home are set correctly.We can determine the ADRCI homepath by using the show home or show homepath commands


adrci> show homes

adrci> show homepth

adrci> set homepath

By default, ADRCI displays the alert log in your default editor. You can use the SET EDITOR command to change your default editor. If no editor is set, ADRCI uses the editor specified by the operating system environment variable $EDITOR

adrci> set editor vi

To display all contents of the alert log
adrci> show alert

To display the last 10 entries of the alert log
adrci> show alert -tail

To display the last 30 entries of the alert log
adrci> show alert -tail 30

To display the last 10 entries and  then waits for more messages to arrive in the alert log
SHOW ALERT -TAIL -F

This displays only alert log messages that contain the string 'ORA-600'
SHOW ALERT -P "MESSAGE_TEXT LIKE '%ORA-600%'"

In Oracle 11gR1, there is bug(Bug 7648929) in "show alert" and you may receive "DIA-48216: Field Len Exceeds Max Field Length [5] [0] [20] [20]" due to the corruption of xml format alert log. To avoid this error, you can upgrade the database to 11gR2 or use "show alert -tail" instead.

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati

Changing the GUI language in SQL Developer

By default, SQL Developer uses the default language of the operating system, e.g.
in Windows platform, the language settings in Regional and Language Options.

There are two ways to change the GUI language in SQL Developer.
1. Change the regional settings .
  In Windows go to Control Panel -> Regional and Language Options
(This also affects the language used on your computer and other applications and you may need to restart machine to be effective)

2.  Edit \sqldeveloper\bin\sqldeveloper.conf file
 - Add/modify the following parameters:

AddVMOption -Duser.language=en
AddVMOption -Duser.country=US
AddVMOption -Duser.region=US

Then, restart the SQL Developer

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati

SQL Developer 3.0 officially released

SQL Developer 3.0 has been officially released in Mar-2011 and is now available for download. It is good to see Oracle has added many new features, especially Oracle Data Modeler now has been integrated with it. Though I cannot say SQL Developer 3 can be a complete replacement for other commercial SQL development and database management products like Toad and PL/SQL, it is enough for DBA daily tasks and development.

SQL Developer still has its advantage -it is FREE ;-)

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati

Indexes are not used on NULLable columns

One of the most common reason why index is not used is because columns are defined as nullable when, in fact, those columns should be not null.

Here is an example.

create table t( c1 number, c2 varchar2(4000));
create index t_idx on t(c1);

begin
for i in 1..10000 loop
insert into t values(i,'abc');
end loop;
end;
/

exec dbms_stats.gather_table_stats('TEST','T');

select c1 from t;
Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      | 10000 | 40000 |    18   (0)| 00:00:01 |
|   1 |  TABLE ACCESS FULL| T    | 10000 | 40000 |    18   (0)| 00:00:01 |
--------------------------------------------------------------------------

This is because c1 is nullable (can be null) , so the optimizer cannot rely on the index as there might be missed rows .
Since the every row in c1 contains value(from 1 to 10000), column c1 should be defined as not null instead of null.

alter table t modify (c1 not null);

select c1 from t;
Execution Plan
----------------------------------------------------------
Plan hash value: 3163761342

------------------------------------------------------------------------------
| Id  | Operation            | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT     |       | 10000 | 40000 |     7   (0)| 00:00:01 |
|   1 |  INDEX FAST FULL SCAN| T_IDX | 10000 | 40000 |     7   (0)| 00:00:01 |
------------------------------------------------------------------------------

After redefining the column c1 as not null, index t_idx is used and cost of this query is reduced from 18 to 7.

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati

ADR files auto housekeeping

One of the features in Oralce I have been longing for is automatic dump and trace files housekeep. In Oracle 10g and before, DBA / System Admin have to write their own scripts and scheduled it to run periodically in order to remove those old dump and trace files.
In Oracle 11g, trace, dump and alert files in ADR are automatically purged by the MMON process. The retention period for the these files can be controlled via two  attributes -- LONGP_POLICY abd SHORTP_POLICY.

To view the current ADR retention policy, you can run the following command

ADRCI>show control
ADRID                SHORTP_POLICY        LONGP_POLICY         LAST_MOD_TIME                            LAST_AUTOPRG_TIME                        LAST_MANUPRG_TIME                        ADRDIR_VERSION       ADRSCHM_VERSION      ADRSCHMV_SUMMARY     ADRALERT_VERSION     CREATE_TIME
-------------------- -------------------- -------------------- ---------------------------------------- ---------------------------------------- ---------------------------------------- -------------------- -------------------- -------------------- -------------------- ----------------------------------------
248536116            720                  8760                 2010-09-21 18:17:08.915859 +08:00        2011-02-15 00:20:57.784615 +08:00                                                 1                    2                    65                   1                    2010-09-21 18:17:08.915859 +08:00
1 rows fetched

By default, LONGP_POLICY and SHORTP_POLICY are set to 8760 and 360 hours respectively.

To change the value of retention policy, use set control command

ADRCI> set control (SHORTP_POLICY = 240)

Note that files that are purged based on LONGP_POLICY includes the following:
-files in the alert directory
-files in the incident/incdir_ directory and metadata(in the directories metadata,ir,lck)  in the incident schema 
-files in the sweep directory
-files in the stage directory
-HM — files in the hm directory and metadata in the HM schema

And files that are purged based on SHORTP_POLICY are:
-files in the trace directory
-files in the cdump directory
-files in the trace/cdmp_ directories 
-files in the incpkg directory and metadata in the IPS schema

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati

Oracle Flashback Table

Flashback Table, available since Oracle 10g, allows user to recover a table to a previous point in time/scn. This feature depends entirely on undo information in the undo segments to perform the recovery and no archivelog will be applied during recovery.

According to Oracle documents, Flashback Table is recommended when 5-10% data of the table being changed. For a large number of changed rows, table ,tablespace or database level recovery would be better.

Here is some points to note when using Flashback Table...

1. To use Flashback Table, a user needs to following privileges
i) FLASHBACK ANY TABLE  or FLASHBACK object privilege
ii) SELECT, INSERT, DELETE, and ALTER privileges on the table.

2. You have to enable row movement in the tables you are recovering.

3. You must be using automatic undo management to use the flashback table feature.

4. How far a table can be flashed back relies on the amount of undo data in the database. Therefore, undo tablespace size and undo retention time are critical in your successfulness of recovery.

5. Oracle hold exclusive DML locks on the table or tables that it is being recovered but the tables continue to remain online.

6. You can’t flash back any of the SYS user’s objects, recovered objects, or a remote table.

8. ROWID will not be preserved for restored data blocks of the table. That's why you have to enable row movement on the tables being recovered.

9. You cannot recover a table to a time before any DDL statements on that table, including any changes on table storage attributes.

10. You cannot flashback on tables with virtual columns.

11. Whenever possible, perform flashback table to scn instead of to timestamp. Since Flashback Table works on scn internally, and timestamp to scn mapping is not always accurate.

12. If you have constraints on the table, you need to disable those constraints before the Flashback Table and enable it with validate after the Flashback.

13. To flashback a table with materialized view, drop the materialized view with preserve clause. Then flashback the table and rebuild the materialized view.

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati

Oracle 11g SQLPLUS error logging

In Oracle 11g, error messages in the SQLPLUS can be recorded to a table automatically when ERRORLOGGING option is turned on.
By default, this feature is off, you can enable this feature just by typing "SET ERRORLOGGING ON" in SQLPLUS and a table called sperrorlog will be created in the current schema. Then all subsequent errors generating from SQL or PL/SQL statements will be written to sperrorlog.


Here is an example

SQL> set errorloggin on
SQL> desc sperrorlog
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 USERNAME                                           VARCHAR2(256)
 TIMESTAMP                                          TIMESTAMP(6)
 SCRIPT                                             VARCHAR2(1024)
 IDENTIFIER                                         VARCHAR2(256)
 MESSAGE                                            CLOB
 STATEMENT                                          CLOB

SQL> select * from aaa;
select * from aaa
              *
ERROR at line 1:
ORA-00942: table or view does not exist


SQL> select timestamp, username,statement, message  from sperrorlog;

TIMESTAMP
---------------------------------------------------------------------------
USERNAME
--------------------------------------------------------------------------------
STATEMENT
--------------------------------------------------------------------------------
MESSAGE
--------------------------------------------------------------------------------
22-MAR-11 04.47.11.000000 PM
SYS
select * from aaa
ORA-00942: table or view does not exist

Find more from Oracle documents:
http://download.oracle.com/docs/cd/B28359_01/server.111/b31189/ch12040.htm#SQPUG160

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati

Oracle Database 11g: Interactive Quick Reference...Your essential guide to Oracle 11g?


Heard from my colleagues that Oracle has released Oracle Database 11g: Interactive Quick Reference. I was tempted to download it and give it a try. However, I was little disappointed by the content of this reference and found it too far to be a reference for my DBA work. ;=(

Hope that there will be improvement for the next release of this reference, just like SQL Developer.

You can the quick reference file through this link:
http://www.oracle.com/webapps/dialogue/ns/dlgwelcome.jsp?p_ext=Y&p_dlg_id=9575302&src=7027600&Act=54

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati