Your analysis is basically correct, notwithstanding the various tangential tuning attempts. But I did notice you could probably gain significant performance instantly using BETWEEN instead of IN, that is with most DBMS's I know, e.g.:
select photo_id from colors where ( r between 12 and 15 ) and ( g betw +een 78 and 81 ) and b between 23 and 26
As for the data structure to load the lot into for a memory scan, there isn't anything purpose built for your problem (except possibly some linear programming modules that are OTT for your simple need). But whether hoh or aoh is the best fit depends on whether photo_id is numeric without gaps, but the lower level hash should have keys r,g,b with the respective values. Also use Storable and only update it if the database changes to avoiding having to read the whole database every time. To search the hash or array, for the aoh case something like:
my @match = map Filter( 'r', $minred, $maxred, Filter( 'g', $mingreen, + $maxgreen, Filter( 'b', $minblue, $maxblue, $array[ $_ ] ))), @array +; sub Filter { my ( $colour, $min, $max, $href ) = @_; $href or return undef(); ( $href -> { $colour } >= $min ) or return undef(); ( $href -> { $colour } <= $max ) or return undef(); return $href; }
for hoh just substitute $array[ $_ ]with $hash{ $_ } in the map statement;
__________________________________________________________________________________

^M Free your mind!

Key to hats: ^I=white ^B=black ^P=yellow ^E=red ^C=green ^M=blue - see Moron's scratchpad for fuller explanation.


In reply to Re: multi-dimensional range lookup by Moron
in thread multi-dimensional range lookup by danmcc

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.