Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

perl DBI

Posted on 2011-09-20
6
Medium Priority
?
331 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
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.

 
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

715 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