Best practices - in memory database table
- From: "jpl" <lapham@xxxxxxxxxxxxxxx>
- Date: 30 Aug 2006 04:28:04 -0700
I often need to load an entire database table into an in-memory perl
variable, for use in quick data lookups. So, for example, I may store
a database table with many columns into a 2 dimensional hash named
%data, using the table's primary key ("id") to index one of the hash
dimensions:
$data{$hashref->{id}} = $hashref;
This is fine and good, I can use this %data hash in my program to
access the data stored in the table. The lookups are quick when
accessing my primary key lookup. However, there may be other UNIQUEly
constrained columns in the table, let's use "filename" as an example.
I cannot efficiently access the record in %data that corresponds to a
particular "filename", I would have to look through every "id" in %data
until I found the matching "filename".
So, at the expense of doubling my memory requirements, I could
construct the hash thusly:
$data{'by_id'}{$hashref->{id} = $hashref;
$data{'by_filename'}{$hashref->{filename}} = $hashref;
Now I can do quick lookups on the records stored in %data using either
"id" or "filename".
Obviously, this solution is not a general solution for any particular
column in the table. My question is, what do people consider the most
flexible design for the perl hash variable to allow for a general way
to perform quick hash-like lookups of data?
I cannot imagine that my solution (expanding the hash for each column
to be indexed) is the best solution.
Thanks for any pointers, Jon
.
- Prev by Date: Re: finishing sth using Childhandles
- Next by Date: DBI::st=HASH(0x88f6520)
- Previous by thread: Copy one installation to another server - not working
- Next by thread: DBI::st=HASH(0x88f6520)
- Index(es):
Relevant Pages
|