How do I count keys in a BerkeleyDB *quickly* (using DB_File)?

D

Damon Hastings

I have a berkeley db which I'm accessing via the DB_File interface (a
btree). I would like a quick way to count how many keys are in the
file. If I just use "scalar keys %hash", then it takes forever! (The
file has millions of keys.) Can anyone help me?
 
S

Sam Holden

Damon Hastings ([email protected]) wrote on MMMDCLVIII September
MCMXCIII in <URL::) I have a berkeley db which I'm accessing via the DB_File interface (a
:) btree). I would like a quick way to count how many keys are in the
:) file. If I just use "scalar keys %hash", then it takes forever! (The
:) file has millions of keys.) Can anyone help me?


'scalar keys %hash' is the way to go. The only way to get the number
of keys from a db hash is to count them.

Of course if the hash is only used by you script (or scripts) then
you could add a 'count' key to the db hash, and keep it up to date
when you modify the hash.

See sig below for the big drawback of that approach...
 
L

Lack Mr G M

|>
|> I have a berkeley db which I'm accessing via the DB_File interface (a
|> btree). I would like a quick way to count how many keys are in the
|> file. If I just use "scalar keys %hash", then it takes forever! (The
|> file has millions of keys.) Can anyone help me?

my $n = 0;
while (my ($dnc1, dnc2) = each %DB_hash) {$n++};

might be quicker - but you'll have to test it...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top