05.11.2015 Views

Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

206<br />

CHAPTER 6 ■ LOCKING AND LATCHING<br />

blocked in the database. If any of these blocked sessions are, in fact, locking a resource that<br />

my session also needs, we will have a deadlock. The deadlock in this case is caused by my session<br />

preventing access to many more resources (in this case, all of the rows in a single table)<br />

than it ever needed. When someone complains of deadlocks in the database, I have them run<br />

a script that finds unindexed foreign keys, <strong>and</strong> 99 percent of the time we locate an offending<br />

table. By simply indexing that foreign key, the deadlocks—<strong>and</strong> lots of other contention<br />

issues—go away. The following example demonstrates the use of this script to locate the<br />

unindexed foreign key in table C:<br />

ops$tkyte@ORA10G> column columns format a30 word_wrapped<br />

ops$tkyte@ORA10G> column tablename format a15 word_wrapped<br />

ops$tkyte@ORA10G> column constraint_name format a15 word_wrapped<br />

ops$tkyte@ORA10G> select table_name, constraint_name,<br />

2 cname1 || nvl2(cname2,','||cname2,null) ||<br />

3 nvl2(cname3,','||cname3,null) || nvl2(cname4,','||cname4,null) ||<br />

4 nvl2(cname5,','||cname5,null) || nvl2(cname6,','||cname6,null) ||<br />

5 nvl2(cname7,','||cname7,null) || nvl2(cname8,','||cname8,null)<br />

6 columns<br />

7 from ( select b.table_name,<br />

8 b.constraint_name,<br />

9 max(decode( position, 1, column_name, null )) cname1,<br />

10 max(decode( position, 2, column_name, null )) cname2,<br />

11 max(decode( position, 3, column_name, null )) cname3,<br />

12 max(decode( position, 4, column_name, null )) cname4,<br />

13 max(decode( position, 5, column_name, null )) cname5,<br />

14 max(decode( position, 6, column_name, null )) cname6,<br />

15 max(decode( position, 7, column_name, null )) cname7,<br />

16 max(decode( position, 8, column_name, null )) cname8,<br />

17 count(*) col_cnt<br />

18 from (select substr(table_name,1,30) table_name,<br />

19 substr(constraint_name,1,30) constraint_name,<br />

20 substr(column_name,1,30) column_name,<br />

21 position<br />

22 from user_cons_columns ) a,<br />

23 user_constraints b<br />

24 where a.constraint_name = b.constraint_name<br />

25 <strong>and</strong> b.constraint_type = 'R'<br />

26 group by b.table_name, b.constraint_name<br />

27 ) cons<br />

28 where col_cnt > ALL<br />

29 ( select count(*)<br />

30 from user_ind_columns i<br />

31 where i.table_name = cons.table_name<br />

32 <strong>and</strong> i.column_name in (cname1, cname2, cname3, cname4,<br />

33 cname5, cname6, cname7, cname8 )

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!