SITUATION:
I have to check many lines of a file, against records in a database. As an example, I have a list of names in a plain text file, one per line. I also have names in a PSQL database field. I need to filter out name from the file against the database. There may be anywhere from 10,000 to 10,000,000 names in the file, and anywhere from 1,000 to 2,000,000 names in the database.

CURRENT SETUP:
I am currently doing the following SQL call inside the file loop:

my $statement = "select name_field from table where name_field = '$NAM +E' limit 1"; my $sth = $dbh->prepare($statement); $sth->execute or die( $sth->errstr ); my @row_ary = $sth->fetchrow_array; if( @row_ary ){ $sth->finish; return 1; } else { $sth->finish; return 0; }

PROBLEM:
It is very slow and uses a lot of CPU. I have read over the DBD-Pg documentation to find a better way to use prepare/bind/execute statements, but couldn't fully understand it. I know I can clean up the current code (and will) but would like some direction before doing so.

Any help is much appreciated.


In reply to PSQL and many queries by citycrew

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.