Home » RDBMS Server » Performance Tuning » index on foreign keys
index on foreign keys [message #467101] Thu, 22 July 2010 04:47 Go to next message
prashant_ora
Messages: 196
Registered: July 2010
Senior Member
Hi,

I have to create indexes on foreign key columns ,now pl suggest me if composite index is already there with foreign key column then that will work or i will have to create a single column index.

Thanks
Prashant
Re: index on foreign keys [message #467105 is a reply to message #467101] Thu, 22 July 2010 04:51 Go to previous messageGo to next message
cookiemonster
Messages: 13921
Registered: September 2008
Location: Rainy Manchester
Senior Member
If the foreign key column is first in the index that's fine, otherwise you need to create a new index.
Re: index on foreign keys [message #467111 is a reply to message #467105] Thu, 22 July 2010 05:02 Go to previous messageGo to next message
rahulvb
Messages: 924
Registered: October 2009
Location: Somewhere Near Equator.
Senior Member
You can force an index skip scan with the /*+ index_ss */ hint.
Re: index on foreign keys [message #467113 is a reply to message #467111] Thu, 22 July 2010 05:04 Go to previous messageGo to next message
rahulvb
Messages: 924
Registered: October 2009
Location: Somewhere Near Equator.
Senior Member
Please check Oracle index skip scan tips
Re: index on foreign keys [message #467121 is a reply to message #467113] Thu, 22 July 2010 05:07 Go to previous messageGo to next message
cookiemonster
Messages: 13921
Registered: September 2008
Location: Rainy Manchester
Senior Member
What's skip scans got to do with anything?
You need to index foreign key columns to avoid locking issues, and possible deadlocks, if you modify the parent.
For that to work the foreign key columns need to be on the leading edge of the index.
Re: index on foreign keys [message #467126 is a reply to message #467121] Thu, 22 July 2010 05:11 Go to previous messageGo to next message
rahulvb
Messages: 924
Registered: October 2009
Location: Somewhere Near Equator.
Senior Member
In My View..
Op is asking to create another index on FK ,
Since OP has composite key index preset then OP does not need NEW
index so SKIP SCAN comes in picture to make most of existing index
Re: index on foreign keys [message #467131 is a reply to message #467126] Thu, 22 July 2010 05:14 Go to previous messageGo to next message
cookiemonster
Messages: 13921
Registered: September 2008
Location: Rainy Manchester
Senior Member
And that counters what I said how?
Re: index on foreign keys [message #467132 is a reply to message #467131] Thu, 22 July 2010 05:15 Go to previous messageGo to next message
rahulvb
Messages: 924
Registered: October 2009
Location: Somewhere Near Equator.
Senior Member
By Skip Scan IF Composite Index already Present


[Updated on: Thu, 22 July 2010 05:17]

Report message to a moderator

Re: index on foreign keys [message #467133 is a reply to message #467132] Thu, 22 July 2010 05:16 Go to previous messageGo to next message
cookiemonster
Messages: 13921
Registered: September 2008
Location: Rainy Manchester
Senior Member
Do you really think skip scan is guaranteed to avoid locking problems?
Re: index on foreign keys [message #467136 is a reply to message #467133] Thu, 22 July 2010 05:19 Go to previous messageGo to next message
rahulvb
Messages: 924
Registered: October 2009
Location: Somewhere Near Equator.
Senior Member
cookiemonster wrote on Thu, 22 July 2010 05:16
Do you really think skip scan is guaranteed to avoid locking problems?


To be Honest , The Answer to your question cookiemonster
"I am not sure" !
Re: index on foreign keys [message #467142 is a reply to message #467136] Thu, 22 July 2010 05:27 Go to previous messageGo to next message
cookiemonster
Messages: 13921
Registered: September 2008
Location: Rainy Manchester
Senior Member
Then it'd be safer to do what I said, don't you think?
Re: index on foreign keys [message #467144 is a reply to message #467142] Thu, 22 July 2010 05:30 Go to previous messageGo to next message
rahulvb
Messages: 924
Registered: October 2009
Location: Somewhere Near Equator.
Senior Member
I am not Convince 100% and somehow I can not deny your statement either. Smile
Re: index on foreign keys [message #467153 is a reply to message #467121] Thu, 22 July 2010 06:06 Go to previous messageGo to next message
prashant_ora
Messages: 196
Registered: July 2010
Senior Member
ok. actually main purpose of creating index on foreign key is to avoid the locking.now if i create all the indexes on foreign keys then more indexes on system will not hamper the performance?
Re: index on foreign keys [message #467159 is a reply to message #467153] Thu, 22 July 2010 06:10 Go to previous messageGo to next message
cookiemonster
Messages: 13921
Registered: September 2008
Location: Rainy Manchester
Senior Member
Well more indexes means more work to do an insert/update/delete but generally it shouldn't be noticeable. If you're worried about performance, test it.
Bear in mind that if the parent table never changes then you don't need to index foreign keys to it.
Re: index on foreign keys [message #467177 is a reply to message #467159] Thu, 22 July 2010 08:07 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
OP doesn't say what release he is using, but from release 9, you no longer have the table lock problem if the FK column is not indexed.
As I understand it, Oracle reversed the implementation of the FK check. Up to 8i, if a session did DML on a parent table (and the child table had no FK index) the session had to lock the entire child table to ensure that no conflicting transaction was started. But from 9i, the session doing the DML on the parent locks only the row in the parent table and all sessions doing DML on the child table do a look-up on the parent to see if the parent row is locked. When I realized this, I thought "Why didn't they do it that way from the beginning?"
Re: index on foreign keys [message #467178 is a reply to message #467177] Thu, 22 July 2010 08:09 Go to previous messageGo to next message
rahulvb
Messages: 924
Registered: October 2009
Location: Somewhere Near Equator.
Senior Member
John Watson wrote on Thu, 22 July 2010 08:07
I thought "Why didn't they do it that way from the beginning?"


That is what we call as Evolution Smile and A nice Piece of info.

[Updated on: Thu, 22 July 2010 08:11]

Report message to a moderator

Re: index on foreign keys [message #467190 is a reply to message #467178] Thu, 22 July 2010 10:09 Go to previous messageGo to next message
cookiemonster
Messages: 13921
Registered: September 2008
Location: Rainy Manchester
Senior Member
Are you sure about that John?
This asktom thread (see Tom's fifth reply) implies that the problem was reduced in 9i not fixed completely.
Re: index on foreign keys [message #467196 is a reply to message #467190] Thu, 22 July 2010 10:53 Go to previous message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
Thanks, CM, that is interesting. I've just tested it, using the SCOTT schema on 11.2. It seems to me that the problem of locking the child table is indeed solved, in so far as DML against the child table will (from 9i on) proceed with no noticeable problems caused by executing DML on the parent. However, transactions against the child table can now block concurrent DML on the parent table.
But is it really a problem? Probably not. The case where you don't bother to index an FK column is usually where the column is checked against some sort of look-up table, used just for validation: not a true dimension table, used for predicates. So I think I would probably prefer not to have the overhead of maintaining an index, and accept the remote possibility of problems when doing work on the look-up table.

Interesting that the problem is now kind-of reversed!
Previous Topic: Delete statement is taking more time for execution
Next Topic: CPU used by user SQLs
Goto Forum:
  


Current Time: Sat May 04 13:49:52 CDT 2024