Here is an easy way to make the bulk-update for DB_File work a lot faster -- preprocess the input data before going near the database. What you can do is very dependent on the nature of your data, but you initial example implies you will always append to existing entries in the DB. So here is an example that show the gains that can be made by preprocessing - I've assumes there are 1000 unique keys in the database, and I'm adding 50k records.

First, note the performance gain from preprocessing.

s/iter original preprocess original 3.26 -- -93% preprocess 0.221 1375% --
And here is the code
#!/usr/bin/perl use strict; use warnings; use Benchmark ':hireswallclock'; use DB_File (); my $DB_FILE1; my $DB_FILE2; my $NUM_RECORDS = 50_000; my $NUM_KEYS = 1000; setup_dbfile(); Benchmark::cmpthese( 10, { 'original' => \&benchmark_dbfile1, 'preprocess' => \&benchmark_dbfile2 } ); sub benchmark_dbfile1 { foreach my $value ( 1 .. $NUM_RECORDS ) { my $key = int(rand($NUM_KEYS)); if ( exists $DB_FILE1->{$key} ) { $DB_FILE1->{$key} .= ",$value"; } else { $DB_FILE1->{$key} = $value; } } } sub benchmark_dbfile2 { my %preprocess = (); foreach my $value ( 1 .. $NUM_RECORDS ) { my $key = int(rand($NUM_KEYS)); push @{ $preprocess{$key} }, $value ; } while (my ($key, $val_list) = each %preprocess) { my $value = join ",", @$val_list; if ( exists $DB_FILE2->{$key} ) { $DB_FILE1->{$key} .= ",$value"; } else { $DB_FILE2->{$key} = $value; } } } sub setup_dbfile { { unlink 'berkeley.db1'; my %data; tie %data, 'DB_File', 'berkeley.db1' or die "$!"; $DB_FILE1 = \%data; } { unlink 'berkeley.db2'; my %data; tie %data, 'DB_File', 'berkeley.db2' or die "$!"; $DB_FILE2 = \%data; } }

In reply to Re^4: fast simple DB (sqlite?) skeleton? by pmqs
in thread fast simple DB (sqlite?) skeleton? by iaw4

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.