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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...