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

后端 未结 4 1369
梦谈多话
梦谈多话 2021-01-05 03:57

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:

4条回答
  •  太阳男子
    2021-01-05 04:39

    I can't find anything in DBI. I think that there is nothing already implemented out of the box, though could be interesting information.

    The other way to do this would be to get the time before and after the execution and then make a simple difference. You can do it from within your Perl script simply getting the time stamp before the query execution, and after, then subtract the two to find the execution time.

    my $start = DateTime->now;
    my $dbh = $db->prepare("SELECT id, name FROM names ORDER BY id;");
    $dbh->execute;
    my $end = DateTime->now;
    
    
    my $elapsedtime = ($end->subtract_datetime($start))->seconds;
    print "Execution time(seconds) : $elapsedtime \n";
    

提交回复
热议问题