DB optimization

This commit is contained in:
Difrex(Denis Zheleztsov) 2014-06-16 12:49:05 +04:00
parent 3ff0f7b936
commit 68268a51b5
2 changed files with 24 additions and 4 deletions

View File

@ -20,6 +20,22 @@ sub new {
return $self; return $self;
} }
sub begin {
my ($self) = @_;
my $dbh = $self->{_dbh};
# Begin transaction
$dbh->do('BEGIN');
}
sub commit {
my ($self) = @_;
my $dbh = $self->{_dbh};
# Commmit transaction
$dbh->do('COMMIT');
}
sub write_out { sub write_out {
my ( $self, %data ) = @_; my ( $self, %data ) = @_;
my $dbh = $self->{_dbh}; my $dbh = $self->{_dbh};
@ -51,8 +67,7 @@ sub write {
my $sth = $dbh->prepare($stmt); my $sth = $dbh->prepare($stmt);
$sth->execute(@bind); $sth->execute(@bind);
$sth->finish();
print "Message writed to DB!\n";
} }
sub select_out { sub select_out {

View File

@ -63,6 +63,10 @@ sub get_echo {
= "<!DOCTYPE html><meta charset=utf8><body><h1>Новые сообщения</h1>\n"; = "<!DOCTYPE html><meta charset=utf8><body><h1>Новые сообщения</h1>\n";
if ( defined($msgs) ) { if ( defined($msgs) ) {
my @msg_list = split /\n/, $msgs; my @msg_list = split /\n/, $msgs;
# Begin transaction
print "Writing messages\n";
$db->begin();
while (<@msg_list>) { while (<@msg_list>) {
my $mes_hash = $_; my $mes_hash = $_;
@ -104,9 +108,10 @@ sub get_echo {
# Write message to DB # Write message to DB
$db->write(%data); $db->write(%data);
# $new_messages .= "$post</p>";
} }
# Commit transaction
$db->commit();
print "Messages writed to DB!\n";
} }
return $msgs; return $msgs;
} }