G
Glodalec
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.
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.