dbTalk Databases Forums  

updates vs queries

comp.databases.ibm-db2 comp.databases.ibm-db2


Discuss updates vs queries in the comp.databases.ibm-db2 forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Deodiaus
 
Posts: n/a

Default updates vs queries - 02-23-2011 , 02:34 PM






I have a piece of C++ code in which I try to update a record for a
primary key. If the record does not exist, an exception is raised and
the data is treated as an insert by hitting the exception logic.
Someone pointed out that this is expensive because of the stack unwind
on the exception logic, but I argue that it is still faster because
finding out if an primary record exists takes time. Others argue that
exception logic should be used for exceptional events rather than as a
kludge which happens by circumstance.
Is this acceptable?

----------example ----------------
int foo::Upate(string stIn){


try{
m_Conn.sql = "update tab1 set foo=" + stIn.c_str() +" where
some=cond ";
m_Conn.exec();
catch(SQLException ex){
m_Conn.sql = "insert into tab1 values(" + stIn.c_str() +" , cond)
";
m_Conn.exec();
}

}

Reply With Quote
  #2  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: updates vs queries - 02-23-2011 , 04:27 PM






On 2011-02-23 21:34, Deodiaus wrote:
Quote:
I have a piece of C++ code in which I try to update a record for a
primary key. If the record does not exist, an exception is raised and
the data is treated as an insert by hitting the exception logic.
Someone pointed out that this is expensive because of the stack unwind
on the exception logic, but I argue that it is still faster because
finding out if an primary record exists takes time. Others argue that
exception logic should be used for exceptional events rather than as a
kludge which happens by circumstance.
Is this acceptable?

----------example ----------------
int foo::Upate(string stIn){


try{
m_Conn.sql = "update tab1 set foo=" + stIn.c_str() +" where
some=cond ";
m_Conn.exec();
catch(SQLException ex){
m_Conn.sql = "insert into tab1 values(" + stIn.c_str() +" , cond)
";
m_Conn.exec();
}

}
You don't mention what version of db2 you are using, but I recommend
that you have a look at MERGE:

http://publib.boulder.ibm.com/infoce...c/r0010873.htm

/Lennart

Reply With Quote
  #3  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: updates vs queries - 02-23-2011 , 04:40 PM



On 2011-02-23 23:27, Lennart Jonsson wrote:
Quote:
On 2011-02-23 21:34, Deodiaus wrote:
I have a piece of C++ code in which I try to update a record for a
primary key. If the record does not exist, an exception is raised and
the data is treated as an insert by hitting the exception logic.
Someone pointed out that this is expensive because of the stack unwind
on the exception logic, but I argue that it is still faster because
finding out if an primary record exists takes time. Others argue that
exception logic should be used for exceptional events rather than as a
kludge which happens by circumstance.
Is this acceptable?

----------example ----------------
int foo::Upate(string stIn){


try{
m_Conn.sql = "update tab1 set foo=" + stIn.c_str() +" where
some=cond ";
m_Conn.exec();
catch(SQLException ex){
m_Conn.sql = "insert into tab1 values(" + stIn.c_str() +" , cond)
";
m_Conn.exec();
}

}

You don't mention what version of db2 you are using, but I recommend
that you have a look at MERGE:

http://publib.boulder.ibm.com/infoce...c/r0010873.htm

I for whatever reason you can't or don't want to use MERGE, be more
specific in your catch clause. I.e. only insert when you catch a "no
rows found" exception.

If the key "rarely" exists it might be better to start with the insert,
catch "key violation", and do the update.

/Lennart

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.