aceofspace has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to find a solution to the following problem:

Re: http://www.perlmonks.org/?node_id=877938

Any suggestion?

Replies are listed 'Best First'.
Re: How to Limit MySql Execution Time?
by BrowserUk (Patriarch) on Dec 21, 2010 at 06:57 UTC

    How about using the mysql command line tool with --quick --raw -be options via a piped open?

    my $sql = ...; my $cmd = qq[mysql --quick --raw -u $user -p$pass -be $sql] my @results; eval{ alarm 5; my $pid = open SQL, "$cmd |" or die $!; push @results, <SQL>; alarm 0; }; if( $@ ) { } ...

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thanks for your reply. But I've a hard time understanding your code and how to implement it.

      Please do me a flavor. Kindly edit my original script below with your implementation.
      #!/usr/bin/perl require 'myconfigure.cgi'; $timeout = 5; use DBI; $keywords = "Whether rocking natural-curls, a-short"; print "Content-type: text/html\n\n"; sub do_search { $time1 = time; $dbh=DBI->connect("dbi:mysql:$database:localhost","$username","$pa +ssword"); $keywords_quoted = $dbh ->quote ($keywords); $query="SELECT count(*) FROM $websites_table WHERE MATCH(title) AG +AINST ($keywords_quoted)"; $sth=$dbh->prepare(qq{$query}); $sth->execute(); $total_count = $sth->fetchrow_array (); $sth->finish; $dbh->disconnect ||die("Couldn't disconnect to database!\n"); $time2 = time; $time_ran = $time2 - $time1; } eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm $timeout; &do_search; alarm 0; }; if($@) { $time2 = time; $time_ran = $time2 - $time1; print "$@, Time Ran: $time_ran, Total Count: $total_count\n"; exit; } print "Time Ran: $time_ran, Total Count: $total_count\n"; exit;

      Thanks so much!

        Did you follow the supplied link to the mysql documentation?


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.