You are on page 1of 14

1/17/12

Oracle SQL tricks

initora

Ma 20, 201 0

VM

NEW UPDAT ES: SCROL L BEL OW FOR NEW SQL T RICKS

UPDATED NEWS:

8 NEW KNOLS (APRIL-MAY 2010)

://

/ /

- /

- -

/232 3

/62#

Oracle applications scripts

With Example

://

/ /

- /

/232 3

/46#

Oracle SQL Rewriting

Faster Performance

://

/ /

- /

/232 3

/45#

LEARN JAV AFX & ANI MATI ON W I TH EX AMP LE

A S TEP BY S TEP GUI DE

:// ://

. .

. .

/ / / /

- / /

- -

- -

- /232 3 /40#

/44#

-11 - 2/232 3

What can you do with your Oracle Database?

:// ://

. .

. .

/ / / /

/ /

Q&A

/232 3 - - /232 3

/33# /34#

Oracle Performance T ning In er ie

LEARN ORACLE OEM AND ALL S TUFFS & TRI CKS


initora.wordpress.com/article/oracle-sql-tricks-232y3pcqwxodx-2/ 1/14

1/17/12

Oracle SQL tricks

initora

http://knol.google.com/k/mayur-vora/learn-oracle-oem-and-all-stuffs-tricks/232y3pcqwxodx/35# Learn Oracle 10g Architecture with diagram: http://knol.google.com/k/mayur-vora/oracle-10g-architecture-with-diagram/232y3pcqwxodx/32# Learn RMAN to clone Database: http://knol.google.com/k/mayur-vora/rman-to-clone-database/232y3pcqwxodx/6# Eas wa to install Oracle 10gR2 on Linus CentOS Step b Step: http://knol.google.com/k/easy-way-to-install-oracle-database-10g-release-2-on-linux-centos-step-bystep#

New Updates: SQL Tricks tested on Oracle DB: Tuning Oracle ith hidden parameters

initora.wordpress.com/article/oracle-sql-tricks-232y3pcqwxodx-2/

2/14

1/17/12

Oracle SQL tricks

initora

Connect system/manager as sysdba; select a.ksppinm b.ksppstvl b.ksppstdf decode (a.ksppity, 1, boolean , 2, string , 3, number , 4, file , a.ksppity) type, a.ksppdesc from sys.x$ksppi a, sys.x$ksppcv b where a.indx = b.indx and a.ksppinm like _% escape order by name; description name, value, default,

Viewing RAM memor

usage for specific SQL statements in Oracle

initora.wordpress.com/article/oracle-sql-tricks-232y3pcqwxodx-2/

3/14

1/17/12

Oracle SQL tricks

initora

slc eet ades drs fo rm vsl $q wee hr sltx lk %E_UTMR; q_et ie NWCSOE 8B40 8B6C 1rwslce. o eetd

slc eet oeain prto, otos pin, ojc_ae betnm tucbts12/04 rn(ye/0412) tucls_eoyue/04 rn(atmmr_sd12) tucetmtdotmlsz/04 rn(siae_pia_ie12) tucetmtdoeassz/04 rn(siae_nps_ie12) dcd(pia_xctos nl,nl, eoeotmleeuin, ul ul otmleeuin| /|oeaseeuin| /| pia_xctos| '|nps_xctos| '| mliasseetos utpse_xcin) fo rm vslpa $q_ln wee hr padeswades+ .drs=.drs() ad n phs_au=.ahvle+ .ahvlewhs_au() ad n pi=.prto_d+ .dwoeaini() ad n pades 8B40 ; .drs= 8B6C p , vslwrae w $q_okra // O1M nu(B iptM), ls_e, atmm otmm p_e, oeasmm nps_e, nm, ae

OEAIN PRTO SLC SAE EET TT SR OT 1 6 HS JI AH ON 59 14 5 1

