Ok, first I tried to look at the problem from a number of angles. I started by suggesting that you could use a tree (e.g., Tree::Binary) to store things - but the problem is that you want to look up data in multiple ways: by serial number, and by last-in. Look-up by serial number is a hash lookup, while look-up by last-in is an ordered list (ordered on the time of the insert). So then I was about to suggest storing your data simultaneously in multiple containers (since it's all references anyway, it's not going to take up that much space). But then, my fingers and mind got all twisted up trying to describe it.

That's a good indication that either it's a bad idea, or I don't understand the idea well enough to teach someone. I've been introduced to the idea before, but I've never actually used multiple containers for a single set of data before. So my experience just isn't great enough to really describe it. Then the epiphany.

You could simply get the display data directly from the database. Set indexes on the serial numbers and the last-insert-time fields. If you get multiple inserts per second, you may need a sub-second time field (I'm not sure if that's standard in RDBMSes or not - I pretty much only use DB2). If you still get multiple inserts simultaneously, it's not really that bad of a thing anyway. You can then query directly for any serial number you want (the database will use a b-tree for index lookup - for 20,000 records, that will mean approximately 14.3 lookups on average), and also query for the 20 highest insert times. Let the database worry about it. It'll use the best algorithms that it can - which you can help by setting up proper indexes.

Basically, you already have a powerful tool for this, and you're already eating most of the overhead of doing this with that tool. Take advantage of that tool for the rest of it :-)


In reply to Re^4: Populate and sort AoA by Tanktalus
in thread Populate and sort AoA by nedals

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.