如何在Perl,DBI中显示查询时间? [英] How can I show the query time in Perl, DBI?

查看:133
本文介绍了如何在Perl,DBI中显示查询时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Perl和DBI管理我的MySQL表,查询等.如何显示查询的运行时间?

I use Perl and DBI to manage my MySQL tables, querys, etc. How can I show the running time of a query?

如果我在控制台中执行SELECT,结果将如下所示:

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)

我需要显示0.17 sec. DBI中有什么方法可以显示Perl中的运行时间,像这样吗?

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; # ???

推荐答案

我在DBI中找不到任何内容.我认为,虽然可能是有趣的信息,但尚未实现任何现成的方法.

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

执行此操作的另一种方法是获取执行前后的时间,然后进行简单的更改.您可以在Perl脚本中执行此操作,只需在查询执行之前获取时间戳,然后在获取时间戳之后,再减去两者即可找到执行时间.

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";

这篇关于如何在Perl,DBI中显示查询时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