Here's another, untested, approach. I'm going to assume that $id, your first-level key, is supposed to be the primary index of the table. A hash implies uniqueness of the keys, otherwise you'd lose data.

The task is to produce a hash, %kol, which contains a table's primary index values as top-level keys, and a hash of each row's other field/value pairs at the second level.

That second-level hash will almost fall in our lap if we use fetchrow_hashref() to read the table. There will be no need to hardcode or independently determine the column names.

First, though, we need to find out what the primary key is. You've assumed it will be id, but lets generalize and make this thing work for any table with a primary index. I'll leave out the connection and all that other stuff, picking up at your # Method n comment.

# Method Z my @key_columns = $dbh->primary_key( $catalog, $schema, $table);
Instead of relying on a count while reading the table to limit the number, we'll tell the database to do it for us.Oops, misthunk, lets have no limits.
# my $limit = 6; my $select = $dbh->prepare("select * from $table");# LIMIT $limit"); $select->execute();
Now we loop through fetchrow_hashref to get a col/val hash of each row. For each, we'll delete the primary key columns, welding together their values to make a top-level key for %kol ( it may be a little unfamiliar to use delete that way, but it's a handy trick). What's left in the $row hashref is exactly what you want for the second-level hash, so we just plug that reference right in there.
while (my $row = $select->fetchrow_hashref()) { my $key = join '', delete @{$row}{@key_columns}; $kol{$key} = $row; }
That's it! You may want to join the key elements using some unlikely character like "\0", just in case you want to extract them from the key later. We didn't need to call finish() on any handles because we didn't leave anything unread.

I have no idea how that will benchmark. It avoids a good bit of data copying, so it ought to be competitive.

After Compline,
Zaxo


In reply to Re: Accessing data from a dynamic database/table by Zaxo
in thread Accessing data from a dynamic database/table by rsiedl

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.