Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > DBI Update counter

Reply
Thread Tools

DBI Update counter

 
 
Glodalec
Guest
Posts: n/a
 
      01-09-2004
Hi !

Is there any way to get proper number of rows affected during update ?

The problem is, that I need to replace records if key exists, or insert
records if there is no key.
I was using REPLACE statement, but this is a performance problem. I am
using round-robin keys, so after table holds specific keys, they are
never more inserted, but only updated, which is in average 80%. REPLACE
statement DELETES and then INSERTS the same key, which is nonsense.

So I tried to do it manualy. If I would always SELECT first for specific
key, that would be performance degrade too, so my idea is to try first
to UPDATE and if it fails, it goes to INSERT.

Let's say, I have to fields, MYKEY and MYFIELD, and the table is:

mysql> select * from mytable;
+-------+---------+
| mykey | myfield |
+-------+---------+
| K1 | 10 |
| K2 | 20 |
| K3 | 30 |
+-------+---------+
3 rows in set (0.00 sec)

Because of nature of my table processing, I am using binding.

So here it is part of my code:

sub DoIt
{
my ($KEY,$VALUE)=@_;
my $UPDATE_SQL="UPDATE mytable SET myfield=? WHERE mykey=?" ;
my $update_handle = $marvindb::dbh->prepare( $UPDATE_SQL );
$update_handle->bind_param(1, $VALUE ) ;
$update_handle->bind_param(2, $KEY, DBI::SQL_VARCHAR );
my $RET=$update_handle->execute() ;
print "Key=${KEY}, Value=${VALUE}, Ret=${RET}, rows=",
$update_handle->rows, "\n";
}
DoIt("K1","50") ;
DoIt("K1","20") ;
DoIt("K2","20") ;
DoIt("K4","50") ;
DoIt("K4","60") ;
DoIt("K4","60") ;


The output for this is:
Key=K1, Value=50, Ret=1, rows=1
Key=K1, Value=20, Ret=1, rows=1
Key=K2, Value=20, Ret=0E0, rows=0
Key=K4, Value=50, Ret=0E0, rows=0
Key=K4, Value=60, Ret=0E0, rows=0
Key=K4, Value=60, Ret=0E0, rows=0


So what I see is that there is no way using UPDATE statement, whether
key exist or no, since if mysql detects, that no changes is needed,
it returns 0.


Any help would be appreciated.
 
Reply With Quote
 
 
 
 
gnari
Guest
Posts: n/a
 
      01-09-2004
"Glodalec" <> wrote in message
news:...

[snipped dbi problem where (if i understand it properly),
the count returned by UPDATE is the number of changed records,
not the number of records selected by WHERE clause]

> So what I see is that there is no way using UPDATE statement, whether
> key exist or no, since if mysql detects, that no changes is needed,
> it returns 0.
>
> Any help would be appreciated.


in my opinion, this is a bug. I do not see this with my postgresql.

if key reuse is much less likely than new keys, then you
could just allways start by inserting, and in the less likely case,
catch the key violation error, and then update.
this results in 1 operation in the common case, 2 in the other

if key reuse is much more likely than new key, and most likely to use a
different value,
you could start by update, and if the count returned is 0, do a insert.
if the key existed but with a different value, (common case) you use 1
operation.
if the key existed but with same value, you use 2 operations (insert
returning error)
if the key is new you use 2 operations.

if most common case is key reuse vith same value, you might look at adding
one dummy integer column, and do
update mytable set value=?, dummy=dummy+1 where key=?;
this should force every found row to be counted. if return is 0,
then do your insert (with dummy=0)

I still think it is a bug.

gnari



 
Reply With Quote
 
 
 
 
ctcgag@hotmail.com
Guest
Posts: n/a
 
      01-09-2004
Glodalec <> wrote:
> Hi !
>
> Is there any way to get proper number of rows affected during update ?


turn on mysql_client_found_rows:


my
$dbh=DBI->connect("DBI:mysql:database=hts;host=localhost;my sql_client_found
_rows=1", "nobody", "mypassword", { RaiseError => 1 }) ;


Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service New Rate! $9.95/Month 50GB
 
Reply With Quote
 
gnari
Guest
Posts: n/a
 
      01-10-2004
<> wrote in message
news:20040109184258.909$...
> Glodalec <> wrote:
> > Hi !
> >
> > Is there any way to get proper number of rows affected during update ?

>
> turn on mysql_client_found_rows:
>
>
> my
>

$dbh=DBI->connect("DBI:mysql:database=hts;host=localhost;my sql_client_found
> _rows=1", "nobody", "mypassword", { RaiseError => 1 }) ;


OT:
I wonder why this is not default. performance issue?

gnari



 
Reply With Quote
 
Glodalec
Guest
Posts: n/a
 
      01-11-2004
In article <20040109184258.909$>,
says...
> Glodalec <> wrote:
> > Hi !
> >
> > Is there any way to get proper number of rows affected during update ?

>
> turn on mysql_client_found_rows:
>
>
> my
> $dbh=DBI->connect("DBI:mysql:database=hts;host=localhost;my sql_client_found
> _rows=1", "nobody", "mypassword", { RaiseError => 1 }) ;
>
>
> Xho
>
>

Ths. I will try it in Mon. Although I had read DBI manual many times, I
see DBD for mysql should as well.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Page File counter and Private Bytes Counter George2 C++ 1 01-31-2008 09:27 AM
Session("counter") vs. ViewState("counter")...a newbie question The Eeediot ASP .Net 3 12-22-2004 09:31 PM
DBI Mysql storing DBI:binary, trouble with character 26 Jerome Hauss Ruby 0 10-13-2004 03:04 PM
DBI and DBI::Oracle packages configuration ulloa Perl 1 07-22-2004 05:52 PM
Mason, DBI, and DBI::Pg Asby Perl Misc 0 07-24-2003 09:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57