Solved

perl DBI

Posted on 2011-09-20
6
306 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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 500 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl split by | 2 97
Perl string replace for refred url 9 62
PERL variable conundrum 9 90
how to print sub line with  sed /awk /perl in bash file 11 23
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

786 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