Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

perl DBI

Posted on 2011-09-20
6
Medium Priority
?
332 Views
Last Modified: 2012-05-12
Hi,

     I have some SOL queries  like
select a,b from table a ;
select * from table b;
....... like 2000 such queries.
I am executing those queries through Perl.

Some of the queries are taking more than 10 minutes .
I want all those such queries which are taking more than 2 minutes .
How this can be achieved ?
I am using
DBD-DB2'   => '1.76-9.5.4',
 'DBI'       => '1.609',
                  '
0
Comment
Question by:SWAYAM4U
6 Comments
 
LVL 23

Expert Comment

by:nemws1
ID: 36567744
Well, since you're talking about a large chunk of time, you don't need to use Time::HiRes and can get away with just using time().

Maybe something like this would work for you:
foreach my $query (@queries) {  # assuming you have all queries in an array
   my $start = time;
   $dbh->execute($query);
   my $end = time;
   my $diff = $end - $start;
   if ($diff > 120) {
      print "$diff seconds - Query took too long\n";
      print "-" x 70;
      print "\n$query\n";
      print "-" x 70;
      print "\n\n";
   }
}

Open in new window


If your queries are all spread throughout a big program, what I would do is replace my calls to "$dbh->execute(...);" with a call to a subroutine, something like "my_execute($dbh, $query);" this (very similar, just as a subroutine and not a loop).  You can always go back and modify the subroutine to NOT do the check when you're done with it.
 
sud my_execute {
   my $dbh = shift;
   my $query = shift;
   my $start = time;
   $dbh->execute($query);
   my $end = time;
   my $diff = $end - $start;
   if ($diff > 120) {
      print "$diff seconds - Query took too long\n";
      print "-" x 70;
      print "\n$query\n";
      print "-" x 70;
      print "\n\n";
   }
}

Open in new window

sub my_execute {
   my $dbh = shift;
   my $query = shift;
   my $start = time;
   $dbh->execute($query);
   my $end = time;
   my $diff = $end - $start;
   if ($diff > 120) {
      print "$diff seconds - Query took too long\n";
      print "-" x 70;
      print "\n$query\n";
      print "-" x 70;
      print "\n\n";
   }
}

Open in new window

0
 

Author Comment

by:SWAYAM4U
ID: 36568337
But this way I need to execute all the query at least once.our DBA complains if we fire queries which takes more than 10 mins.
What I need
foreach @query
alarm 600
dbh->execute ;

if dbh returns within 600 sends
reset alarm

else kill the dbh-execute()
0
 
LVL 23

Expert Comment

by:nemws1
ID: 36568889
Ah! Sorry, I misinterpreted your question - I thought you were trying to weed out queries that ran more than 2 mins so you would never run them again.

Would it be possible to just let your program run (making sure to check returns and error codes of any queries, of course!) and have a separate program that just repeatedly and periodically runs "mysqladmin proc" and kills any of your queries whose "Time" column is showing more than 10 minutes? (kill with "mysqladmin kill <id>").

I do know that there's no nice/easy way to kill off a DBI session (perhaps using fork/exec, but that seems even messier) within a Perl script.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 4

Expert Comment

by:schubach
ID: 36568997
nemws1, I don't think the poster is using mysql, since the original post says "I am using DBD-DB2'.  Perhaps this should be posted to the db2 forum to see if there is a way to kill long running queries in that database.
0
 
LVL 23

Expert Comment

by:nemws1
ID: 36569089
schubach, you are absolutely correct - my mistake!
0
 
LVL 9

Accepted Solution

by:
parparov earned 2000 total points
ID: 36569560
You can set the alarm:
http://search.cpan.org/~timb/DBI-1.616/DBI.pm#Signal_Handling_and_Canceling_Operations
describes how.

I think, in $SIG{ALRM} you will have to get a list of pending queries and then execute

$dbh->execute("CANCEL query_id");
Your DBA will surely help you providing the exact SQL to be executed.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question