How can I show the query time in Perl, DBI?

dbi, mysql, perl

Solution

Using Time::HiRes could also be an easy way to find the query time. Here is an example that uses Time::HiRes:

use Time::HiRes;

$start_time = Time::HiRes::gettimeofday();

my $dbh = $db->prepare("SELECT id, name FROM names ORDER BY id;");
$dbh->execute;

$end_time = Time::HiRes::gettimeofday();

my $elapsedtime = sprintf("%.6f", $end_time - $start_time);
print "Execution time(seconds) : $elapsedtime \n";

Problem

I use Perl and DBI to manage my MySQL tables, querys, etc. How can I show the running time of a query? If I do a SELECT in the console, the result will be like this: ``` +-----+-------------+ | id | name | +-----+-------------- | 1 | Jack | | 2 | Joe | | 3 | Mary | +-----+-------------+ 3 rows in set (0.17 sec) ``` I need to show `0.17 sec`. There is any way in DBI to show the running time in Perl, something like this? ``` my $dbh = $db->prepare("SELECT id, name FROM names ORDER BY id;"); $dbh->execute; print $dbh->runnin_time; # ??? ```

Original source