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

From: Norman Dunbar <oracle_at_dunbar-it.co.uk>
Date: Wed, 22 May 2013 12:06:22 +0100
Message-ID: <519CA6AE.8070807_at_dunbar-it.co.uk>



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 ....;
else

   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? ;-)

Cheers,
Norm.

-- 
Norman Dunbar
Dunbar IT Consultants Ltd

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

Company Number: 05132767
--
http://www.freelists.org/webpage/oracle-l
Received on Wed May 22 2013 - 13:06:22 CEST

Original text of this message