加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl – 使用这种替代机制进行DBI查询有什么问题?

发布时间:2020-12-15 21:48:07 所属栏目:大数据 来源:网络整理
导读:在 DBI documentation中,这是多次执行查询的推荐代码: $sth = $dbh-prepare_cached($statement);$sth-execute(@bind);$data = $sth-fetchall_arrayref(@attrs);$sth-finish; 但是,我看到许多*查询方法允许传递一个准备好的和缓存的语句句柄来代替查询字符串
在 DBI documentation中,这是多次执行查询的推荐代码:
$sth = $dbh->prepare_cached($statement);
$sth->execute(@bind);
$data = $sth->fetchall_arrayref(@attrs);
$sth->finish;

但是,我看到许多*查询方法允许传递一个准备好的和缓存的语句句柄来代替查询字符串,这使得这成为可能:

$sth = $dbh->prepare_cached($statement);
$data = $dbh->selectall_arrayref($sth,%attrs,@bind);

这种方法有什么问题吗?我还没有看到它在野外使用过.

FWIW,我对这两个实现进行了基准测试.当在第一个实现中使用fetchall_arrayref和在第二个实现中使用selectall_arrayref查询连续两行时,第二种方法显得略微(4%).

* The full list of query methods which support this are:

  • selectrow_arrayref – normal method with prepared statements is fetchrow_arrayref
  • selectrow_hashref – ” ” fetchrow_hashref
  • selectall_arrayref – ” ” fetchall_arrayref
  • selectall_hashref – ” ” fetchall_hashref
  • selectcol_arrayref (doesn’t really count,as it has no parallel method using the first code path as described above – so the only way
    to use prepared statements with this method is to use the second code
    path above)

解决方法

它没有任何问题,只要你打算只进行一次获取.当您使用select * _ *方法时,所有数据都会返回到一个块中.我的DBI代码更像是这样的:
$sth = $dbh->prepare_cached($statement);
$sth->execute(@bind);
while (my $row = $sth->fetch) { # alias for fetchrow_arrayref
  # do something with @$row here
}

使用select * _ *方法没有相当于此的东西.

如果您要调用fetchall_ *(或者您只获取1行),那么继续使用带语句句柄的select * _ *方法.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读