OTOS NM iptM)LS_E OTMMOEASMMO1M PIN AE nu(B ATMM P_E NPS_E //

- - - GOPB RU Y 1 6 28 17 2// 600 SM EI 1// 600 ODR RES


4/14

48 52 48 52

8 57 96

TBEACS FL AL CES UL

initora.wordpress.com/article/oracle-sql-tricks-232y3pcqwxodx-2/

1/17/12

Oracle SQL tricks

initora

TBEACS FL AL CES U

LNIE IETM

10 00

Finding a file on

our Oracle server

tnsnames.ora file listener.ora file location alert log file location trace and dump file locations sqlnet.log file location(s)

fn . pitge Iaetmr id rn|rp lr|oe

dr/ aet.o|oe i s lr*lgmr

Finding the most recent files in a director

l at*tcha s l .r|ed

dr*tcODmr i .r/-|oe

initora.wordpress.com/article/oracle-sql-tricks-232y3pcqwxodx-2/

5/14

1/17/12

Oracle SQL tricks

initora

pop laeetrtenm o tefl t fn: rmt pes ne h ae f h ie o id acp &ieae cet flnm hs fn /-rn|rp &ieae ot id pitge I flnm

Adding C

om Me

age

o he O acle ale

log

we ohr te hn tes hn dm_utmwieaet:_ydt| Apiainerr|:a1| bscso.rt_lr(pssae| plcto ro |vr| ecutrd) nonee ;

1. 2. 3. 4. 5.

****************************************************** Gather the location of the alert log directory


initora.wordpress.com/article/oracle-sql-tricks-232y3pcqwxodx-2/ 6/14

1/17/12

Oracle SQL tricks

initora

****************************************************** select name into :alert_loc from v$parameter where name = background_dump_destination ;

****************************************************** Set the utl_file_dir (prior to Oracle9i, you must bounce the database) ******************************************************

alter system set utl_file_dir = :alert_log );

****************************************************** Open the alert log file for write access ****************************************************** utl_file.fopen( :alert_loc , alertprod.log , W');

****************************************************** Write the custom message to the alert log file ******************************************************

dbms_output.put_line( invalid_application_error );

****************************************************** Close the alert log file ******************************************************


initora.wordpress.com/article/oracle-sql-tricks-232y3pcqwxodx-2/ 7/14

1/17/12

Oracle SQL tricks

initora

utl_file.fclose( :alert_loc );

Di

ib

ing

anda d O acle file

o all Se

tnsnames.ora sqlnet.ora .profile for the Oracle UNIX user

#/i/s !bnkh eh satn dsrbto o taae.r co trig itiuin f nnmsoa #Nt:dnmsfl i i tefr HS DTBS oe bae ie s n h om OT AAAE frhs i `a dnmsak {pit$ }` o ot n ct bae|w rn 2 d o d=ctdnmsak {pit$ }` b`a bae|w rn 1 eh co satn dsrt $ot trig it o hs rp- tsae.r $ot/t/nnmsoa c p nnmsoa hs:ectsae.r rh$otl l/t/nnmsoa s hs s a ectsae.r dn oe

doee msd ings yi1 doee po9 ings rdi doee tsm ings et1 patr reo patr reo ts9 eti tsap etps

He e a e ome ef l SQL ick , e ed on an O acle da aba e. initora.wordpress.com/article/oracle-sql-tricks-232y3pcqwxodx-2/ 8/14

1/17/12

Oracle SQL tricks

initora

Virtual inde In Oracle 10 and Oracle 9


V i a id i afa e fOa e1 adOa e9 Te e e c 0 c . h e f i a id e i i ae h ei ec fa id , ih e e e e a a c bid gi, i c e a a i a ai h id ei e.I i i f e e d a d h id a e e e f 'ed e ihi. T bida i SL CET Q> RAE id *; e / T a id e h f e ig aee : a

i eIDXid _ae NE e

a e aec _ae NSGET/ i b _ ( ) OEMN *

e h id e e

h f e

ig

aee :

SL ATRSSINST" e Q> LE ESO E _ _

e e _ d e"=TU; g i e RE h f e ig (h id e i e

T d e a i a id i h d aae e c e e a b d ' h ea c a i daidc b_ _ ) : SL SLC id _ Q> EET e 'I$'IUSLC BN%MNSEET

e,id _aeFO daidc e RM b_ _ WEEid _aeNTLK HR e O IE e,id _aeFO daid e/ e RM b_ e

Does Oracle use m inde or not?


One can use the inde monitoring feature to check if inde es are used b an application or not. When the MONI O ING AGE propert is set for an inde , one can quer the v$object_usage to see if the inde is being used or not. Here is an e ample: SQL> CREATE TABLE t1 (c1 NUMBER); Table created. SQL> CREATE INDEX t1_id ON t1(c1); Inde created. SQL> ALTER INDEX t1_id MONITORING USAGE; Inde altered. SQL> SQL> SELECT table_name, inde _name, monitoring, used FROM v$object_usage; TABLE_NAME INDEX_NAME MON USE
initora.wordpress.com/article/oracle-sql-tricks-232y3pcqwxodx-2/ 9/14

1/17/12

Oracle SQL tricks

initora

T1 T1_IDX YES NO SQL> SELECT * FROM t1 WHERE c1 = 1; no rows selected SQL> SELECT table_name, index_name, monitoring, used FROM v$object_usage; TABLE_NAME INDEX_NAME MON USE T1 T1_IDX YES YES To reset the values in the v$object_usage view, disable index monitoring and re-enable it: ALTER INDEX indexname NOMONITORING USAGE; ALTER INDEX indexname MONITORING USAGE;

Find last N records from table


I think I have found a easy way to select last n record from a table. below is my SQL statement: select * from (select rownum a, column1,column2 ,,columnn from my_table) where a > ( select (max(rownum)-10) from my_table); where my_table is the sample table s name ; column1 to columnn are the columns in the table, you can replace 10 as the n where you want to select;

How to move tablespace to a new location


1) Take the tablespace offline ALTER TABLESPACE tablespace-name OFFLINE; 2) Use the OS to Move the tablespace to the new location

