andreas1234567 has asked for the wisdom of the Perl Monks concerning the following question:

A recent discussion titled MLDBM performance problem made me curious on the performance of MLDBM versus a DBMS such as MySQL.

Here is a Benchmark of a small, write/read intensive script.

#!/usr/bin/perl -sw use strict; use Benchmark qw (cmpthese); use MLDBM qw(DB_File Storable); use Fcntl; use DBI; our $N ||= 1e4; # ------ MLDBM variables my %hash = (); my $dbname = q{testmldbm.db}; my $dbm; # ------ MySQL variables my $dbh = undef; my %sths = (); # ------ MLDBM functions sub init_mldbm { $dbm = tie (%hash, 'MLDBM', $dbname, O_CREAT|O_RDWR, 0640) or die $! +; } sub write_mldbm { my ($key,$value) = @_; init_mldbm() unless $dbm; my $arref = $value; $hash{$key} = $arref; return $hash{$key}; } sub read_mldbm { my $key = shift; init_mldbm() unless $dbm; return (exists( $hash{$key} )) ? $hash{$key} : undef; } sub reset_mldbm { if (-f $dbname) { unlink($dbname) or die(qq{failed to unlink $dbname}); } } # ------ MySQL functions sub connect_mysql{ $dbh = DBI->connect('DBI:mysql:test', '...', '...'); die("connect failed") if(!$dbh); } sub write_mysql { my ($key,$value) = @_; connect_mysql() unless $dbh; my $sql = q{insert into mldbm(k,v) values(?,?)}; $sths{$sql} = $dbh->prepare($sql) unless $sths{$sql}; $sths{$sql}->execute($key, $value) or die(qq{execute failed using va +lue:$value}); } sub read_mysql { my $value = shift; connect_mysql() unless $dbh; my $sql = q{ select v from mldbm where k=? }; $sths{$sql} = $dbh->prepare($sql) unless $sths{$sql}; $sths{$sql}->execute($value) or die(qq{execute failed using value:$v +alue}); my $ary_ref = $sths{$sql}->fetchrow_hashref; return $ary_ref->{v}; } sub reset_mysql { connect_mysql() unless $dbh; my $sql = q{ delete from mldbm }; $sths{$sql} = $dbh->prepare($sql) unless $sths{$sql}; $sths{$sql}->execute() or die(qq{execute failed}); } # ------ main ------ reset_mldbm(); reset_mysql(); my ($i,$j) = (1,1); cmpthese($N, { mldbm => sub { write_mldbm($i,$i); read_mldbm($i++); }, mysql => sub { write_mysql($j,$j); read_mysql($j++); }, } ); __END__ =head1 NAME mldbm - Benchmark MLDBM vs MySQL =head1 MySQL commands mysql 4.1.20, Linux 2.6.9, ext3 file system, single IDE disk MySQL tables are MyISAM. create database mldbm; use mldbm; create table mldbm (k integer primary key, v integer not null); =head1 RESULTS $ ./mldbm.pl -N=1e4 Rate mysql mldbm mysql 4950/s -- -10% mldbm 5495/s 11% -- $ ./mldbm.pl -N=1e5 Rate mldbm mysql mldbm 4011/s -- -16% mysql 4778/s 19% -- $ ./mldbm.pl -N=2e5 Rate mldbm mysql mldbm 3798/s -- -23% mysql 4918/s 29% -- $ ./mldbm.pl -N=5e5 Rate mldbm mysql mldbm 4110/s -- -17% mysql 4928/s 20% -- =cut
It seems MLDBM is slightly faster than MySQL for smaller (<= 1e4) data sets, but as the data set size grows, MySQL seems to operate faster.

Comments please.

--
print map{chr}unpack(q{A3}x24,q{074117115116032097110111116104101114032080101114108032104097099107101114})

Replies are listed 'Best First'.
Re: Benchmark MLDBM vs MySQL
by perrin (Chancellor) on Jun 12, 2007 at 12:10 UTC
    I think you'll find that the choice of dbm affects this greatly. See the benchmarks in MLDB::Sync for example. Also, make sure you connect to MySQL as localhost, so it will use Unix sockets and not TCP.
Re: Benchmark MLDBM vs MySQL
by Zaxo (Archbishop) on Jun 12, 2007 at 07:07 UTC

    I didn't rerun these, but I'd like to commend you for using the benchmark to investigate scaling performance. It stands out that MySQL exhibits a much more gradual degradation.

    After Compline,
    Zaxo