My quick suggestion would be to use index instead of this regex $data =~ /$id/

if (index($data,$id)+1){

UPDATE:Ovid's and RMGir's soutions are much more scalable. My suggestion is more of use index instead of regexes.

Benchmark

Benchmark: timing 10000 iterations of index, regex... index: 3 wallclock secs ( 3.52 usr + 0.00 sys = 3.52 CPU) @ 28 +40.91/s (n=10000) regex: 92 wallclock secs (91.44 usr + 0.00 sys = 91.44 CPU) @ 10 +9.36/s (n=10000)

Code for benchmark

sub regex { foreach my $data (@db) { foreach my $id (@id) { if ($data =~ /$id/) {1} } } } sub index { foreach my $data (@db) { foreach my $id (@id) { if (index($data,$id)+1) {1} } } }

UPDATE 2: to reiterate Ovid's point - The benchmark of my nested loop solution converges with Ovid's solution with just a hundred entries in each of your data sets



grep
<hand_wave> These are not the monks you're looking for </hand_wave>

In reply to Re: need more efficient way to write this query script by grep
in thread need more efficient way to write this query script by Anonymous Monk

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.