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%).
解决方法
它没有任何问题,只要你打算只进行一次获取.当您使用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 * _ *方法. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |