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

后端 未结 4 1342
梦谈多话
梦谈多话 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";
    
    0 讨论(0)
  • 2021-01-05 04:46

    You take a timestamp before you run the query, and a timestamp after. The difference is your query execution time. For obtaining high-resolution timestamps, see Time::HiRes

    0 讨论(0)
  • 2021-01-05 04:50

    DBI#Profile, DBI::Profile, DBI::ProfileData, DBI::ProfileDumper, dbiprof

    0 讨论(0)
  • 2021-01-05 04:55

    Reading @daxim's links to documentation, there is a simple way to achieve this by running your script with DBI_PROFILE=2 which is from DBI::Profile

    Example output:

    DBI::Profile: 53.203692s 50.67% (6725 calls) script.pl @ 2016-01-21 11:51:49
    'INSERT INTO FOO ("BAR") VALUES (?)' =>
        0.057596s / 2 = 0.028798s avg (first 0.051621s, min 0.005975s, max 0.051621s)
    'INSERT INTO BAZ ("QUX") VALUES (?)' =>
        0.367184s / 44 = 0.008345s avg (first 0.039410s, min 0.002445s, max 0.039410s)
    
    0 讨论(0)
提交回复
热议问题