Upload
kamalbhardwaj123
View
233
Download
0
Embed Size (px)
Citation preview
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 1/92
QUERIESSTATUS
Startup time
select to_char(startup_time, 'HH24:MI DD-MON-YY') "tartup time"!rom #i$sta$ce%
How large is the database
col "Data&ase ie" !ormat a2col "ree space" !ormat a2col "*se+ space" !ormat a2select rou$+(sum(use+&tes) % .24 % .24 % .24 ) // ' 01' "Data&ase ie", rou$+(sum(use+&tes) % .24 % .24 % .24 ) -
rou$+(!reep % .24 % .24 % .24) // ' 01' "*se+ space", rou$+(!reep % .24 % .24 % .24) // ' 01' "ree space"!rom (select&tes !rom #+ata!ile u$io$ all
select &tes !rom #temp!ile u$io$ all select &tes !rom #lo) use+,
(select sum(&tes) as p !rom +&a_!ree_space) !ree roup & !reep %
Distribution of objects and data
Which schemas are taking up all of the spaceset paes 333col "sie M1" !ormat 333,333,333col "O&ects" !ormat 333,333,333select o&o5$er "O5$er",o&_c$t "O&ects",+eco+e(se_sie, N*66, , se_sie) "sie M1"!rom (select o5$er, cou$t(7) o&_c$t!rom +&a_o&ects roup & o5$er) o&,(select o5$er, ceil(sum(&tes)%.24%.24) se_sie!rom +&a_seme$ts roup & o5$er) se5here o&o5$er 8 seo5$er(9)or+er & +esc ,2 +esc, .%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 2/92
Show the ten largest objects in the database
;ol o5$er !ormat a.<;ol seme$t_$ame !ormat a;ol seme$t_tpe !ormat a.<;ol m& !ormat 333,333,333
select o5$er,seme$t_$ame,seme$t_tpe,m&!rom(select o5$er,seme$t_$ame,seme$t_tpe,&tes % .24 % .24 "M1"!rom +&a_seme$tsor+er & &tes +esc)5here ro5$um = ..%
Is jaa installed in the database!
This will return 9000'ish if it is...select cou$t(7)
!rom all_o&ects5here o&ect_tpe li>e '?@ABA?'a$+ o5$er 8 'Y'%
Displa" character set information
select 7 !rom $ls_+ata&ase_parameters%
Show all used features
select $ame, +etecte+_usaes!rom +&a_!eature_usae_statistics5here +etecte+_usaes C %
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 3/92
SESSION
Show all connected users
set li$es . paes 333
col ID !ormat a.<select user$ame,si+ // ',' // serial "ID",status,last_call_et "6ast Actiit"!rom #sessio$5here user$ame is $ot $ullor+er & status +esc,last_call_et +esc
/
Time since last user actiit"
set li$es . paes 333select user$ame
, !loor(last_call_et % E) "Mi$utes", status!rom #sessio$5here user$ame is $ot $ullor+er & last_call_et
/
Sessions sorted b" logon time
set li$es . paes 333col ID !ormat a.<col osuser !ormat a.<col loi$_time !ormat a.4select user$ame, osuser, si+ // ',' // serial "ID", status, to_char(loo$_time, 'hh24:mi ++%mm%') loi$_time, last_call_et!rom #sessio$5here user$ame is $ot $ullor+er & loi$_time
/
Show user info including os pid
col "ID%FGIA6" !ormat a.col user$ame !ormat a.<col osuser !ormat a.<col proram !ormat a4select ssi+ // ',' // sserial "ID%FGIA6", suser$ame, sosuser, pspi+ "O ID", sproram
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 4/92
!rom #sessio$ s, #process phere spa++r 8 pa++ror+er & to_$um&er(pspi+)
/
Show a users current s#l
elect sJl_teKt!rom #sJlarea5here (a++ress, hash_alue) i$(select sJl_a++ress, sJl_hash_alue
!rom #sessio$ 5here user$ame li>e 'Luser$ame')
/
Session status associated with the specified os process id
select suser$ame, ssi+, sserial, pspi+, last_call_et, status!rom B#FION s, B#GO;F p5here sADDG 8 pADDGa$+ pspi+8'Lpi+'
/
All actie s#l
set !ee+&ac> o!!set sereroutput o$ sie 3333colum$ user$ame !ormat a2colum$ sJl_teKt !ormat a<< 5or+_5rappe+&ei$ !or K i$ (select user$ame//'('//si+//','//serial//') ospi+ 8 '// process // ' proram 8 ' // proram user$ame, to_char(6O0ON_IMF,' Da HH24:MI') loo$_time, to_char(ss+ate,' Da HH24:MI') curre$t_time, sJl_a++ress, sJl_hash_alue !rom #sessio$
5here status 8 'A;IBF' a$+ ra5toheK(sJl_a++ress) =C '' a$+ user$ame is $ot $ull ) loop !or i$ (select sJl_teKt !rom #sJlarea 5here a++ress 8 KsJl_a++ress ) loop i! ( sJl_teKt $ot li>e '?liste$eret_cm+?' a$+ sJl_teKt $ot li>e '?GAOHF(6_ADDGF)?' ) the$ +&ms_outputput_li$e( '--------------------' )P
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 5/92
+&ms_outputput_li$e( Kuser$ame )P +&ms_outputput_li$e( Kloo$_time // ' ' // Kcurre$t_time // ' 68' //KsJl_hash_alue)P +&ms_outputput_li$e( su&str( sJl_teKt, ., 2< ) )P e$+ i!P e$+ loopP e$+ loopPe$+P
/
Displa" an" long operations
set li$es . paes 333col user$ame !ormat a.<col messae !ormat a4col remai$i$ !ormat 3333select user$ame, to_char(start_time, 'hh24:mi:ss ++%mm%') starte+, time_remai$i$ remai$i$
, messae!rom #sessio$_lo$ops5here time_remai$i$ 8 or+er & time_remai$i$ +esc%
$ist open cursors per user
set paes 333select sessuser$ame, sesssi+, sessserial, statalue cursors
!rom #sesstat stat, #stat$ame s$, #sessio$ sess5here sessuser$ame is $ot $ulla$+ sesssi+ 8 statsi+a$+ statstatistic 8 s$statistica$+ s$$ame 8 'ope$e+ cursors curre$t'or+er & alue
/
Or alternatiel!...set li$es . paes 333select cou$t(hash_alue) cursors
, si+, user_$ame!rom #ope$_cursorroup &
si+, user_$ameor+er &
cursors%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 6/92
I%IT &ARA'ETERS
Show latent parameter changes
select $ame
, alue!rom #parameter5here ismo+i!ie+ Q8 'A6F'
/
Show non(default parameters
set paes 333 li$es .col $ame !ormat acol alue !ormat a<select $ame, alue!rom #parameter
5here is+e!ault 8 'A6F'a$+ alue is $ot $ullor+er & $ame
/
or for name"'parameter' st!le...set paes 333 li$es .select $ame // '8' // +eco+e(tpe, 2, '''') // alue
// +eco+e(tpe, 2, '''') parameter!rom #parameter5here is+e!ault 8 'A6F'a$+ alue is $ot $ullor+er & $ame
/
Reset)Unset a spfile parameter
Setting a parameter to "' ' often isn't enough. #o this instea$...alter sstem reset =parameterC scope8sp!ile si+8'7'
/
The si$"'%' &it is alwa!s necessar! een in non ()* $ata&ase.
USER $ist users
set paes 333 li$es .col user$ame !ormat a2col status !ormat aRcol ta&lespace !ormat a2col temp_ts !ormat a2
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 7/92
select user$ame, accou$t_status status, create+, +e!ault_ta&lespace ta&lespace, temporar_ta&lespace temp_ts!rom +&a_usersor+er & user$ame%
$oc* or unloc* a user
alter user =user$ameC accou$t loc>P
And to unloc*+++
alter user =user$ameC accou$t u$loc>P
R,$E-ind a role
select 7!rom +&a_roles5here role li>e 'Lrole'
/
Show what roles are granted to a user
select ra$tee, ra$te+_role, a+mi$_optio$!rom +&a_role_pris5here ra$tee li>e upper('Luser$ame')
/
Show what s"stem priileges are granted to a role
select priilee, a+mi$_optio$!rom role_ss_pris5here role li>e 'Lrole'
/
Show what table priileges are granted to a role
select o5$er // '' // ta&le_$ame "A16F", colum$_$ame, priilee, ra$ta&le!rom role_ta&_pris5here role li>e 'Lrole'
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 8/92
%
Administration ( AS'
•
*heck if css is running/working• #ispla! $isk+groups
• Show $isk space usage
• *reate a $isk group
• )$$ a $isk to a group
• #rop a $isk group
• Is )S, performing a &alancing operation
• *heck the internal consistenc! of a $iskgroup
.hec* if css is running)wor*ing
crsctl check css$
Displa" dis*(groups
set li$es .col $ame !ormat a.col path !ormat aselect $ame, roup_$um&er, +is>_$um&er, mou$t_status, state, path
!rom #asm_+is>or+er & roup_$um&er
/note. ) group num&er of -ero his in$icates that a $isk is aaila&le &ut hasn't !et &een assigne$ to a
$isk group.
Show dis* space usage
select $ame, roup_$um&er, +is>_$um&er, total_m&, !ree_m&
!rom #asm_+is>or+er & roup_$um&er
/
.reate a dis* group
create +is>roup +ata.eKter$al re+u$+a$c+is> '%+e%ra5%ra5.'
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 9/92
/
or with multiple raw partitions...multiple +is>screate +is>roup +ata2eKter$al re+u$+a$c
+is> '%+e%ra5%ra52', '%+e%ra5%ra5'
/
or with multiple fail groups...create +is>roup +ata$ormal re+u$+a$c!ailroup co$troller. +is> '%+e%ra5%ra54'!ailroup co$troller2 +is> '%+e%ra5%ra5E'
/
Add a dis* to a group
alter +is>roup +ata.a++ +is> '%+e%ra5%ra54'
/
Wil$car$s can &e use$ for raw $eice names eg. raw%
Drop a dis* group
+rop +is>roup '=$ameC'
/
Is AS' performing a balancing operation
select 7!rom #asm_operatio$
/
,ount/$ismount $isk groupsalter +is>roup all mou$t
alter +is>roup +ata. mou$t
alter +is>roup all +ismou$t
alter +is>roup +ata. +ismou$t
.hec* the internal consistenc" of a dis*group
alter +is>roup +ata. chec> all%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 10/92
Administration ( RA. /0gR1
• Start stop an$ check *(S *luster (ea$! Serices
• Start/stop no$eapps
• Start/stop asm
• Start/stop a $ata&ase all no$es
• Start/stop an in$ii$ual instance• *heck the I1 config
• *hange the I1 a$$ress su&netmask or interface
• 2ocate the oting $isk
• (etrie O*( Oracle *luster (egistr! information
• To preent a $ata&ase starting at &oot time
• *hange the priate interconnect su&net
Start2 stop and chec* .RS 3.luster Read" Serices4
Note. Start/stop nee$ to &e run as root from the *(S home.
Start *(Scrsctl start crs
Stop *(Scrsctl stop crs
*heck *(S's statuscrsctl chec> crs
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 11/92
See the status of the arious sericescrs_stat -t
Start)stop nodeapps
srctl start $o+eapps -$ =$o+e $ameC
srctl stop $o+eapps -$ =$o+e $ameC
Start)stop asm
srctl start asm -$ =$o+e $ameC
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 12/92
srctl stop asm -$ =$o+e $ameC
Start)stop a database 3all nodes4
srctl start +ata&ase -+ =+ata&ase $ameC
srctl stop +ata&ase -+ =+ata&ase $ameC
Start)stop an indiidual instance
srctl start i$sta$ce -+ =+ata&ase $ameC -i =i$sta$ce $ameC
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 13/92
srctl stop i$sta$ce -+ =+ata&ase $ameC -i =i$sta$ce $ameC
.hec* the 5I& config
srctl co$!i $o+eapps -$ =$o+eC -a - -s -l
.hange the 5I& address2 subnetmas* or interface
srctl stop $o+eapps -$ =$o+e.Csrctl stop $o+eapps -$ =$o+e2Csrctl mo+i! $o+eapps -$ =$o+e.C -A =ip_a++ressC%=$et mas>C%=i$ter!aceCsrctl mo+i! $o+eapps -$ =$o+e2C -A =ip_a++ressC%=$et mas>C%=i$ter!aceC
srctl start $o+eapps -$ =$o+e.Csrctl start $o+eapps -$ =$o+e2C
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 14/92
$ocate the oting dis*
crsctl Juer css ote+is>
Retrie ,.R 3,racle .luster Registr"4 information
ocrchec>
To preent a database starting at boot time
srctl +isa&le +ata&ase -+ =+ata&ase $ameC
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 15/92
.hange the priate interconnect subnet
3irst fin$ the interface which is in use as the interconnect + run as root from the crs home4oi!c! eti!
,ake a note of the interface name eth5 in the following e6ample then
run the following4oi!c! +eli! -lo&al eth.oi!c! seti! -lo&al eth.%=our $e5 su&$etC:cluster_i$terco$$ect
Administration ( 6ob Scheduler
• 2ist sche$ule$ 7o&s
• Su&mit a 7o&
• (emoe a 7o&
• (eset a &roken 7o&
• )$$ a program to the 50g sche$uler os program
•
)$$ a program to the 50g sche$uler store$ proce$ure• Sche$ule a 50g 7o&
$ist scheduled jobs
set li$es . paes 333col schema_user !ormat a.<col !ails !ormat 333select o&, schema_user, to_char(last_+ate, 'hh24:mi ++%mm%') last_ru$, to_char($eKt_+ate, 'hh24:mi ++%mm%') $eKt_ru$, !ailures !ails
, &ro>e$, su&str(5hat, ., .<) 5hat!rom +&a_o&sor+er & 4%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 16/92
Submit a job
+&ms_o&su&mit('=co+eC', =start timeC, =repeat timeC, G*F)P
3or e6ample4+eclare o&_i+ $um&erP&ei$+&ms_o&su&mit(
o&_i+, 'a$++ail_+ata_cle$se'
, tru$c(ss+ate9.)922%24, 'ss+ate9.', true)Pe$+P%
This will run a store$ proce$ure calle$ '$ail!8$ata8clense' each $a! at 50pm.
Remoe a job
ou nee$ to &e connecte$ as the user who owns the 7o&eKec +&ms_o&remoe(=o& $um&erC)P
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 17/92
Reset a bro*en job
ou nee$ to &e connecte$ as the user who owns the 7o&eKec +&ms_o&&ro>e$(=o& $um&erC, A6F)P
Add a program to the /0g scheduler 3os program4
&ei$+&ms_sche+ulercreate_proram(proram_$ame8C'ANDYo&_test',proram_actio$8C'%home%oracle%a$+&%o&_testsh',proram_tpe8C'FF;*A16F',comme$ts8C'test o&',e$a&le+8CG*F)Pe$+P%
Add a program to the /0g scheduler 3stored procedure4
&ei$+&ms_sche+ulercreate_proram(
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 18/92
proram_$ame8C'ANDYo&_test',proram_actio$8C'a$+o&_test',proram_tpe8C'OGFD_GO;FD*GF',comme$ts8C'test proram',e$a&le+8CG*F)Pe$+P%
Schedule a /0g job
&ei$+&ms_she+ulercreate_o&(o&_$ame8C'a$+a$+_o&_test',proram_$ame8C'a$+o&_test',start_+ate8C)
Administration ( Rec"cle bin
• #ispla! the contents of the rec!cle &in
• Empt! the rec!cle &in• #rop an o&7ect with out putting it in the rec!cle &in
Displa" the contents of the rec"cle bin
sho5 reccle&i$
Empt" the rec"cle bin
pure reccle&i$
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 19/92
Drop an object with out putting it in the rec"cle bin
+rop =o&ect_tpeC =o&ect_$ameC pureP
Structure ( Tablespace
• Ta&lespace usage
• Show the files that comprise a ta&lespace
• Ta&lespaces :;0< full
• =ser >uotas on all ta&lespaces
• 2ist all o&7ects in a ta&lespace
• Show all ta&lespaces use$ &! a user
• *reate a temporar! ta&lespace
• )lter a $ata&ases $efault temporar! ta&lespace
• Show segments that are approaching ma68e6tents
• 2ist the contents of the temporar! ta&lespaces
Tablespace usage
set paes 333col ta&lespace_$ame !ormat a4col "sie M1" !ormat 333,333,333col "!ree M1" !ormat 33,333,333col "? *se+" !ormat 333select tsuta&lespace_$ame, ceil(tsuuse+_m&) "sie M1", +eco+e(ceil(ts!!ree_m&), N*66,,ceil(ts!!ree_m&)) "!ree M1", +eco+e(. - ceil(ts!!ree_m&%tsuuse+_m&7.), N*66, ., . - ceil(ts!!ree_m&%tsuuse+_m&7.)) "? use+"!rom (select ta&lespace_$ame, sum(&tes)%.24%.24 use+_m&
!rom +&a_+ata_!iles roup & ta&lespace_$ame u$io$ all
select ta&lespace_$ame // ' 77FM77', sum(&tes)%.24%.24 use+_m&!rom +&a_temp_!iles roup & ta&lespace_$ame) tsu
, (select ta&lespace_$ame, sum(&tes)%.24%.24 !ree_m&!rom +&a_!ree_space roup & ta&lespace_$ame) ts!
5here tsuta&lespace_$ame 8 ts!ta&lespace_$ame (9)or+er & 4%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 20/92
Show the files that comprise a tablespace
set li$es .col !ile_$ame !ormat aSselect !ile_$ame, ceil(&tes % .24 % .24) "sie M1"!rom +&a_+ata_!iles5here ta&lespace_$ame li>e 'LNAMF'
%
Tablespaces 7809 full
set paes 333 li$es .col ta&lespace_$ame !ormat a4col "sie M1" !ormat 333333333col "? *se+" !ormat 333col "R?" !ormat 333333col "S<?" !ormat 333333select tsuta&lespace_$ame, ceil(tsuuse+_m&) "sie M1", . - !loor(ts!!ree_m&%tsuuse+_m&7.) "? *se+", (tsuuse+_m& % .) 7 (2 - !loor(ts!!ree_m&%tsuuse+_m&7.)) "R?", (tsuuse+_m& % .) 7 (2< - !loor(ts!!ree_m&%tsuuse+_m&7.)) "S<?"
!rom (select ta&lespace_$ame, sum(&tes)%.24%.24 use+_m& !rom +&a_+ata_!iles roup & ta&lespace_$ame) tsu, (select ta&lespace_$ame, sum(&tes)%.24%.24 !ree_m& !rom +&a_!ree_space roup & ta&lespace_$ame) ts!5here tsuta&lespace_$ame 8 ts!ta&lespace_$ame (9)a$+ . - !loor(ts!!ree_m&%tsuuse+_m&7.) C8 Ror+er & ,4,<%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 21/92
User #uotas on all tablespaces
col Juota !ormat a.select user$ame, ta&lespace_$ame, +eco+e(maK_&tes, -., 'u$limite+' , ceil(maK_&tes % .24 % .24) // 'M' ) "*OA"!rom +&a_ts_Juotas
5here ta&lespace_$ame $ot i$ ('FM')%
$ist all objects in a tablespace
set paes 333col o5$er !ormat a.<col seme$t_$ame !ormat a4col seme$t_tpe !ormat a2select o5$er, seme$t_$ame, seme$t_tpe!rom +&a_seme$ts5here lo5er(ta&lespace_$ame) li>e lo5er('?Lta&lespace?')or+er & o5$er, seme$t_$ame
%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 22/92
Show all tablespaces used b" a user
select ta&lespace_$ame, ceil(sum(&tes) % .24 % .24) "M1"!rom +&a_eKte$ts5here o5$er li>e 'Luser_i+'roup & ta&lespace_$ameor+er & ta&lespace_$ame
%
.reate a temporar" tablespace
create temporar ta&lespace temptemp!ile '=!ile_$ameC' sie <M%
Alter a databases default temporar" tablespace
alter +ata&ase +e!ault temporar ta&lespace temp%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 23/92
Show segments that are approaching ma:;e:tents
col seme$t_$ame !ormat a4select o5$er, seme$t_tpe, seme$t_$ame, maK_eKte$ts - eKte$ts as "spare", maK_eKte$ts
!rom +&a_seme$ts5here o5$er $ot i$ ('Y','YFM')a$+ (maK_eKte$ts - eKte$ts) = .or+er & 4%
To change ma6e6tentsalter =seme$t_tpeC =seme$t_$ameC storae(maKeKte$ts .<)P
$ist the contents of the temporar" tablespace3s4
set paes 333 li$es .col user$ame !ormat a.<col m& !ormat 333,333select suuser$ame, sessi+
, sesserial, suta&lespace, ceil((su&loc>s 7 +t&loc>_sie) % .4R<SE) M1!rom #sort_usae su, +&a_ta&lespaces +t, #sessio$ ses5here suta&lespace 8 +tta&lespace_$amea$+ susessio$_a++r 8 sessa++r%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 24/92
Structure ( ,bjects
• 3in$ an o&7ect
•
Inali$ o&7ects• Show the si-e of an o&7ect
• )ll o&7ects owne$ &! a user
• Source co$e of a proce$ure
• ?et an o&7ects $$l 9i onwar$s
• #ispla! compilation errors an$ warnings
• 3in$ all ta&les containing the specifie$ column
• 2ist all ta&les owne$ &! a user sorte$ &! si-e
-ind an object
set paes 333col o5$er !ormat a.<col o&ect_$ame !ormat a4col o&ect_tpe !ormat a2select o5$er, o&ect_$ame, o&ect_tpe!rom +&a_o&ects5here lo5er(o&ect_$ame) li>e lo5er('?Lo&ect?')or+er & o5$er, o&ect_tpe, o&ect_$ame%
Inalid objects
2ist inali$ o&7ects...set li$es 2 paes 333
col "o&" !ormat a4select o5$er // '' // o&ect_$ame "o&",o&ect_tpe!rom +&a_o&ects5here status 8 'INBA6ID'%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 25/92
(ecompile all inali$ o&7ects...TU%r+&ms%a+mi$%utlrpsJl
Show the si<e of an object
col seme$t_$ame !ormat a2select seme$t_$ame, &tes "IVF_1YF", ceil(&tes % .24 % .24) "IVF_M1"!rom +&a_seme$ts5here seme$t_$ame li>e 'Lo&_$ame'
%
All objects owned b" a user
col o&ect_$ame !ormat a4select o&ect_$ame, o&ect_tpe!rom +&a_o&ects5here o5$er 8 'Luser'or+er & o&ect_tpe, o&ect_$ame%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 26/92
Source code of a procedure
select teKt!rom +&a_source5here o5$er 8 'ANDY'a$+ $ame 8 'I6F_F'a$+ tpe 8 'A;WA0F 1ODY'or+er & li$e
%
=et an objects ddl 3>i onwards4
Note. Works for 9i an$ newer.
1arameters4 O@AE*T8T1E O@AE*T8N),E S*BE,)set paesie set lo$ 3select +&ms_meta+ataet_++l('A16F','A16F_A','ANDY') !rom +ualP
Displa" compilation errors and warnings
sho5 errors
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 27/92
sho5 errors ie5 =ei5_$ameCsho5 errors proce+ure =proc_$ameC
select 7 !rom +&a_errorsP
-ind all tables containing the specified column
set paes 333 li$es .col ta& !ormat aEcol colum$_$ame !ormat a2select o5$er // '' // ta&le_$ame as ta&
, colum$_$ame!rom +&a_ta&_colum$s5here colum$_$ame li>e upper('Lcol')%
$ist all tables owned b" a user sorted b" si<e
set li$es . paes 333col seme$t_$ame !ormat a4col m& !ormat 333,333,333select seme$t_$ame, ceil(sum(&tes) % .24 % .24) "M1"
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 28/92
!rom +&a_seme$ts5here o5$er li>e 'Luser'a$+ seme$t_tpe 8 'A16F'roup & seme$t_$ameor+er & ceil(sum(&tes) % .24 % .24) +esc%
Structure ( Inde:es
• In$e6 info &! ta&le
• Show in$e6e$ columns
Inde: info b" table
select ii$+eK_$ame, ita&lespace_$ame, ceil(s&tes % .4R<SE) "ie M1"
!rom +&a_i$+eKes i, +&a_seme$ts s5here ii$+eK_$ame 8 sseme$t_$amea$+ ta&le_$ame li>e 'Lta&le'or+er & 2, .
/
Show inde:ed columns
select colum$_$ame!rom +&a_i$+_colum$s5here i$+eK_$ame 8 'Li$+eK'or+er & colum$_positio$
%
Structure ( -iles
• #ispla! all $atafiles tempfiles an$ logfiles an$ their si-es
• Cuick $atafile health check
• Show $irectories that contain $atafiles
• 2ist autoe6tensi&le $atafiles
• Turn autoe6ten$ off for all $atafiles
• ,oe files &etween $isks
• 2ist controlfiles• *reate an OS comman$ for eer! file in the $ata&ase
• 3in$ $uplicate filenames
• 2ist files that are in hot+&ackup mo$e
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 29/92
Displa" all datafiles2 tempfiles and logfiles 3and their si<es4
set li$es . paes 333col $ame !ormat a<select $ame, &tes!rom (select$ame, &tes
!rom #+ata!ileu$io$ allselect $ame, &tes!rom #temp!ileu$io$ allselect l!mem&er "$ame", l&tes!rom #lo!ile l!, #lo l5here l!roup 8 lroupu$io$ allselect $ame, !rom #co$trol!ile) use+
, (select sum(&tes) as p!rom +&a_!ree_space) !ree
%
Quic* datafile health chec*
ou shoul$ onl! see online an$ s!stem ma!&e rea$+onl! tooselect +isti$ct status !rom #+ata!ile%
Show directories that contain datafiles
select +isti$ct su&str($ame, ., i$str($ame, '%', -.)) DIG!rom #+ata!ileor+er & .
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 30/92
%
$ist autoe:tensible datafiles
select !ile_$ame!rom +&a_+ata_!iles5here autoeKte$si&le 8 'YF'%
Turn autoe:tend off for all datafiles
select 'alter +ata&ase +ata!ile ''' // !ile_$ame // ''' autoeKte$+ o!!P'!rom +&a_+ata_!iles%
'oe files between dis*s
set trimspool o$ 5rap o!!set hea+i$ o!!set eri! o!!set paes . li$es .
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 31/92
spool re$amesJl
select 'alter +ata&ase re$ame !ile ''' //$ame // ''' to ''' //replace($ame // '''P', '%u.', '%u2')
!rom #+ata!ile%
select 'alter +ata&ase re$ame !ile ''' //mem&er // ''' to ''' //replace(mem&er // '''P', '%u.', '%u2')
!rom #lo!ile%
spool o!!
$ist controlfiles
select $ame!rom #co$trol!ile%
.reate an ,S command for eer" file in the database
The e6ample $eletes all filesselect 'rm ' // $ame!rom (select $ame
!rom #+ata!ileu$io$ allselect $ame!rom #temp!ileu$io$ allselect mem&er!rom #lo!ile
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 32/92
u$io$ all select $ame !rom #co$trol!ile
)%
-ind duplicate filenames
Searches for files with the same name in all $irectoriesselect cou$t(su&str($ame, i$str($ame, '%', -.) 9 ., 333)) "total", cou$t(+isti$ct su&str($ame, i$str($ame, '%', -.) 9 ., 333)) "+isti$ct"!rom #+ata!ile%
$ist files that are in hot(bac*up mode
set li$es . paes 333col $ame !ormat aEselect +!$ame, &status, to_char(time, 'hh24:mi:ss ++%mm%') time!rom #+ata!ile +!, #&ac>up &5here +!!ile 8 &!ile
a$+ &status 8 'A;IBF'or+er & &!ile
/
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 33/92
Structure ( Redo $ogs
• (emoe $rop
• a$$ logfile mem&ers
• 2ist mem&ers an$ si-es
Remoe drop
alter +ata&ase +rop lo!ile mem&er '=!ile$ameC'P
add logfile members
alter +ata&ase a++ lo!ile mem&er '=!ile$ameC' to roup =roupCP
$ist members and si<es
col mem&er !ormat aEcol "ie M1" !ormat 3,333,333
select l!mem&er, ceil(l&tes % .24 % .24) "ie M1"!rom #lo!ile l!, #lo l5here lroup 8 l!roupor+er & .%
Structure ( Undo
• *onerting from (oll&ack to =n$o
• #ispla! the roll&ack segments• )lter un$o retention
• What's in un$o
• Is an!thing rolling &ack at the momentD
Displa" the rollbac* segments
select seme$t_$ame, status!rom +&a_roll&ac>_ses%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 34/92
Alter undo retention
alter sstem set u$+o_rete$tio$8< scope8memorP
Note. This is in secon$s
?hat@s in undo
select ta&lespace_$ame, status, cou$t(7) as HO_MANY!rom +&a_u$+o_eKte$tsroup & ta&lespace_$ame, status%
Is an"thing rolling bac* at the moment!
2ook for the use$8u&lk alue $ecreasing. If it is the session connecte$ with it is rolling &ack.
When it reaches -ero roll&ack is complete.set li$es . paes 333col user$ame !ormat a.<
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 35/92
col comma$+ !ormat a2select sesuser$ame, su&str(sesproram, ., .3) comma$+, trause+_u&l>!rom #sessio$ ses, #tra$sactio$ tra5here sessa++r 8 trases_a++r%
Structure ( .onstraints
• Show all constraints on a ta&le
• 2ist ta&les that are using the specifie$ ta&le as a foreign ke!
• 1ro$uce a list of $isa&le$ fk constraints
• 1ro$uce ena&le statements all $isa&le$ fk constraints
• 2ist parent ta&les that ma! nee$ fi6ing/re+importing
• 2ist missing foriegn ke! alues
• Show all ta&le constraints for a user
Show all constraints on a table
col tpe !ormat a.col co$s_$ame !ormat aselect +eco+e(co$strai$t_tpe,
';', ';hec>','O', 'G%O Bie5','', 'rimar','G', 'orei$','*', '*$iJue','B', ';hec> ie5') tpe
, co$strai$t_$ame co$s_$ame, status, last_cha$e!rom +&a_co$strai$ts5here o5$er li>e 'Lo5$er'a$+ ta&le_$ame li>e 'Lta&le_$ame'or+er & .%
$ist tables that are using the specified table as a foreign *e"
set li$es . paes 333
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 36/92
select ao5$er, ata&le_$ame, aco$strai$t_$ame!rom +&a_co$strai$ts a, +&a_co$strai$ts &5here aco$strai$t_tpe 8 'G'a$+ ar_co$strai$t_$ame 8 &co$strai$t_$amea$+ ar_o5$er 8 &o5$era$+ &o5$er 8 'Lta&le_o5$er'a$+ &ta&le_$ame 8 'Lta&le_$ame'%
Same as a&oe &ut pro$uces '$isa&le constraint' statementsset li$es . paes 333col +isco$ !ormat a.select 'alter ta&le '//ao5$er//''//ata&le_$ame//' +isa&le co$strai$t'//aco$strai$t_$ame//'P' +isco$!rom +&a_co$strai$ts a, +&a_co$strai$ts &5here aco$strai$t_tpe 8 'G'a$+ ar_co$strai$t_$ame 8 &co$strai$t_$amea$+ ar_o5$er 8 &o5$era$+ &o5$er 8 'Lta&le_o5$er'
a$+ &ta&le_$ame 8 'Lta&le_$ame'%
&roduce a list of disabled f* constraints
set li$es . paes 333col ta&le !ormat aEcol co$strai$t_$ame !ormat aselect o5$er//''//ta&le_$ame "ta&le", co$strai$t_$ame!rom +&a_co$strai$ts5here status 8 'DIA16FD'
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 37/92
a$+ co$strai$t_tpe 8 'G'a$+ o5$er $ot i$ ('Y','YFM')or+er & .,2%
&roduce enable statements all disabled f* constraints
set li$es . paes 333select 'alter ta&le '//o5$er//''//ta&le_$ame//' e$a&le co$strai$t
'//co$strai$t_$ame//'P' "e$a&le"!rom +&a_co$strai$ts5here status 8 'DIA16FD'a$+ co$strai$t_tpe 8 'G'a$+ o5$er $ot i$ ('Y','YFM')or+er & .%
$ist parent tables that ma" need fi:ing)re(importing
select +isti$ct ro5$er // '' // rta&le_$ame "eKp"!rom +&a_co$strai$ts c, +&a_co$strai$ts r5here cstatus 8 'DIA16FD'a$+ cco$strai$t_tpe 8 'G'
a$+ cr_o5$er 8 ro5$era$+ cr_co$strai$t_$ame 8 rco$strai$t_$amea$+ co5$er $ot i$ ('Y','YFM')or+er & .%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 38/92
$ist missing foriegn *e" alues
Note. =seful for resoling O()+09;select 'select '//cccolum$_$ame- //' !rom '//co5$er//''//cta&le_$ame- //' a 5here $ot eKists (select ''K'' !rom '- //ro5$er//''//rta&le_$ame-
//' 5here '//rccolum$_$ame//' 8 a'//cccolum$_$ame//')'!rom +&a_co$strai$ts c, +&a_co$strai$ts r, +&a_co$s_colum$s cc, +&a_co$s_colum$s rc5here cco$strai$t_tpe 8 'G'a$+ co5$er $ot i$ ('Y','YFM')a$+ cr_o5$er 8 ro5$era$+ co5$er 8 cco5$era$+ ro5$er 8 rco5$era$+ cco$strai$t_$ame 8 ccco$strai$t_$amea$+ rco$strai$t_$ame 8 rcco$strai$t_$amea$+ cr_co$strai$t_$ame 8 rco$strai$t_$amea$+ ccpositio$ 8 rcpositio$
a$+ co5$er 8 'Lta&le_o5$er'a$+ cta&le_$ame 8 'Lta&le_$ame'a$+ cco$strai$t_$ame 8 'Lco$strai$t_$ame'or+er & co5$er, cta&le_$ame, cco$strai$t_$ame, ccpositio$%
Show all table constraints for a user
Note. This still nee$s some work...set li$es . paes 333&rea> o$ ta&le_$ame
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 39/92
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 40/92
$ist all materiali<ed iew logs
select lo_o5$er, lo_ta&le!rom +&a_mie5_los%
.reate a simple materiali<ed iew
create materialie+ ie5 a$+_mie5
re!resh X!ast / complete / !orcestart 5ith ss+ate$eKt ss+ate 9 .%245ith primar >eas select 7 !rom test_ta&le%
-ast " up$ate changes onl!
.omplete " wipe an$ repopulate the miew
-orce " fast if possi&le complete if not.
Show all materiali<ed and resfresh times
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 41/92
set li$es . paes 333col last_re!resh !ormat a2select o5$er, mie5_$ame, to_char(last_re!resh_+ate, '++%mm% hh24:mi') last_re!resh!rom +&a_mie5sor+er & o5$er, last_re!resh%
Show materiali<ed iew tables and masters
set li$es .col mie5 !ormat a4col master !ormat a4select o5$er // '' // $ame mie5, master_o5$er // '' // master master!rom +&a_mie5_re!resh_times%
Show refresh jobs in dba;jobs
This is useful for spotting failuresset li$es .col o& !ormat 3333
col lo_user !ormat a.<col last !ormat a.<col $eKt !ormat a.<col !ail !ormat 3333col 5hat !ormat a2select o&, lo_user, to_char(last_+ate, '++%mm% hh24:mi') last, to_char($eKt_+ate, '++%mm% hh24:mi') $eKt, !ailures !ail
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 42/92
, replace(5hat, '"') 5hat!rom +&a_o&s5here 5hat li>e '?+&ms_re!reshre!resh?'%
'anuall" start a refresh
eKecute +&ms_mie5re!resh ('=o5$erm_ta&leC')P
-orce a complete refresh
eKecute +&ms_mie5re!resh ('=o5$erm_ta&leC',';')P
Structure ( &artitions
• 2ist partitione$ ta&les
• 2ist a ta&les partitions
• Show partition si-es for the specifie$ ta&le
• ,oe a partition to a new ta&lespace
• )$$ a partition
• Split a partition
• #rop a partition• Truncate a partition
$ist partitioned tables
set paes 333 li$es .col ta&le_$ame !ormat a4select ta&le_$ame
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 43/92
, partitio$i$_tpe tpe, partitio$_cou$t partitio$s!rom +&a_part_ta&les5here o5$er 8 'Lo5$er'or+er & .%
$ist a tables partitions
set paes 333 li$es .col hih_alue !ormat a2col ta&lespace_$ame !ormat a2select partitio$_$ame, ta&lespace_$ame, hih_alue!rom +&a_ta&_partitio$s5here ta&le_o5$er 8 'Lo5$er'a$+ ta&le_$ame 8 'Lta&le_$ame'or+er & partitio$_positio$%
Show partition si<es for the specified table
set paes 333 li$es .
col ta&lespace_$ame !ormat a2col $um_ro5s !ormat 333,333,333select ppartitio$_$ame, pta&lespace_$ame, p$um_ro5s, ceil(s&tes % .24 % .24) m&!rom +&a_ta&_partitio$s p, +&a_seme$ts s5here pta&le_o5$er 8 so5$era$+ ppartitio$_$ame 8 spartitio$_$ame
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 44/92
a$+ pta&le_$ame 8 sseme$t_$amea$+ pta&le_o5$er 8 'Lo5$er'a$+ pta&le_$ame 8 'Lta&le_$ame'or+er & partitio$_positio$%
'oe a partition to a new tablespace
alter ta&le =ta&le_$ameC
moe partitio$ =partitio$_$ameCta&lespace =ta&lespace_$ameC$oloi$%
Add a partition
alter ta&le =ta&le_$ameCa++ partitio$ =partitio$_$ameC alues less tha$ (=alueC)ta&lespace =ta&lespace_$ameC%
or...alter ta&le =ta&le_$ameCa++ partitio$ =partitio$_$ameC alues (=alueC)
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 45/92
ta&lespace =ta&lespace_$ameC%
Split a partition
alter ta&le =ta&le_$ameCsplit partitio$ =partitio$_$ameC at (=alueC)i$to (partitio$ =partitio$_$ameC, partitio$ =partitio$_$ameC)up+ate lo&al i$+eKes
%
Drop a partition
alter ta&le =ta&le_$ameC +rop partitio$ =partitio$_$ameC%
Truncate a partition
alter ta&le =ta&le_$ameC tru$cate partitio$ =partitio$_$ameC%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 46/92
&erformance ( =eneral
• Show currentl! e6ectuing s>l
• Session statistics
• (esource intensie s>l
• 3ile io stats
• In session tracing• switch on eent 500FG
• (ows per &lock
• Show the &uffer cache a$isor!
Show currentl" e:ectuing s#l
select sJl_teKt!rom #sJlarea5here users_eKecuti$ C %
Session statistics
select s$$ame, stalue!rom #sesstat st, #stat$ame s$5here stAII; 8 s$AII;a$+ stBA6*F C a$+ stID 8 LIDor+er & alue +esc%
Resource intensie s#l
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 47/92
change ;59 to match &lock si-eselect sJl_teKt, eKecutio$s, to_char((((+is>_rea+s9&u!!er_ets)%eKecutio$s) 7 R.32)%.4R<SE,'3,333,333,33')
as total_ets_per_eKec_m&
, to_char((( +is>_rea+s %eKecutio$s) 7 R.32)%.4R<SE,'3,333,333,33') as +is>_rea+s_per_eKec_m&
, to_char((( &u!!er_ets %eKecutio$s) 7 R.32)%.4R<SE,'3,333,333,33')
as &u!!er_ets_per_eKec_m&, parsi$_user_i+!rom #sJlarea5here eKecutio$s C .or+er & E +esc%
-ile io stats
(e>uires time$8statistics"trueset li$es R paes 333
col !$ame hea+i$ "ile Name" !ormat aEcol siem& hea+i$ "ie(M&)" !ormat 33,333col phr+s hea+i$ "Gea+s" !ormat 333,333,333col rea+tim hea+i$ "ime" !ormat 33333col ph5rts hea+i$ "rites" !ormat 3,333,333col 5ritetim hea+i$ "ime" !ormat 33333select lo5er($ame) !$ame, (&tes % .4R<SE) siem&, phr+s, rea+tim, ph5rts, 5ritetim!rom #+ata!ile +!
, #!ilestat !s5here +!!ile 8 !s!ileor+er & .%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 48/92
In session tracing
To switch it on4eKec +&ms_sstemset_sJl_trace_i$_sessio$ (=si+C, =serialC, true)P
To switch it off4eKec +&ms_sstemset_sJl_trace_i$_sessio$ (=si+C, =serialC, !alse)P
switch on eent /00B
To switch it on4alter sessio$ set ee$ts '.4E trace $ame co$teKt !oreer, leel R'P
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 49/92
To switch it off4alter sessio$ set ee$ts '.4E trace $ame co$teKt o!!'P
Note. use tkprof to interpret the results.
Rows per bloc*
select a(ro5_cou$t) a, maK(ro5_cou$t) maK, mi$(ro5_cou$t) mi$!rom ( select cou$t(7) ro5_cou$t
!rom Lta&le_$ame roup & su&str(ro5i+, ., .<) )%
Show the buffer cache adisor"
Note. The current setting is halfwa! $own an$ has a rea$ factor of one.set li$es . paes 333col est_m& !ormat 33,333col est+_phsical_rea+s !ormat 333,333,333,333,333
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 50/92
select sie_!or_estimate est_m&, est+_phsical_rea+_!actor, est+_phsical_rea+s!rom #+&_cache_a+ice5here $ame 8 'DFA*6'or+er & sie_!or_estimate%
$&8cache8a$ice nee$s to &e on for the a&oe to work alter sstem set +&_cache_a+ice8o$P
&erformance ( $oc*s D'$
• Show sessions that are &locking each other
• Show locke$ o&7ects
• Show which row is locke$
• 2ist locks
Show sessions that are bloc*ing each other
select 'ID ' // l.si+ //' is &loc>i$ ' // l2si+ &loc>i$!rom #loc> l., #loc> l25here l.&loc> 8. a$+ l2reJuest C a$+ l.i+.8l2i+.a$+ l.i+28l2i+2%
Show loc*ed objects
set li$es . paes 333col user$ame !ormat a2col sess_i+ !ormat a.
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 51/92
col o&ect !ormat a2<col mo+e_hel+ !ormat a.select oracle_user$ame // ' (' // sosuser // ')' user$ame, ssi+ // ',' // sserial sess_i+, o5$er // '' //o&ect_$ame o&ect, o&ect_tpe, +eco+e( l&loc>
, , 'Not 1loc>i$', ., '1loc>i$', 2, '0lo&al') status
, +eco+e(loc>e+_mo+e, , 'No$e', ., 'Null', 2, 'Go5- ()', , 'Go5- ()', 4, 'hare', <, '%Go5- ()', E, 'FKclusie', O_;HAG(lmo+e)) mo+e_hel+
!rom #loc>e+_o&ect , +&a_o&ects +
, #loc> l, #sessio$ s5here o&ect_i+ 8 +o&ect_i+a$+ o&ect_i+ 8 li+.a$+ sessio$_i+ 8 ssi+or+er & oracle_user$ame, sessio$_i+%
Show which row is loc*ed
select +oo&ect_$ame, ro5_5ait_o&, ro5_5ait_!ile, ro5_5ait_&loc>, ro5_5ait_ro5, +&ms_ro5i+ro5i+_create (., GO_AI_O1@, GO_AI_I6F,
GO_AI_16O;W, GO_AI_GO)!rom #sessio$ s, +&a_o&ects +o5here si+8Lsi+a$+ sGO_AI_O1@ 8 +oO1@F;_ID%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 52/92
Then select the row with that rowi$...select 7 !rom =ta&leC 5here ro5i+8=ro5i+CP
$ist loc*s
colum$ loc>_tpe !ormat a.2colum$ mo+e_hel+ !ormat a.colum$ mo+e_reJueste+ !ormat a.colum$ &loc>i$_others !ormat a2colum$ user$ame !ormat a.F6F; sessio$_i+
, loc>_tpe, mo+e_hel+, mo+e_reJueste+, &loc>i$_others, loc>_i+.GOM +&a_loc> lHFGF loc>_tpe NO IN ('Me+ia Gecoer', 'Ge+o hrea+')%
&erformance ( $oc*s DD$
•
Show all $$l locks in the s!stem• Slightl! more simple ersion of the a&oe
• ?enerate kill statement for $$l locking sessions
Show all ddl loc*s in the s"stem
select +eco+e(lo&>lo&tp,, 'NF O1@F;', ., 'INDF', 2, 'A16F', , ';6*FG',4, 'BIF', <, 'YNONYM', E, 'F*FN;F',
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 53/92
S, 'GO;FD*GF', R, '*N;ION', 3, 'A;WA0F',.., 'A;WA0F 1ODY', .2, 'GI00FG',., 'YF', .4, 'YF 1ODY',.3, 'A16F AGIION', 2, 'INDF AGIION', 2., '6O1',22, '6I1GAGY', 2, 'DIGF;OGY', 24, '*F*F',2R, '@ABA O*G;F', 23, '@ABA ;6A', , '@ABA GFO*G;F',2, 'INDFYF', , 'OFGAOG',4, 'A16F *1AGIION', <, 'INDF *1AGIION',4, '6O1 AGIION', 4., '6O1 *1AGIION',42, 'MAFGIA6IVFD BIF',4, 'DIMFNION',44, ';ONF', 4E, 'G*6F F', 4S, 'GFO*G;F 6AN',4R, ';ON*MFG 0GO*',<., '*1;GIION', <2, '6O;AION',<<, 'M6 ;HFMA', <E, '@ABA DAA',<S, 'F;*GIY GOI6F', <3, 'G*6F',E2, 'FBA6*AION ;ONF','*NDFINFD'
) o&ect_tpe, lo&>l$ao& o&ect_$ame, p$>lp$mo+ loc>_mo+e_hel+
, p$>lp$reJ loc>_mo+e_reJueste+, sessi+, sesserial, sesuser$ame!rom #sessio$_5ait s5, K#>lo& lo&, K#>lp$ p$, #sessio$ ses5here s5ee$t 8 'li&rar cache loc>'a$+ s5p.ra5 8 lo&>lh+a+ra$+ lo&>lh+a+r 8 p$>lp$h+la$+ p$>lp$mo+ Q8 a$+ p$>lp$use 8 sessa++r%
Slightl" more simple ersion of the aboe
select sesuser$ame, ++lsessio$_i+, sesserial, o5$er // '' // ++l$ame o&ect, ++ltpe, ++lmo+e_hel+!rom +&a_++l_loc>s ++l, #sessio$ ses
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 54/92
5here o5$er li>e '?useri+?'a$+ ++lsessio$_i+ 8 sessi+%
=enerate *ill statement for ddl loc*ing sessions
select 'alter sstem >ill sessio$ ''' // ++lsessio$_i+ // ',' // sesserial// ''' imme+iateP'!rom +&a_++l_loc>s ++l
, #sessio$ ses5here o5$er li>e '?useri+?'a$+ ++lsessio$_i+ 8 sessi+%
&erformance ( ?aits
• Top ten hotest o&7ects &! touch count
• Waits &! file
• Segment Waits
•
Time waite$ for latches• I$entif! hot &locks
• 2ook at the performance stats for the instance
Top ten hotest objects b" touch count
col o5$er !ormat a2 tru$ccol o&ect_$ame!ormat acol touches !ormat 3,333,333select 7!rom (
select cou$t(7), sum(tch) O*;HF
, u$ame ONFG, o$ame O1@F;_NAMF!rom K#&h K, o&# o, user# u5here Ko& 8 oo&a$+ oo5$er 8 uuserroup & u$ame, o$ame
or+er & 2 +esc
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 55/92
)5here ro5$um = ..%
?aits b" file
col $ame !ormat aEselect $ame, cou$t
!rom K#>c&!5ait, #+ata!ile5here i$+K 9 . 8 !ileor+er & 2%
Segment ?aits
select o&ect_$ame, o&, statistic_$ame, alue!rom #seme$t_statistics5here o5$er li>e 'Lo5$er'a$+ statistic_$ame li>e '?5aits?'
a$+ alue C or+er & statistic_$ame, alue +esc%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 56/92
Time waited for latches
col ee$t !ormat aselect ee$t, time_5aite+, rou$+(time_5aite+7.% *M (time_5aite+) OBFG(),2) 5ait_pct!rom (
select ee$t
, time_5aite+!rom #sstem_ee$t5here ee$t $ot i$ (
'Null ee$t','clie$t messae','r+&ms ipc repl','smo$ timer','r+&ms ipc messae',' I+le ait','6%6 loc> timer','!ile ope$','pmo$ timer','MON oes to sleep'
,'irtual circuit status','+ispatcher timer','67Net messae !rom clie$t','parallel Juer +eJueue 5ait','pipe et')
u$io$(select $ame, alue!rom #ssstat5here $ame li>e ';* use+ 5he$ call starte+')
)or+er & 2 +esc%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 57/92
Identif" hot bloc*s
2ook at all waits for the instancecol ee$t !ormat aselect ee$t, total_5aits, time_5aite+
!rom #sstem_ee$t5here ee$t li>e '?5ait?'or+er & 2,%
If there are lots of '$ata &lock' waits get a &reak+$own of themselect 7!rom #5aitstat%
Then run this to i$entif! the file &lock an$ reason co$e...select p. "ile ", p2 "1loc> ", p "Geaso$ ;o+e"!rom #sessio$_5ait5here ee$t 8 '&u!!er &us 5aits'%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 58/92
Note. ou might nee$ to run this a few times &efore an!thing is $ispla!e$.
$oo* at the performance stats for the instance
select $$ame, salue!rom #stat$ame $, #ssstat s
5here $statistic 8 sstatisticor+er & $class, $$ame%
&erformance ( Statistics
• ?ather $ata&ase statistics
• ?ather stats for a single schema...
• #elete stats
• ?ather s!stem stats
• E6port/Import optimi-er statistics• Ol$ st!le anal!-e
• Show a count of anal!e-e$ ta&les &roken $own &! schema
• Show ta&les that hae anal!-e $ates ol$er than to$a!
=ather database statistics
?ather stats on the entire $ata&ase...eKecute +&ms_statsather_+ata&ase_statsP
Or...
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 59/92
eKecute +&ms_statsather_+ata&ase_stats( -estimate_perce$t 8C ., -metho+_opt 8C 'OG A66 ;O6*MN IVF .',-casca+e 8C G*F)P
=ather stats for a single schema+++
eKecute +&ms_statsather_schema_stats(';O')P
Or...eKecute +&ms_statsather_schema_stats( -o5$$ame 8C ';O', -
estimate_perce$t 8C ., -metho+_opt 8C 'OG A66 ;O6*MN IVF .',-casca+e 8C G*F)P
ou can let oracle come up with the estimate figure &! using $&ms8stats.auto8sample8si-e
or...eKecute +&ms_statsather_schema_stats( -o5$$ame 8C 'Y', -casca+e 8C G*F)P
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 60/92
Ta&le statisticseKec +&ms_statsather_ta&le_stats('=o5$erC', '=ta&le_$ameC')P
Delete stats
eKec +&ms_stats+elete_+ata&ase_statsP
eKec +&ms_stats+elete_schema_stats(';O')P
eKec +&ms_stats+elete_ta&le_stats(';O', 'FM6OYFF')P
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 61/92
eKec +&ms_stats+elete_i$+eK_stats(';O', 'FM6OYFF_W')P
=ather s"stem stats
eKecute +&ms_statsather_sstem_stats('tart')P
Wait for a while + i$eal! with the $ata&ase un$er a t!pical workloa$eKecute +&ms_statsather_sstem_stats('top')P
To see the current s!stem statisticsselect p$ame, pal.!rom ssauK_stats#
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 62/92
5here s$ame 8 'YA_MAIN'%
E:port)Import optimi<er statistics
*reate a ta&le to hol$ the statistics...eKec +&ms_statscreate_stat_ta&le(o5$$ame 8C 'Y',-statta& 8C 'pro+_stats', t&lspace 8C '*FG')P
1opulate the ta&le with the current stats...eKec +&ms_statseKport_schema_stats(o5$$ame 8C ';O',-stato5$8C'Y', statta&8C'pro+_stats')P
)t this point !ou nee$ to45 take an e6port of the 'pro$8stats' ta&le
import 'pro$8stats' into the target $ata&aseH loa$ the statistics using this comman$...eKec +&ms_statsimport_schema_stats(o5$$ame 8C ';O',-stato5$8C'Y', statta&8C'pro+_stats')P
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 63/92
,ld st"le anal"<e
a$ale ta&le emploees compute statisticsP
a$ale ta&le emploees estimate statistics sample . ro5sP
a$ale ta&le emploees estimate statistics sample .< perce$tP
a$ale i$+eK emploees_i$+ compute statisticsP
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 64/92
Show a count of anal"e<ed tables bro*en down b" schema
set paes 333 li$es .select ao5$er, atotal_ta&les ta&les, $l(&a$ale+_ta&les,) a$ale+!rom (select o5$er
, cou$t(7) total_ta&les
!rom +&a_ta&lesroup & o5$er) a
, (select o5$er, cou$t(last_a$ale+) a$ale+_ta&les!rom +&a_ta&les5here last_a$ale+ is $ot $ullroup & o5$er) &
5here ao5$er 8 &o5$er (9)a$+ ao5$er $ot i$ ('Y', 'YFM')or+er & atotal_ta&les - $l(&a$ale+_ta&les,) +esc%
Show tables that hae anal"<e dates older than toda"
This is useful if !ou are running an anal!-e an$ want to see how much is left to $o
select cou$t(last_a$ale+) le!t_to_+o!rom +&a_ta&les5here o5$er 8 'Lschema'a$+ tru$c(last_a$ale+) = tru$c(ss+ate)or+er & .%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 65/92
&erformance ( Quer" Tuning
• *reate a plan ta&le
• )utotrace
•
E6plain plan• 3in$ a >uer!'s hash
• ?ra& the s>l associate$ with a hash
• 2ook at a >uer!'s stats in the s>l area
.reate a plan table
TU%r+&ms%a+mi$%utlKpla$sJl
Autotrace
To switch it on4colum$ pla$_plus_eKp !ormat a.
set autotrace o$ eKplai$ Displas the eKecutio$ pla$ o$lset autotrace traceo$l eKplai$ +o$t ru$ the Juerset autotrace o$ ho5s the eKecutio$ pla$ as 5ell as statistics o! thestateme$tset autotrace o$ statistics Displas the statistics o$lset autotrace traceo$l Displas the eKecutio$ pla$ a$+ the statistics
To switch it off4set autotrace o!!
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 66/92
E:plain plan
eKplai$ pla$ !orselect
or...eKplai$ pla$ set stateme$t_i+ 8 '&a+.' !orselect
Then to see the output...set li$es . paes 333TU%r+&ms%a+mi$%utlKpls
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 67/92
-ind a #uer"@s hash
1ut something uni>ue in the like clauseselect hash_alue, sJl_teKt!rom #sJlarea5here sJl_teKt li>e '?IMIN06INW?O6DFGGF?'
%
=rab the s#l associated with a hash
select sJl_teKt!rom #sJlarea5here hash_alue 8 'Lhash'%
$oo* at a #uer"@s stats in the s#l area
select eKecutio$s, cpu_time, +is>_rea+s, &u!!er_ets, ro5s_processe+, &u!!er_ets % eKecutio$s!rom #sJlarea
5here hash_alue 8 'Lhash'%
&erformance ( 'emor"
• S?) &reak$own
• 1?) usage &! username
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 68/92
• #ispla! pool usage
S=A brea*down
set li$es . paes 333col &tes !ormat 333,333,333
compute sum o! &tes o$ pool&rea> o$ pool s>ip .select pool, $ame, &tes!rom #sastator+er & pool, $ame%
&=A usage b" username
select stsi+ "ID",s$$ame "YF",ceil(stalue % .24 % .24) "M1"
!rom #sesstat st,#stat$ame s$5here ststatistic 8 s$statistica$+ si+ i$ (select si+
!rom #sessio$5here user$ame li>e 'Luser')
a$+ upper(s$$ame) li>e '?0A?'or+er & stsi+,
stalue +esc
/
Displa" pool usage
select $ame, sum(&tes)!rom #sastat5here pool li>e 'share+ pool'roup & $ame%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 69/92
&erformance ( Statspac*
• Take a snapshot
• #elete one or more snapshots
• ?enerate a report
• 2ist snapshots
• Install statspack• =ninstall statspack
• Sche$ule an$ hourl! snapshot
Ta*e a snapshot
eKec statspac>s$apP
Or to specif! a leel...eKec statspac>s$ap(i_s$ap_leel 8C E, i_mo+i!_parameter 8C 'true')P
2eel 0 + This leel captures general statistics inclu$ing roll&ack segment row cache S?)
s!stem eents &ackgroun$ eents session eents s!stem statistics wait statistics lock statistics
an$ 2atch information.
2eel + This leel inclu$es capturing high resource usage SC2 Statements along with all $ata
capture$ &! lower leels.
2eel G + This leel inclu$es capturing SC2 plan an$ SC2 plan usage information for high
resource usage SC2 Statements along with all $ata capture$ &! lower leels.
2eel J + This leel captures segment leel statistics inclu$ing logical an$ ph!sical rea$s row
lock itl an$ &uffer &us! waits along with all $ata capture$ &! lower leels.
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 70/92
2eel 50 + This leel inclu$es capturing *hil$ 2atch statistics along with all $ata capture$ &!
lower leels.
Delete one or more snapshots
TU%r+&ms%a+mi$%sppureP
=enerate a report
TU%r+&ms%a+mi$%spreportsJl
$ist snapshots
col "Date%ime" !ormat aselect s$ap_i+, s$ap_leel, to_char(s$ap_time,'HH24:MI: DD-MM-YYYY') "Date%ime"!rom stats#s$apshot, #+ata&aseor+er & s$ap_i+%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 71/92
Install statspac*
5. *reate a ta&lespace minimum si-e 500,@
. (un...TU%r+&ms%a+mi$%spcreate
Uninstall statspac*
TU%r+&ms%a+mi$%sp+rop
Schedule and hourl" snapshot
TU%r+&ms%a+mi$%spautosJl
Note. This uses $&ms87o& so 7o&8>ueue8processes nee$s to &e set greater than 0.
To see the 7o&4select o&, 5hat!rom +&a_o&s%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 72/92
To $elete the 7o&4eKec +&ms_o&remoe(=o& $um&erC)P
&erformance ( A?R
• #ispla! a list of snapshots
• 1ro$uce a report
• To see the snapshot interal an$ retention perio$
• *hange the snapshot interal
• *hange the retention perio$
• ,anuall! take a snapshot
• 2ist all &aselines
• *reate a &aseline
• (emoe a &aseline
• Ena&le/#isa&le automatic snapshots
• Time mo$el >ueries
• 1ro$uce an )ctie Session Bistor! )SB report
Displa" a list of snapshots
set li$es . paes 333select s$ap_i+, s$ap_leel, to_char(&ei$_i$teral_time, '++%mm% hh24:mi:ss') &ei$!rom +&a_hist_s$apshotor+er & .%
&roduce a report
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 73/92
TU%r+&ms%a+mi$%a5rrptsJl
To see the snapshot interal and retention period
col s$ap_i$teral !ormat acol rete$tio$ !ormat aselect s$ap_i$teral, rete$tio$!rom +&a_hist_5r_co$trol
%
.hange the snapshot interal
Note. This e6ample changes it to H0 minuteseKec +&ms_5or>loa+_repositormo+i!_s$apshot_setti$s (i$teral 8C )
.hange the retention period
Note. This e6ample changes it to two weeks 5F $a!seKec +&ms_5or>loa+_repositormo+i!_s$apshot_setti$s (rete$tio$ 8C .47247E)
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 74/92
'anuall" ta*e a snapshot
eKec +&ms_5or>loa+_repositorcreate_s$apshot
$ist all baselines
set li$es .col &aseli$e_$ame !ormat a4select &aseli$e_i+, &aseli$e_$ame
, start_s$ap_i+, e$+_s$ap_i+!rom +&a_hist_&aseli$eor+er & .%
.reate a baseline
eKec +&ms_5or>loa+_repositorcreate_&aseli$e (=start s$apC,=e$+s$apC,'=$ameC')
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 75/92
Remoe a baseline
eKec +&ms_5or>loa+_repositor+rop_&aseli$e('=&aseli$e $ameC')
Enable)Disable automatic snapshots
Note. This 7o& is ena&le$ &! $efaulteKec +&ms_sche+ulere$a&le('0AHFG_A_@O1')
an$ to $isa&le...eKec +&ms_sche+uler+isa&le('0AHFG_A_@O1')
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 76/92
Time model #ueries
S!stem time mo$elset li$es . paes 333select stat_$ame, alue
!rom #ss_time_mo+elor+er & alue +esc%
Session time mo$elset li$es . paes 333select stat_$ame, alue!rom #sess_time_mo+el5here si+ 8 'Lsi+'or+er & alue +esc%
&roduce an Actie Session Histor" 3ASH4 report
TU%r+&ms%a+mi$%ashrptsJl
Cac*up ( Data=uard
• Startup comman$s
• To remoe a $ela! from a stan$&!
• *ancel manage$ recoer!
• (egister a missing log file
• If 3)2 $oesn't work an$ it sa!s the log is alrea$! registere$
• *heck which logs are missing
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 77/92
• #isa&le/Ena&le archie log $estinations
• Turn on fal tracing on the primar! $&
• Stop the #ata ?uar$ &roker
• Show the current instance role
• 2ogical stan$&! appl! stop/start
•
See how up to $ate a ph!sical stan$&! is• #ispla! info a&out all log $estinations
• #ispla! log $estinations options
• 2ist an! stan$&! re$o logs
Startup commands
startup $omou$talter +ata&ase mou$t sta$+& +ata&asePalter +ata&ase recoer ma$ae+ sta$+& +ata&ase +isco$$ectP
To remoe a dela" from a standb"
alter +ata&ase recoer ma$ae+ sta$+& +ata&ase ca$celP
alter +ata&ase recoer ma$ae+ sta$+& +ata&ase $o+ela +isco$$ectP
.ancel managed recoer"
alter +ata&ase recoer ma$ae+ sta$+& +ata&ase ca$celP
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 78/92
Register a missing log file
alter +ata&ase reister phsical lo!ile '=!ullpath%!ile$ameC'P
If -A$ doesn@t wor* and it sa"s the log is alread" registered
alter +ata&ase reister or replace phsical lo!ile '=!ullpath%!ile$ameC'P
If that $oesn't work tr! this...shut+o5$ imme+iatestartup $omou$talter +ata&ase mou$t sta$+& +ata&asePalter +ata&ase recoer automatic sta$+& +ata&aseP
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 79/92
wait for the recoer! to finish + then cancelshut+o5$ imme+iatestartup $omou$talter +ata&ase mou$t sta$+& +ata&asePalter +ata&ase recoer ma$ae+ sta$+& +ata&ase +isco$$ectP
.hec* which logs are missing
(un this on the stan$&!...select localthrea+, localseJue$ce !rom
(select threa+ , seJue$ce
!rom #archie+_lo5here +est_i+8.) local
5here localseJue$ce $ot i$(select seJue$ce
!rom #archie+_lo 5here +est_i+82 a$+
threa+ 8 localthrea+)%
Disable)Enable archie log destinations
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 80/92
alter sstem set lo_archie_+est_state_2 8 '+e!er'Palter sstem set lo_archie_+est_state_2 8 'e$a&le'P
Turn on fal tracing on the primar" db
alter sstem set 6O0_AG;HIBF_GA;F 8 .2RP
Stop the Data =uard bro*er
alter sstem set +_&ro>er_start8!alse%
Show the current instance role
select +ata&ase_role!rom #+ata&ase%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 81/92
$ogical standb" appl" stop)start
Stop...alter +ata&ase stop loical sta$+& applP
Start...alter +ata&ase start loical sta$+& applP
See how up to date a ph"sical standb" is
(un this on the primar!set $um5i+th .<select maK(seJue$ce) curre$t_seJ!rom #lo%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 82/92
Then run this on the stan$&!set $um5i+th .<select maK(applie+_seJ) last_seJ!rom #archie_+est_status%
Displa" info about all log destinations
To &e run on the primar!set li$es .set $um5i+th .<
colum$ ID !ormat 33colum$ "G6s" !ormat 33colum$ actie !ormat 33col tpe !ormat a4
select +s+est_i+ i+, a+status, +s+ata&ase_mo+e +&_mo+e, a+archier tpe, +srecoer_mo+e, +sprotectio$_mo+e, +ssta$+&_lo!ile_cou$t "G6s", +ssta$+&_lo!ile_actie actie
, +sarchie+_seJ!rom #archie_+est_status +s, #archie_+est a+5here +s+est_i+ 8 a++est_i+a$+ a+status Q8 'INA;IBF'or+er &
+s+est_i+%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 83/92
Displa" log destinations options
To &e run on the primar!set $um5i+th R li$es .colum$ i+ !ormat 33select +est_i+ i+, archier
, tra$smit_mo+e, a!!irm, as$c_&loc>s as$c, $et_timeout $et_time, +ela_mi$s +ela, reope$_secs reope$, reister,&i$+i$!rom #archie_+estor+er &
+est_i+%
$ist an" standb" redo logs
set li$es . paes 333col mem&er !ormat aS
select stroup, stseJue$ce, ceil(st&tes % .4R<SE) m&, l!mem&er!rom #sta$+&_lo st, #lo!ile l!5here stroup 8 l!roup%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 84/92
Cac*up ( -lashbac*
• To put the session &ack in time
• *op! ol$ $ata into a new ta&le
• E6port a $ata&ase from a point in time• 3in$ the current S*N num&er
• Set the $ata&ase recoer! $irector! an$ si-e
• Set the flash&ack $ata&ase retention target
• Turn on flash&ack $ata&ase
• What is the earliest time the $ata&ase can &e flashe$ &ack toD
• ?et a list of times an$ scn num&ers
• 3lash the $ata&ase &ack
To put the session bac* in time
Note. un$o8management must &e auto Note. Take a look at un$o8retention to get an i$ea of how far &ack !ou might &e a&le to go.
Note. This can't &e run as s!s + s!stem works thougheKec +&ms_!lash&ac>e$a&le_at_time(to_+ate('2E-A*0-24 .2::', 'YYYY-MON-DDHH24:MI:'))P
To $isa&le flash&ack run...eKec +&ms_!lash&ac>+isa&leP
.op" old data into a new table
3irst create an empt! cop! of the source ta&le...create ta&le ol+_test_ta&leta&lespace a$+
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 85/92
as select 7!rom test_ta&le5here ro5$um = .%
Now insert the ol$ $ata into it...i$sert %79 AFND 7% ol+_test_ta&le
(select 7 !rom test_ta&le as o! timestampto_timestamp('24-A*0-E .2::','DD-MON-YY HH24:MI:'))
%
E:port a database from a point in time
eKp % !ile8!ull_scr3+mp lo8!ull_scr3lo !lash&ac>_time8O_IMFAM('.-3-2E 3::', 'DD-MM-YYYY HH24:MI:')
eKp % par!ile8!ull_scr3par
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 86/92
parfile...useri+8sstem%777777!ile8!ull_scr3+mplo8!ull_scr3lo!lash&ac>_time8'2E-3-. .2::'
-ind the current S.% number
select +&ms_!lash&ac>et_sstem_cha$e_$um&er !rom +ual%
Set the database recoer" director" and si<e
These settings are $!namicalter sstem set +&_recoer_!ile_+est8'=pathC' scope8&oth%alter sstem set +&_recoer_!ile_+est_sie8=sieC scope8&oth%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 87/92
Set the flashbac* database retention target
alter sstem set +&_!lash&ac>_rete$tio$_taret8=mi$utesC scope8&oth%
Turn on flashbac* database
shut+o5$ imme+iatestartup mou$talter +ata&ase !lash&ac> o$P
alter +ata&ase ope$P
?hat is the earliest time the database can be flashed bac* to!
select ol+est_!lash&ac>_sc$ sc$, to_char(ol+est_!lash&ac>_time, 'hh24:mi:ss ++%mm%') ol+est_time!rom #!lash&ac>_+ata&ase_lo%
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 88/92
=et a list of times and scn numbers
set paes 333select sc$, to_char(time_+p, 'hh24:mi:ss ++%mm%') time!rom smo$_sc$_timeor+er & sc$%
-lash the database bac*
shut+o5$ imme+iatestartup mou$t eKclusie
!lash&ac> +ata&ase to sc$ =sc$CP
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 89/92
or...!lash&ac> +ata&ase to timestamp to_timestamp('22%4%2S .3::','++%mm%hh24:mi:ss')P
alter +ata&ase ope$ resetlosP
'anual upgrade from >i to /0g Release 1
This proce$ure $escri&es the steps necessar! to manuall! upgra$e a $ata&ase from 9..6 to 50..6.
It assumes that !ou hae alrea$! installe$ 50g onto the serer. Of course it is essential that !outake a &ackup of !our $ata&ase &efore attempting an! upgra$e.
• 5. *ompile an! inali$ o&7ects
TU%r+&ms%a+mi$%utlrpsJl
• . *reate a s!sau6 ta&lespace
Baing a s!sau6 ta&lespace is a re>uirement in 50g. So if !ou haen't alrea$! got one create one
now.create ta&lespace ssauK+ata!ile '=!ile_$ameC' sie <.2M
eKte$t ma$aeme$t localseme$t space ma$aeme$t auto%
• H. (un utlu50i.s>l
This utilit! script checks that the $ata&ase is rea$! to &e upgra$e$ to 50g. It also i$entifies an!
actions that nee$ to &e taken. The script is locate$ in the 50g oracle home so !ou will nee$ to
specif! the full path to it.
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 90/92
T%u.%app%oracle%pro+uct%.2%+&_.%r+&ms%a+mi$%utlu.2isJl
(eiew the output an$ make an! necessar! alterations. ,ake a note of how man! inali$ o&7ects
there are.
• F. Shut the $ata&ase $own with either normal or imme$iate
shut+o5$ imme+iate
• . *op! the spfile or pfile an$ the passwor$ file from the e6isting home to the 50g one.
cp #ZOGA;6F_HOMF[%+&s%7#ZOGA;6F_ID[7 =$e5_homeC%+&s%
• G. E$it orata&
)lter /etc/orata& or /ar/opt/oracle/orata& to point to the50g home. Once $one rerun oraen to
&ring the alteration into effect.
• J. =pgra$e the $ata&ase
sJlplus "% as ss+&a"
startup upra+e
This ne6t &it is the upgra$e itself. It takes roughl! half an hour to complete. Spool the output to a
file so that !ou can reiew it afterwar$.TU%r+&ms%a+mi$%catupr+sJl
• ;. (ecompile an! inali$ o&7ects
TU%r+&ms%a+mi$%utlrpsJl
*ompare the num&er of inali$ o&7ects with the num&er note$ in step H. It shoul$ hopefull! &e the
same or less.
• 9. Then check the status of the upgra$e
TU%r+&ms%a+mi$%utlu.2ssJl
• 50. )lter or remoe initialisation parameters
Temporaril! creating a pfile is the easiest wa!.create p!ile !rom sp!ileP
shut+o5$ imme+iate
i #ZOGA;6F_HOMF[%+&s%i$it#ZOGA;6F_ID[ora
)lter/remoe parameters i$entifie$ in step 9. Set compati&le to 50..0.0.0
startup
create sp!ile !rom p!ileP
shut+o5$ imme+iate
startuphat's itQ
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 91/92
8/12/2019 Important Oracle Query Script[1] (1)
http://slidepdf.com/reader/full/important-oracle-query-script1-1 92/92
This one will $ispla! the hash an$ the SC2 itself4
set paes 333 li$es .&rea> o$ hash_alueselect hash_alue, sJl_teKt
!rom #sJlteKt5here hash_alue i$ (select a!thash_alue!rom sJl_eKec_&e!ore &e!, sJl_eKec_a!ter a!t5here a!teKecutio$s C &e!eKecutio$sa$+ a!thash_alue 8 &e!hash_alue (9)
)or+er &hash_alue
, piece%
• . Ti$! up
#on't forget to remoe the snapshot ta&les once !ou'e finishe$4• +rop ta&le sJl_eKec_&e!ore
• %
•
• +rop ta&le sJl_eKec_a!ter
• %