List:MySQL and Perl« Previous MessageNext Message »
From:Christopher Pryce Date:April 2 2004 2:25am
Subject:Re: How to speed UPDATES
View as plain text  
On Thursday, April 1, 2004, at 02:16 PM, Etienne Orliac wrote:

>  The piece of code for the update is ():
>
> foreach my $h (@$Selec)
> {
>         my $epoch  = ${%$h}{'EPOCH'};
>         my $site   = ${%$h}{'SITE'};
>         my $corsite  =  ("'"."$site"."'");    # Corresponds to the 
> station field
>         my $corep    = ("'"."$epoch"."'");  # Corresponds to the epoch 
> field

consider calling $dbh->quote() or use placeholders

>         my $sth = $dbh->do( "
>                               UPDATE $table
>                               SET $SET
>                               WHERE ( epoch LIKE $corep  AND station = 
> $corsite)
>                               LIMIT 1
>                              ");
> }

Ideally, you should prepare a statement outside of a foreach loop, and 
execute it inside, something along the lines of  (untested and normal 
error checking omitted):

my $sql = qq(  UPDATE $table SET $SET WHERE ( epoch = ? AND station = ? 
) );
my $sth = $dbh->prepare( $sql );

foreach my $h (@$Selec) {
	$sth->execute( $h->{'EPOCH'}, $h->{'SITE'} ) ;
}

Note that using placeholders eliminates the need for quoting char 
values. The Driver does that for you. For a batch of 1500 inserts, you 
eliminate 1500 prepare statements, which will be a great savings.

cp

Thread
How to speed UPDATESEtienne Orliac1 Apr
  • Re: How to speed UPDATESUlrich Borchers1 Apr
    • Re: How to speed UPDATESlshen1 Apr
Re: How to speed UPDATESEtienne Orliac1 Apr
  • Re: How to speed UPDATESUlrich Borchers1 Apr
  • Re: How to speed UPDATESChristopher Pryce2 Apr
Re: How to speed UPDATESEtienne Orliac2 Apr