Solved

perl DBI

Posted on 2011-09-20
6
318 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…

730 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