oracle sql tricks
Post on 20-Apr-2015
117 Views
Preview:
TRANSCRIPT
1/17/12 Oracle SQL tricks – initora
1/14initora.wordpress.com/article/oracle-sql-tricks-232y3pcqwxodx-2/
2U DF OH�6 4 / �WU L F N V
$ �2U DF OH�/ HDU Q L Q J�'DWDEDV H
May 20, 2010
New Updates on Oracle Tricks
NEW UPDATES: SCROLL BELOW FOR NEW SQL TRICKS
UPDATED NEWS: 8 NEW KNOLS (APRIL-MAY 2010)
:K D W �L V �$X W R P D W L F �'D W D E D V H �'L D J Q R V W L F �0R Q L W R U ��$''0��L Q �2U D F O H �'D W D E D V H ���J "
http://knol.google.com/k/mayur-v/what-is-automatic-database-diagnostic/232y3pcqwxodx/62#
Oracle applications scripts – With Example
http://knol.google.com/k/mayur-v/oracle-applications-scripts-with-example/232y3pcqwxodx/46#
Oracle SQL Rewriting – Faster Performance
http://knol.google.com/k/mayur-v/oracle-sql-rewriting-faster-performance/232y3pcqwxodx/45#
LEARN JAVAFX & ANIMATION WITH EXAMPLE – A STEP BY STEP GUIDE
http://knol.google.com/k/mayur-v/learn-javafx-animation-with-example-a/232y3pcqwxodx/44#
:K D W ¶ V �Q H Z �L Q �2U D F O H ���*�5�"
http://knol.google.com/k/mayur-vora/what-s-new-in-oracle-11g-r2/232y3pcqwxodx/40#
What can you do with your Oracle Database?
http://knol.google.com/k/mayur-vora/what-can-you-do-with-your-oracle/232y3pcqwxodx/33#
Oracle Performance Tuning Interview Q & A
http://knol.google.com/k/mayur-vora/oracle-performance-tuning-interview-q-a/232y3pcqwxodx/34#
LEARN ORACLE OEM AND ALL STUFFS & TRICKS
$ EVW U DFW
$ X W KR U V
V Mayur
1/17/12 Oracle SQL tricks – initora
2/14initora.wordpress.com/article/oracle-sql-tricks-232y3pcqwxodx-2/
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#
Easy way to install Oracle 10gR2 on Linus CentOS Step by Step:
http://knol.google.com/k/easy-way-to-install-oracle-database-10g-release-2-on-linux-centos-step-by-
step#
New Updates: SQL Tricks tested on Oracle DB:
——————————————————————-
Tuning Oracle with hidden parameters
2U D F O H �K D V �Q X P H U R X V �³ K L G G H Q ´ �S D U D P H W H U V �W K D W �D U H �X V H G �W R �F K D Q J H �W K H �L Q W H U Q D O
E H K D Y L R U �R I �2U D F O H ���$V �\ R X �P D \ �N Q R Z ��D O O �K L G G H Q �S D U D P H W H U V �E H J L Q �Z L W K �D Q
X Q G H U V F R U H �F K D U D F W H U �
2I I L F L D O O \ ��2U D F O H �V W D W H V �W K D W �W K H �K L G G H Q �S D U D P H W H U �V K R X O G �Q H Y H U �E H �F K D Q J H G
X Q O H V V �G L U H F W H G �E \ �2U D F O H �W H F K Q L F D O �V X S S R U W ��E X W �P R V W �H [ S H U W �2U D F O H �'%$¶ V �Z L O O
F R P P R Q O \ �D G M X V W �W K H V H �S D U D P H W H U V �W R �L P S U R Y H �S H U I R U P D Q F H �
7K H V H �K L G G H Q �S D U D P H W H U V �D U H �X V H G �E \ �2U D F O H �W R �L Q G L Y L G X D O L ] H �W K H �L Q W H U Q D O
E H K D Y L R U �R I �W K H �P H F K D Q L V P V �I R U �6*$�P H P R U \ �P D Q D J H P H Q W ��R E M H F W
P D Q D J H P H Q W ��D Q G �K X Q G U H G �R I �R W K H U �L Q W H U Q D O �P H F K D Q L V P V �
0D Q \ �V D Y Y \ �2U D F O H �S U R I H V V L R Q D O V �F R P P R Q O \ �D G M X V W �W K H �K L G G H Q �S D U D P H W H U V �W R
L P S U R Y H �W K H �R Y H U D O O �S H U I R U P D Q F H �R I �W K H L U �V \ V W H P V ���+R Z H Y H U ��E H F D X V H �W K H V H �D U H
³ X Q G R F X P H Q W H G ´ �S D U D P H W H U V ��P R V W �2U D F O H �S U R I H V V L R Q D O V �U H O \ �R Q �S X E O L F D W L R Q V
V X F K �D V �2U D F O H �,Q W H U Q D O V �W R �J H W �L Q V L J K W V �L Q W R �W K H �S U R S H U �V H W W L Q J �I R U �W K H �K L G G H Q
S D U D P H W H U V �
)R U �H [ D P S O H ��Z K H Q H Y H U �L Q G H [ �F R Q W H Q W L R Q �L V �H [ S H U L H Q F H G ��D V �H Y L G H Q F H G �E \
S U R F H V V �Z D L W V ���D G M X V W L Q J �W K H �I R O O R Z L Q J �S D U D P H W H U V �P D \ �E H �K H O S I X O �
B G E B E O R F N B K D V K B E X F N H W V �± �'H I D X O W V �W R ���[ �G E B E O R F N B E X I I H U V �E X W �V K R X O G �E H
W K H �Q H D U H V W �S U L P H �Q X P E H U �W R �W K H �Y D O X H �R I ��[ �G E B E O R F N B E X I I H U V �
B G E B E O R F N B K D V K B O D W F K H V �± �'H I D X O W V �W R ������E X W �������L V �D �E H W W H U �Y D O X H �
B N J O B O D W F K B F R X Q W �± �'H I D X O W V �W R �] H U R �Z K L F K �L V �P H D Q V ���Q X P E H U �R I �&38V ��/R F N
F R Q W H Q W L R Q �F D Q �R I W H Q �E H �U H G X F H G �E \ �U H �V H W W L Q J �W K L V �Y D O X H �W R �� &38V ����
1/17/12 Oracle SQL tricks – initora
3/14initora.wordpress.com/article/oracle-sql-tricks-232y3pcqwxodx-2/
,W �L V �H D V \ �W R �G L V S O D \ �K L G G H Q �L Q L W L D O L ] D W L R Q �S D U D P H W H U V ���+H U H �L V �W K H �R Q H �W K D W �,
X V H �
Connect system/manager as sysdba;
select
a.ksppinm name,
b.ksppstvl value,
b.ksppstdf default,
decode
(a.ksppity, 1,
‘boolean’, 2,
‘string’, 3,
‘number’, 4,
‘file’, a.ksppity) type,
a.ksppdesc description
from
sys.x$ksppi a,
sys.x$ksppcv b
where
a.indx = b.indx
and
a.ksppinm like ‘_%’ escape ”
order by
name;
Viewing RAM memory usage for specific SQL statements in
Oracle
2U D F O H �L�Q R Z �K D V �W K H �D E L O L W \ �W R �G L V S O D \ �5$0�P H P R U \ �X V D J H �D O R Q J �Z L W K �H [ H F X W L R Q
S O D Q �L Q I R U P D W L R Q ���7R �J H W �W K L V �L Q I R U P D W L R Q �\ R X �Q H H G �W R �J D W K H U �W K H �D G G U H V V �R I
W K H �G H V L U H G �64/�V W D W H P H Q W �I U R P �W K H �Y �V T O �Y L H Z ���)R U �H [ D P S O H ��L I �\ R X �K D Y H �D
T X H U \ �W K D W �R S H U D W H V �D J D L Q V W �W K H �1(:B&86720(5�W D E O H ��\ R X �F D Q �U X Q �W K H
1/17/12 Oracle SQL tricks – initora
4/14initora.wordpress.com/article/oracle-sql-tricks-232y3pcqwxodx-2/
I R O O R Z L Q J �T X H U \ �W R �J H W �W K H �D G G U H V V �
s e l e c t
a d d r e s s
f r o m
v $ s q l
w h e r e
s q l _ t e x t l i k e ‘ % N E W _ C U S T O M E R ’ ;
8 8 B B 4 6 0 C
1 r o w s e l e c t e d .
1R Z �W K D W �Z H �K D Y H �W K H �D G G U H V V ��Z H �F D Q �S O X J �L W �L Q W R �W K H �I R O O R Z L Q J �V F U L S W �W R �J H W
W K H �H [ H F X W L R Q �S O D Q �G H W D L O V �D Q G �W K H �3*$�P H P R U \ �X V D J H �I R U �W K H �64/�V W D W H P H Q W �
s e l e c t
o p e r a t i o n ,
o p t i o n s ,
o b j e c t _ n a m e n a m e ,
t r u n c ( b y t e s / 1 0 2 4 / 1 0 2 4 ) “ i n p u t ( M B ) ” ,
t r u n c ( l a s t _ m e m o r y _ u s e d / 1 0 2 4 ) l a s t _ m e m ,
t r u n c ( e s t i m a t e d _ o p t i m a l _ s i z e / 1 0 2 4 ) o p t _ m e m ,
t r u n c ( e s t i m a t e d _ o n e p a s s _ s i z e / 1 0 2 4 ) o n e p a s s _ m e m ,
d e c o d e ( o p t i m a l _ e x e c u t i o n s , n u l l , n u l l ,
o p t i m a l _ e x e c u t i o n s | | ’ / ' | | o n e p a s s _ e x e c u t i o n s | | ’ / ' | |
m u l t i p a s s e s _ e x e c t i o n s ) “ O / 1 / M ”
f r o m
v $ s q l _ p l a n p ,
v $ s q l _ w o r k a r e a w
w h e r e
p . a d d r e s s = w . a d d r e s s ( + )
a n d
p . h a s h _ v a l u e = w . h a s h _ v a l u e ( + )
a n d
p . i d = w . o p e r a t i o n _ i d ( + )
a n d
p . a d d r e s s = ’ 8 8 B B 4 6 0 C ’ ;
+H U H �L V �W K H �O L V W L Q J �I U R P �W K L V �V F U L S W �
O P E R A T I O N O P T I O N S N A M E i n p u t ( M B ) L A S T _ M E M O P T _ M E M O N E P A S S _ M E M O / 1 / M
— — — — — — – — - — — — — — – — — — - — — — - — -
S E L E C T S T A T E
S O R T G R O U P B Y 4 5 8 2 8
1 6 1 6 2 6 / 0 / 0
H A S H J O I N S E M I 4 5 8 2 5 9 7 6
5 1 9 4 2 1 8 7 1 6 / 0 / 0
T A B L E A C C E S S F U L L O R D E R S
5 1
1/17/12 Oracle SQL tricks – initora
5/14initora.wordpress.com/article/oracle-sql-tricks-232y3pcqwxodx-2/
T A B L E A C C E S S F U L L I N E I T E M 1 0 0 0
+H U H �Z H �V H H �W K H �G H W D L O V �D E R X W �W K H �H [ H F X W L R Q �S O D Q �D O R Q J �Z L W K �V S H F L I L F �P H P R U \
X V D J H �G H W D L O V ��7K L V �L V �D Q �H [ F L W L Q J �Q H Z �D G Y D Q F H �L Q �2U D F O H �L�D Q G �J L Y H V �W K H �2U D F O H
'%$�W K H �D E L O L W \ �W R �K D Y H �D �Y H U \ �K L J K �O H Y H O �R I �G H W D L O �D E R X W �W K H �L Q W H U Q D O �H [ H F X W L R Q
R I �D Q \ �64/�V W D W H P H Q W �
Finding a file on your Oracle server
2Q H �R I �W K H �P R V W �F R Q I R X Q G L Q J �W D V N V �I R U �W K H �2U D F O H �S U R I H V V L R Q D O �L V �O R F D W L Q J
U H O H Y D Q W �I L O H �R Q �W K H �2U D F O H �V H U Y H U ���7K H V H �I L O H V �L Q F O X G H �W U D F H �I L O H V �Q H H G H G �I R U
G L D J Q R V W L F V ��O R F D W L Q J �W K H �2U D F O H �D O H U W �O R J �I L O H ��D Q G �O R F D W L Q J �P L V F H O O D Q H R X V �I L O H V
R Q �W K H �V H U Y H U �
6R P H �R I �W K H �F R P P R Q �X V H V �I R U �W K H �I L O H �O R F D W L R Q �L Q F O X G H �
–tnsnames.ora file
–listener.ora file location
–alert log file location
–trace and dump file locations
–sqlnet.log file location(s)
)R U W X Q D W H O \ ��W K H �81,;�V \ Q W D [ �I R U �O R F D W L Q J �I L O H V �L V �F R Q V W D Q W �R Q �/L Q X [ ��+3�8;��$,;
D Q G �6R O D U L V ��D Q G �X Q L I R U P �E H W Z H H Q �.R U Q �6K H O O �%R X U Q H �V K H O O �D Q G �%R X U Q H �$J D L Q
6K H O O ��E D V K ��
81,;�
f i n d . – p r i n t | g r e p – I a l e r t | m o r e
:,1'2:6�
d i r / s a l e r t * . l o g | m o r e
Finding the most recent files in a directory
2Q F H �\ R X �K D Y H �O R F D W H G �W K H �G L U H F W R U \ �I R U �D �W U D F H �R U �G X P S �I L O H ��W K H �Q H [ W �V W H S �L V �W R
O R F D W H �W K H �P R V W �U H F H Q W �I L O H �L Q �W K H �G L U H F W R U \ ��
81,;�
l s – a l t * . t r c | h e a d
:,1'2:6
d i r * . t r c / O - D | m o r e
<R X �F D Q �D O V R �X V H �64/ 3O X V �V F U L S W V �W R �D F F H S W �Y D O X H V �D Q G �X V H �W K H �64/ 3O X V
³ K R V W ´ �F R P P D Q G �W R �V K H O O �R X W �W R �W K H �R S H U D W L Q J �V \ V W H P �W R �O R F D W H �I L O H V ���+H U H �L V �D Q
1/17/12 Oracle SQL tricks – initora
6/14initora.wordpress.com/article/oracle-sql-tricks-232y3pcqwxodx-2/
H [ D P S O H �R I �V X F K �D �V F U L S W �
p r o m p t “ p l e a s e e n t e r t h e n a m e o f t h e f i l e t o f i n d : “
a c c e p t & f i l e n a m e
h o s t f i n d / - p r i n t | g r e p – I & f i l e n a m e
Adding Custom Messages to the Oracle alert log
,Q �2U D F O H ��D O O �P D M R U �V \ V W H P �P H V V D J H V �D U H �Z U L W W H Q �W R �W K H �2U D F O H �D O H U W �O R J ���,Q
R U G H U �W R �F U H D W H �D �V L Q J O H �S O D F H �I R U �D O O �2U D F O H �V \ V W H P �P H V V D J H V ��W K H �2U D F O H
S U R I H V V L R Q D O �P X V W �G H Y H O R S �D �P H W K R G �I R U �Z U L W L Q J �D S S O L F D W L R Q �P H V V D J H V �L Q W R �W K H
D O H U W �O R J �I L O H �
%\ �F R Q V R O L G D W L Q J �D O O �2U D F O H �P H V V D J H V ��E R W K �V \ V W H P �P H V V D J H V �D Q G �D S S O L F D W L R Q
P H V V D J H V ��G D W D E D V H �D G P L Q L V W U D W L R Q �L V �J U H D W O \ �V L P S O L I L H G �E H F D X V H �D �F H Q W U D O �I L O H
H [ L V W V �I R U �D O O �2U D F O H �U H O D W H G �D O H U W �P H V V D J H V �
7K H �2U D F O H �'%$�F D Q �F U H D W H �D �J O R E D O �3/�64/�V W R U H G �S U R F H G X U H �K D Q G O H �W K L V
L Q W H U D F W L R Q �Z L W K �W K H �D O H U W �O R J �I L O H ��D Q G �W K H �J O R E D O �S D F N D J H �F D Q �E H �F D O O H G ��S D V V L Q J
W K H �D S S U R S U L D W H �P H V V D J H �W R �W K H �S U R F H G X U H ���+H U H �L V �D Q �H [ D P S O H �R I �W K L V �W \ S H �R I
F D O O �
w h e n o t h e r s t h e n
d b m s _ c u s t o m . w r i t e _ a l e r t ( : p _ s y s d a t e | | ‘ A p p l i c a t i o n e r r o r ’ | | : v a r 1 | | ’
e n c o u n t e r e d ’ ) ;
7K L V �H D V \ �Z U L W L Q J �W R �W K H �D O H U W �O R J �L V �D F F R P S O L V K H G �E \ �X V L Q J �2U D F O H ¶ V �X W O B I L O H
S D F N D J H ���7K H �X W O B I L O H �S D F N D J H �D O O R Z V �2U D F O H �64/�D Q G �3/�64/�W R �U H D G �D Q G �Z U L W H
G L U H F W O \ �I U R P �I O D W �I L O H V �R Q �W K H �V H U Y H U �
:U L W L Q J �F X V W R P �P H V V D J H V �W R �W K H �2U D F O H �D O H U W �O R J �U H T X L U H V �W K H �I R O O R Z L Q J �V W H S V �
1. /R F D W H �W K H �E D F N J U R X Q G �G X P S �G L U H F W R U \ ��W K H �O R F D W L R Q �R I �W K H �D O H U W �O R J ��
2. 6H W �W K H �X W O B I L O H B G L U �L Q L W L D O L ] D W L R Q �S D U D P H W H U �
3. ([ H F X W H �X W O B I L O H �I R S H Q �W R �R S H Q �W K H �I L O H �I R U �Z U L W H �D F F H V V �
4. 8V H �G E P V B R X W S X W �S X W B O L Q H �W R �Z U L W H �W K H �F X V W R P �P H V V D J H �W R �W K H �D O H U W �O R J �
5. ([ H F X W H �X W O B I L O H �I F O R V H �W R �F O R V H �W K H �I L O H
+H U H �L V �D �F R G H �H [ D P S O H �I R U �2U D F O H �L�W K D W �L O O X V W U D W H V �W K H �S U R F H V V �
– ******************************************************
– Gather the location of the alert log directory
1/17/12 Oracle SQL tricks – initora
7/14initora.wordpress.com/article/oracle-sql-tricks-232y3pcqwxodx-2/
– ******************************************************
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
– ******************************************************
1/17/12 Oracle SQL tricks – initora
8/14initora.wordpress.com/article/oracle-sql-tricks-232y3pcqwxodx-2/
utl_file.fclose(‘:alert_loc’);
Distributing standard Oracle files to all Servers
,Q �D �O D U J H �H Q Y L U R Q P H Q W ��W K H �2U D F O H �'%$�P X V W �U R X W L Q H O \ �G L V W U L E X W H �F R P P R Q �I L O H V
W R �D O O �2U D F O H �V H U Y H U V ���7K H V H �F R P P R Q �I L O H V �L Q F O X G H �
–tnsnames.ora
–sqlnet.ora
–.profile for the Oracle UNIX user
7K L V �G L V W U L E X W L R Q �F D Q �E H �D �K X J H �W D V N �I R U �O D U J H �2U D F O H �V K R S V �Z L W K �K X Q G U H G V �R I
2U D F O H �V H U Y H U V ��,Q �81,;��W K L V �L V �D �V X S H U �X V H I X O �V F U L S W �W K D W �,�X V H �W R �G L V W U L E X W H
F R P P R Q �I L O H V �W R �D O O �V H U Y H U V ���1R W H �W K D W �W K L V �V F U L S W �U H T X L U H V �W K H ��U K R V W V �V H W X S �W R
D O O R Z �W K H �81,;�U F S �D Q G �U V K �F R P P D Q G V ��
# ! / b i n / k s h
e c h o ‘ s t a r t i n g d i s t r i b u t i o n o f t n a n a m e s . o r a ’
# N o t e : d b n a m e s f i l e i s i n t h e f o r m H O S T D A T A B A S E
f o r h o s t i n ` c a t d b n a m e s | a w k ‘ { p r i n t $ 2 } ’ `
d o
d b = ` c a t d b n a m e s | a w k ‘ { p r i n t $ 1 } ’ `
e c h o s t a r t i n g d i s t r t o $ h o s t
r c p - p t n s n a m e s . o r a $ h o s t : / e t c / t n s n a m e s . o r a
r s h $ h o s t l s – a l / e t c / t n s n a m e s . o r a
d o n e
1R W H �W K D W �W K L V �V F U L S W �X V H V �D �G U L Y L Q J �I L O H �F D O O H G �G E Q D P H V �W K D W �F R Q W D L Q V �D �O L V W �R I
K R V W Q D P H �D Q G �G D W D E D V H �Q D P H �S D L U V ���+H U H �L V �D �V D P S O H �R I �W K L V �I L O H �
d i o n e g e s m y s i d 1
d i o n e g e s p r o d 9 i
d i o n e g e s t e s t m 1
p r a e t o r t e s t 9 i
p r a e t o r t e s t a p p s
Here are some useful SQL tricks, tested on an Oracle
database.
—————————————————————————-
1/17/12 Oracle SQL tricks – initora
9/14initora.wordpress.com/article/oracle-sql-tricks-232y3pcqwxodx-2/
�����������������
Virtual index In Oracle 10 and Oracle 9
V i r t u a l i n d e x i s a f e a t u r e o f O r a c l e 1 0 a n d O r a c l e 9 . T h e p u r p o s e o f v i r t u a l
i n d e x e s i s s i m u l a t e t h e e x i s t e n c e o f a n i n d e x , w i t h o u t a c t u a l l y b u i l d i n g i t , s o y o u
c a n e x p l a i n p l a n a s i f t h e i n d e x e x i s t e d . I t i s i m p o r t a n t t o d r o p t h e i n d e x a f t e r
y o u ' r e d o n e w i t h i t .
T o b u i l d a v i r t u a l i n d e x r u n t h e f o l l o w i n g s t a t e m e n t :
S Q L > C R E A T E u n i q u e I N D E X i n d e x _ n a m e o n t a b l e _ n a m e ( c o l _ n a m e ) N O S E G M E N T / * v i r t u a l
i n d e x * / ;
T o u s e t h e i n d e x r u n t h e f o l l o w i n g s t a t e m e n t :
S Q L > A L T E R S E S S I O N S E T " _ u s e _ n o s e g m e n t _ i n d e x e s " = T R U E ;
T o d e t e c t a v i r t u a l i n d e x i n t h e d a t a b a s e r u n t h e f o l l o w i n g s q l ( t h i s i n d e x e s
d o n ' t h a v e a n y c o l u m n s i n d b a _ i n d _ c o l u m n s ) :
S Q L > S E L E C T i n d e x _ o w n e r , i n d e x _ n a m e F R O M d b a _ i n d _ c o l u m n s W H E R E i n d e x _ n a m e N O T L I K E
' B I N $ % ' M I N U S S E L E C T o w n e r , i n d e x _ n a m e F R O M d b a _ i n d e x e s /
Does Oracle use my index or not?
One can use the index monitoring feature to check if indexes are used by an application or
not. When the MONITORING USAGE property is set for an index, one can query the
v$object_usage to see if the index is being used or not. Here is an example:
SQL> CREATE TABLE t1 (c1 NUMBER);
Table created.
SQL> CREATE INDEX t1_idx ON t1(c1);
Index created.
SQL> ALTER INDEX t1_idx MONITORING USAGE;
Index altered.
SQL>
SQL> SELECT table_name, index_name, monitoring, used FROM v$object_usage;
TABLE_NAME INDEX_NAME MON USE
1/17/12 Oracle SQL tricks – initora
10/14initora.wordpress.com/article/oracle-sql-tricks-232y3pcqwxodx-2/
—————————— —————————— — —
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 locationtablespace-
datafile-name.dbf’;
4) Take the tablespace back online
1/17/12 Oracle SQL tricks – initora
11/14initora.wordpress.com/article/oracle-sql-tricks-232y3pcqwxodx-2/
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 retrieval 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:
1/17/12 Oracle SQL tricks – initora
12/14initora.wordpress.com/article/oracle-sql-tricks-232y3pcqwxodx-2/
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 sysman identified by <the new password> ;
3. Verify that the new password works
SQL> connect sysman/<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.sysman.eml.mntr.emdRepPwd=<some encrypted value>
Replace the encrypted value with the new password value
b) Search for oracle.sysman.eml.mntr.emdRepPwdEncrypted=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:
1/17/12 Oracle SQL tricks – initora
13/14initora.wordpress.com/article/oracle-sql-tricks-232y3pcqwxodx-2/
a)oracle.sysman.eml.mntr.emdRepPwd=
verify that the password is encrypted
b) oracle.sysman.eml.mntr.emdRepPwdEncrypted=
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 by Database1. Shutdown Normal the primary 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;
1/17/12 Oracle SQL tricks – initora
14/14initora.wordpress.com/article/oracle-sql-tricks-232y3pcqwxodx-2/
Like this: Be the first to like this article.
SVRMGRL>archive log start;
SVRMGRL>alter database open;
5. 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
6. Update and/or Create TNSNAMES.ora and LISTENER.ora files to include standby db
7. Copy the OracleAdmin<sid> directory to the corresponding standby db directory
8. 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’)
9. Use ORADIM to create StandBy Oracle DB service
10. Copy the password file PWD<sid>.ora to PWDSTANDBY.ora
11. SVRMGRL>Connect sys/change_on_install@standby
12. SVRMGRL>STARTUP NOMOUNT pfile=OracleSTANDBYAdminPFileinit.ora;
13. SVRMGRL>ALTER DATABASE MOUNT STANDBY DATABASE;
14. SVRMGRL>ALTER DATABASE RENAME FILE ‘old_name’ TO ‘new_name’;
15. Copy over any archive log files
16. SVRMGRL>RECOVER STANDBY DATABASE;
17. SVRMGRL> <Ret> | FILENAME | AUTO | CANCEL
Like
top related