3) Run the following command: ALTER TABLESPACE tablespace-name RENAME DATAFILE OS path to old tablespacetablespace-datafile- name.dbf TO OS path to new locationtablespacedatafile-name.dbf ; 4) Take the tablespace back online
10/14

initora.wordpress.com/article/oracle-sql-tricks-232y3pcqwxodx-2/

1/17/12

Oracle SQL tricks

initora

ALTER TABLESPACE tablespace-name ONLINE; If the following error is displayed ORA-01113: file n needs media recovery ORA-01110: data file n: new location file name Issue the the following command: recover datafile new location file name ; and take the tablespace back online ALTER TABLESPACE tablespace-name ONLINE;

Simple solution for ORA-12638: Credential retrieval failed


If the Oracle client was installed using the domain administrator account instead of the local administrator, the following message could be displayed when trying to connect: ORA-12638: Credential retrie al failed From the wording of the error message one could think that this error means that he is using the wrong password but this is not the case. To solve this: 1) Go to /oracle home/network/admin and open sqlnet.ora 2) Search for SQLNET.AUTHENTICATION_SERVICES= (NTS) and change it to SQLNET.AUTHENTICATION_SERVICES= (NONE) 3) Save the file and try to connect, this should work

What to do when Enterprise Manager is not able to connect to the database instance (ORA-28001) If you are trying to connect to the Oracle enterprise Manger and you get the following errors: Enterprise Manager is not able to connect to the database instance. And in t Enterprise Manager is not able to connect to the database instance. And in the Agent connection to instance section: Status: Failed Details: ORA-28001: the password has expired (DBD ERROR:
initora.wordpress.com/article/oracle-sql-tricks-232y3pcqwxodx-2/ 11/14

1/17/12

Oracle SQL tricks

initora

OCISessionBegin) One of the reasons could be that the password for the SYSMAN user is expired. However, changing the password alone will not solve this issue. Several additional steps are required in order to make Oracle Enterprise Manager connect: Before you start: Verify that ORACLE_HOME, ORACLE_SID environment variables are set. If not, set them using as environment variables or open a command line and type SET ORACLE_SID=<The database SID> 1. Stop the dbconsole: emctl stop dbconsole (dos and Unix) or using the windows services stop the OrcleDBConsole<The database SID>. 2. Connect to the database as a user with DBA privilege with SQL*Plus and run the following command: alter user s sman identified b <the new password> ; 3. Verify that the new password works SQL> connect s sman/<the new password> 4. Go to ORACLE_HOME/<HostName_SID>/sysman/config and save a backup of the emoms.properties file. a) Open the file emoms.properties and search for: oracle.s sman.eml.mntr.emdRepPwd=<some encrypted value> Replace the encrypted value with the new password value b) Search for oracle.s sman.eml.mntr.emdRepPwdEncr pted=TRUE and change TRUE to FALSE c) Save and close emoms.properties 5. Restart the dbconsole: emctl start dbconsole (dos and Unix) or using the windows services start the OrcleDBConsole<The database SID>. 6. Open emoms.properties again and Search for:
initora.wordpress.com/article/oracle-sql-tricks-232y3pcqwxodx-2/ 12/14

