问题描述
我知道:
-
$ sth-&f; fetchrow_hashref
返回一个从数据库中获取的行的hashref, -
$ sth-> fetchrow_arrayref
返回从数据库中获取的行的arrayref,并且 -
$ sth-> fetchrow_array
返回从数据库中获取的行的数组。
$sth->fetchrow_hashref
returns a hashref of the fetched row from database,$sth->fetchrow_arrayref
returns an arrayref of the fetched row from database, and$sth->fetchrow_array
returns an array of the fetched row from database.
但是我想知道有关这些的最佳实践。
But I want to know best practices about these. When should we use fetchrow_hashref and when should we use fetchrow_arrayref and when should we use fetchrow_array?
推荐答案
当我为$ work写YAORM的时候,我在我们的环境(MySQL)中对所有这些进行了基准测试,发现arrayref的性能与array相同,而hashref的速度要慢许多。因此,我同意,最好尽可能使用array *。它有助于加糖您的应用程序以了解它正在处理的列名称。另外,获取的列越少越好,因此请尽可能避免使用 SELECT *
语句-直接进行 SELECT的操作 ;
。
When I wrote YAORM for $work, I benchmarked all of these in our environment (MySQL) and found that arrayref performed the same as array, and hashref was much slower. So I agree, it is best to use array* whenever possible; it helps to sugar your application to know which column names it is dealing with. Also the fewer columns you fetch the better, so avoid SELECT *
statements as much as you can - go directly for SELECT <just the field I want>
.
但这仅适用于企业应用程序。如果您要执行的任务不是时间紧迫的,请采用最容易使用的格式来显示数据。请记住,在开始完善应用程序之前,效率是 programmer 而不是机器最快的。开始花费比您编写代码更多的时间来执行应用程序,这需要花费数百万次执行。
But this only applies to enterprise applications. If you are doing something that is not time-critical, go for whichever form presents the data in a format you can most easily work with. Remember, until you start refining your application, efficiency is what is fastest for the programmer, not for the machine. It takes many millions of executions of your application to start saving more time than you spent writing the code.
这篇关于什么时候使用$ sth-> fetchrow_hashref,$ sth-> fetchrow_arrayref和$ sth-> fetchrow_array?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!