Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Large table query code help!

by Anonymous Monk
on Feb 02, 2012 at 20:05 UTC ( #951518=note: print w/replies, xml ) Need Help??

in reply to Large table query code help!

What are you trying to achieve in your code? If you want account numbers that are both in table 1 and table 2, it can be easily done with SQL only. In table 1 but not in table 2? No problem. Database engines have been optimising for these sorts of things for decades, so it will very likely work faster and look cleaner than your current code.

As I understand it, you query for the second table in batches of fifty, leave the last batch of (up to) 50 accounts in $allaccounts, and then print out that variable.

Does the values in @files on line 34 needed to somehow have comma separated in order to the code to work?

I am not familiar with your database library (nor do you anywhere mention which one you are using), but if it supports placeholders such as those you are using, I very much doubt that.

Replies are listed 'Best First'.
Re^2: Large table query code help!
by Anonymous Monk on Feb 02, 2012 at 20:48 UTC
    I see your point and as far as this code sample is, the issue here is that this line:
    for( my $i = 0; $i < @data; $i += 50 ) { ...
    should end after the last foreach loop, no db calls or whatever else should matter since you mentioned that all has been working, or:
    #!/usr/bin/perl -w use strict; use CGI qw/:standard/; use POSIX qw(strftime); my $db = "test"; my $sql_dbh = lib::Db->connect( 'local' ); # get a list of account numbers my @data = map { $_->{ ACCOUNT } } @{ $sql_dbh->exec( qq|select accoun +t from my_table where type='A'|) }; if (!@data) { die "bye now";exit; } my $numb = @data; print "\n Numbers of Accounts: $numb\n"; # Check 50 records at time my $allaccounts; for( my $i = 0; $i < @data; $i += 50 ) { my @files = @data[$i .. $i+49]; #my $all = join(', ', @files); #print "\n $i - $all\n"; #test data #push(@files, "11223","5776","0001","87100"); my $placeholders = join ',', ('?') x @files; $allaccounts = $sql_dbh->exec( "select * from othertable where acc i +n ($placeholders)", @files ); #} # not here my $cc=0; foreach (@$allaccounts) { $cc++; # print report my $found = qq|$_->{ name }$_->{ number }|; print "\n $cc - Reporting: $found\n"; } } # end here

    Enjoy it !!!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://951518]
[holli]: You see, I have a friend whose daughter has been damaged seriously because she wasn't vaccinated against measles. (Her mother being anti-vaxx).
[LanX]: Contrary to Olivia newton John I don't like physical confrontations ...
[LanX]: Oh, you spread measles in your saliva?
[LanX]: ... and why did you bite hos daughter?
[LanX]: *his
[holli]: And this guy was also Anti-Vaxx and I just snapped. I shoed him all over the yard
[james28909]: biting between the legs? now thats my kind of religion.
[holli]: and back into the building, saw him later peeking out of the window looking scared. It was hilarious.
[LanX]: Let's get physical...
[james28909]: wish i could stay and chat. i have an unsuspecting client to give an estimate to. muhhahaha

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (12)
As of 2017-12-13 18:04 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (373 votes). Check out past polls.