1/17/12

Oracle SQL tricks

initora

a)oracle.s sman.eml.mntr.emdRepPwd= verify that the password is encrypted b) oracle.s sman.eml.mntr.emdRepPwdEncr pted= verify that the value is set to TRUE 7. Refresh Oracle Enterprise Manager
-

Display BAD Index


select owner|| .'||index_name|| STATUS= ||status status from dba_indexes where status= UNUSABLE union all select owner|| .'||index_name|| DOMIDX_STATUS= ||domidx_status status from dba_indexes where domidx_status= IDXTYP_INVLD union all select owner|| .'||index_name|| DOMIDX_OPSTATUS= ||domidx_opstatus status from dba_indexes where domidx_opstatus= FAILED union all select owner|| .'||index_name|| FUNCIDX_STATUS= ||funcidx_status status from dba_indexes where funcidx_status= DISABLED /
-

Steps to create Stand


1. Shutdown Normal the primary database

by Database

2. Copy all of the tablespace datafiles to the standby datafile directory Note: This may take a long time depending on datafile size and network traffic OS>COPY <drive>:OracleORADATA<sid>*.dbf <drive>:OracleORADATASTANDBY*.* OS>COPY *.arc <drive>:OracleORADATASTANDBYArchive*.* 3. Ensure the following INIT.ORA parameters are set on the primary DB: LOG_ARCHIVE_DEST = <drive>:OracleORADATA<sid>Archive LOG_ARCHIVE_FORMAT = LOG%s%t.ARC LOG_ARCHIVE_START = TRUE 4. StartUp the primary DB with archive logging turned on SVRMGRL>connect internal SVRMGRL>startup mount <sid>; SVRMGRL>alter database archivelog;
initora.wordpress.com/article/oracle-sql-tricks-232y3pcqwxodx-2/ 13/14

1/17/12

Oracle SQL tricks

initora

SVRMGRL>archive log start; SVRMGRL>alter database open; . create a standby control file SVRMGRL>alter database create standby controlfile as <drive>:OracleORADATASTANDBYstandbycf.ctl ; MOVE standbycf.ctl control01.ctl COPY control01.ctl control02.ctl COPY control01.ctl control03.ctl . Update and/or Create TNSNAMES.ora and LISTENER.ora files to include standby db . Copy the OracleAdmin<sid> directory to the corresponding standby db directory . Modify the init.ora file as follows: DB_NAME must remain the same as the primary database s entry STANDBY_ARCHIVE_DEST = <drive>:OracleORADATASTANDBYArchive LOG_ARCHIVE_DEST = <drive>:OracleORADATASTANDBYArchive LOCK_NAME_SPACE = STANDBY DB_FILE_NAME_CONVERT = ( <drive>:OracleORADATA<sid> ,'<drive>:OracleORADATASTANDBY ) LOG_FILE_NAME_CONVERT = ( <drive>:OracleORADATA<sid>Archive ,'<drive>:OracleORADATASTANDBYArchive ) . Use ORADIM to create StandBy Oracle DB service . Copy the password file PWD<sid>.ora to PWDSTANDBY.ora . SVRMGRL>Connect sys/change_on_install@standby . SVRMGRL>STARTUP NOMOUNT pfile=OracleSTANDBYAdminPFileinit.ora; . SVRMGRL>ALTER DATABASE MOUNT STANDBY DATABASE; . SVRMGRL>ALTER DATABASE RENAME FILE old_name TO new_name ; . Copy over any archive log files . SVRMGRL>RECOVER STANDBY DATABASE; . SVRMGRL> <Ret> | FILENAME | AUTO | CANCEL
Like hi : Like Be he fi o like hi a icle.

initora.wordpress.com/article/oracle-sql-tricks-232y3pcqwxodx-2/

14/14

You might also like