如何解决MySQL数据库分析并使用Perl检查其结果
我想看看我的查询花费多少时间来使用perl执行。到目前为止,这是我的工作
#!/usr/bin/perl -w
use DBI;
use strict;
use warnings;
use diagnostics;
use POSIX qw(strftime);
my $driver = "mysql";
my $database = "employee";
my $dsn = "DBI:$driver:database=$database";
my $userid = "root";
my $password = "****";
my $sql_statement;
my $sth;
my $data;
my $row;
my $dbh = DBI->connect($dsn,$userid,$password ) or die $DBI::errstr;
my $foo = "SET profiling=on;"."\n"."SELECT * FROM employee;"."\n"."SHOW PROFILES;";
open my $fh,"<",\$foo;
binmode $fh,":encoding(utf8)";
while ($sql_statement = <$fh>)
{
$data = $dbh->selectall_arrayref($sql_statement) or die "issue is : $dbh->errstr";
}
my $retirement_date;
if (scalar @$data)
{
$retirement_date = strftime( "%F %H:%M:%S",localtime(time() - @$data[0]));
}
print $retirement_date;
但是我没有任何结果。
预期的输出应该是这样
5 15.86296022 SELECT * from employee;
其中5是查询ID,15.86296022是执行时间和最后一次查询。
请建议如何实现。
解决方法
您没有得到任何结果,因为您误解了从selectall_arrayref()
返回的数据结构。这是arrayrefs的arrayref-因此,您需要在其中深入两层才能获取实际数据。
我用以下代码替换了代码的第二部分:
同时($ sql_statement = ){ 警告“ $ sql_statement \ n”; $ data = $ dbh-> selectall_arrayref($ sql_statement)或die“ issue is:$ dbh-> errstr”; 打印join('|',@ $ _),“ \ n”表示@ $ data; }
当我运行它时,我得到了:
$ perl dbprofile
SET profiling=on;
DBD::mysql::db selectall_arrayref failed: fetch() without execute() at dbprofile line 25,<$fh> line 1.
SELECT * FROM employee;
1 | Fred | 2020-09-09
2 | Bill | 2035-06-23
3 | Jane | 2058-03-11
SHOW PROFILES;
1 | 0.00027717 | SELECT * FROM employee
我不确定“没有execute()的fetch()”错误是怎么回事,我现在没有时间进行调查。但是您可以看到所有数据-包括配置文件信息。
,请查看以下演示代码是否提供所需的输出
#!/usr/bin/perl
#
# vim: ai:ts=4:sw=4
#
use strict;
use warnings;
use feature 'say';
use DBI;
use Config::General;
my($dsn,$dbh,$sth,$query,$rv);
my $conf = Config::General->new( "$ENV{HOME}/.my.cnf" );
my %config = $conf->getall;
# Keep password inside code
$config{password} = 'your_password_here';
$dsn = "DBI:mysql:database=$config{database};host=$config{host};port=$config{port}";
$dbh = DBI->connect($dsn,$config{user},$config{password},{
RaiseError => 1,mysql_enable_utf8 => 1
});
$query = "SET PROFILING=on";
$sth = $dbh->prepare($query) or die $dbh->errstr;
$rv = $sth->execute() or die $dbh->errstr;
#say $DBI::errstr if $rv < 0;
my $db_table = 'your_db_table_here';
$query = "SELECT * FROM $db_table";
$sth = $dbh->prepare($query) or die $dbh->errstr;
$rv = $sth->execute() or die $dbh->errstr;
#say $DBI::errstr if $rv < 0;
$query = "SHOW PROFILES";
$sth = $dbh->prepare($query) or die $dbh->errstr;
$rv = $sth->execute() or die $dbh->errstr;
#say $DBI::errstr if $rv < 0;
while( my $row = $sth->fetchrow_arrayref ) {
say join("\t",@{$row});
}
$query = "SET PROFILING=off";
$sth = $dbh->prepare($query) or die $dbh->errstr;
$rv = $sth->execute() or die $dbh->errstr;
#say $DBI::errstr if $rv < 0;
$sth->finish();
$dbh->disconnect();
$ENV{HOME}/.my.cnf
文件的样本
[mysql]
host=yout_db_host_name
user=your_db_user_name
database=your_db_name
default-character-set=utf8
port=3306
输出样本
1 0.0014745 SELECT * FROM new_movies
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。