Perl-Sensitive Sunglasses | |
PerlMonks |
Re^2: How to improve MYSQL search performance of perl?by nan (Novice) |
on Aug 19, 2005 at 15:27 UTC ( [id://485178]=note: print w/replies, xml ) | Need Help?? |
Hi CountZero, They are all URLs, for example: http://www.permonks.org/. What I did before is to make the database stuff as a sub routine and call it every time a new line is read as I don't know how to optimize the codes:
sub search{
#take search parameter from html <form/> my $q = $_[0]; #taking param; my $found = 0; #initialize category found count; #open database my $ref = {RaiseError => 1, AutoCommit => 0}; my $dbh = DBI->connect('DBI:mysql:diet', $ref) || die "Failed to connect: $DBI::errstr"; #prepare SQL search statement my $sql = qq{select topic FROM table1 WHERE uri LIKE '$q'}; my $sth = $dbh->prepare($sql); $sth->execute(); while(my $record = $sth->fetchrow_hashref) { no warnings; print &topic($record->{topic}), " \n"; $found++; } if ($found == 0){ #if no category was found, output no found print " $q is not found in the database! \n";} $sth->finish(); $dbh->disconnect(); #disconnect from database; } Thanks again, Nan
In Section
Seekers of Perl Wisdom
|
|