views:

78

answers:

1

I want to use in Berkeley DB the following Perl logic (for many millions records):

$hash{key1}{key2}{key3}{count1}++;
$hash{key1}{key2}{key3}{count2}++;

...

for (key1) {
   for (key2) {
     for (key3) {
        print $hash{key1}{key2}{key3}{count1}."\t".$hash{key1}{key2}{key2}{count2};
        }
     }
  }

Any example from multiple keys? I may of couse use "pseudo-multiple" key ( key1_key2_key3 ); but is there any other way?

+4  A: 

Berkeley-db doesn't support multiple keys like that. Each record can have only one key.

You can concatenate the keys to form a single key, as you stated.

You can use MLDBM to give the appearance of nested keys. But that works by storing a serialized hash under key1, so it will be very inefficient if you have a lot of keys nested under the top-level key.

Or, you can give up on BDB and go with a real SQL database. DBD::SQLite is easy to install and includes the SQLite database engine along with its driver.

I'd go with either concatenating the keys or a real database, depending on what exactly you're trying to do.

cjm
DBM::Deep is a less-shonky alternative to MLDBM.
hobbs