Re: Better way (more efficient) to find existance of row

  • From: Norman Dunbar <oracle@xxxxxxxxxxxxxxx>
  • To: oracle-l@xxxxxxxxxxxxx
  • Date: Wed, 22 May 2013 12:06:22 +0100

Morning Paresh,

On 21/05/13 18:23, Paresh Yadav wrote:
> Using exceptions to handle if-else is a bad idea. The performance will be
> slow.
Have you tried this and run timings on it? One of my firct contract jobs 
years ago was to go through many millions of lines of PL/SQL, written by 
a third party, and find places to improve it.

There were numerous occurrences of this:

select count(*)
into whatever
from table
where some_condition;

if whatever = 0 then
   insert into table values ....;
   update table set .......;
end if;

We did some timings and found that, on average, using exceptions was 
faster. The problem being that the where clause will run through the 
entire table, unless restricted by a rownum = 1, unless there's an index 
(there invariably wasn't!)

That was in the days before the MERGE statement as mentioned by Niall, 
and although I have not yet done any timings myself, I suspect (!) that 
MERGE will be even faster than using exceptions.
When I get a few mins to set up a table with a few million rows, I'll 
try it out and report back, unless someone else already has the info.

> Better to use If else and try to put most probable outcome of the
> comparison in the if part.
Well, any good developer knows to put the most likely outcome first in 
an IF/CASE/whatever statement - don't they? ;-)


Norman Dunbar
Dunbar IT Consultants Ltd

Registered address:
Thorpe House
61 Richardshaw Lane
West Yorkshire
United Kingdom
LS28 7EL

Company Number: 05132767

Other related posts: