Solved

perl DBI

Posted on 2011-09-20
6
287 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now