in reply to Re: fast simple DB (sqlite?) skeleton?
in thread fast simple DB (sqlite?) skeleton?
Your SQLite benchmark is seriously flawed. You really, really should prepare() your INSERT statement and wrap everything into a single transaction. And please, don't use "synchronous=OFF".
#!/usr/bin/perl use strict; use warnings; use Benchmark ':hireswallclock'; use DB_File (); use DBI (); my $DB_FILE; my $DBH_SQLITE; my @CHARS = ( 'A' .. 'Z', 'a' .. 'z', 0 .. 9, qw( ! @ $ % ^ & * +) ); my $NUM_RECORDS = 50_000; setup_dbfile(); setup_sqlite(); Benchmark::cmpthese( 10, { 'berkeley' => \&benchmark_dbfile, 'sqlite' => \&benchmark_sqlite } ); untie %$DB_FILE; sub benchmark_dbfile { my $value = 0; while ( $value < $NUM_RECORDS ) { my $key = join( '', @CHARS[ map { rand @CHARS } ( 1 .. 5 ) ] ) +; if ( exists $DB_FILE->{$key} ) { $DB_FILE->{$key} .= ",$value"; } else { $DB_FILE->{$key} = $value; } $value++; } return; } sub benchmark_sqlite { my $sth = $DBH_SQLITE->prepare( 'INSERT INTO benchmark ( key, valu +e ) VALUES ( ?, ? )' ); $DBH_SQLITE->begin_work; my $value = 0; while ( $value < $NUM_RECORDS ) { my $key = join( '', @CHARS[ map { rand @CHARS } ( 1 .. 5 ) ] ) +; $sth->execute( $key, $value ); $value++; } $DBH_SQLITE->commit; return; } sub setup_sqlite { $DBH_SQLITE = DBI->connect( 'dbi:SQLite:dbname=benchmark.sqlite', '', '', { 'RaiseError' => 1 } ); if ( ! $DBH_SQLITE->tables( undef, undef, 'benchmark', 'TABLE' ) ) + { $DBH_SQLITE->do( 'CREATE TABLE benchmark ( key VARCHAR, value +INTEGER )' ); } return; } sub setup_dbfile { my %data; tie %data, 'DB_File', 'berkeley.db' or die "$!"; $DB_FILE = \%data; return; }
Results:
s/iter berkeley sqlite berkeley 4.59 -- -81% sqlite 0.877 423% --
This is not surprising, because DB_File has to perform an extra "SELECT" for every "INSERT".
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: fast simple DB (sqlite?) skeleton?
by iaw4 (Monk) on Jan 27, 2010 at 13:54 UTC | |
by pmqs (Friar) on Jan 28, 2010 at 11:49 UTC | |
by WizardOfUz (Friar) on Jan 28, 2010 at 13:11 UTC | |
by pmqs (Friar) on Jan 29, 2010 at 10:11 UTC | |
by pmqs (Friar) on Jan 29, 2010 at 09:57 UTC |