简体   繁体   English

DBIx :: Class :: ResultSet每次迭代项目新查询

[英]DBIx::Class::ResultSet iterate over items new query every time

I am using DBIx::Class::ResultSet to query for items and paginate my data. 我正在使用DBIx :: Class :: ResultSet来查询项目并对我的数据进行分页。 This is the query I use: 这是我使用的查询:

my $clients = $c->model('DB::User')->search(
    { active_yn => 'y', client_yn => 'y' },
    {   
        columns => [qw/id first_name last_name username email/],
        order_by => ['last_name'],
        page     => ($c->req->param('page') || 1), 
        rows     => 20,
    }   
); 

Once, I get the resultset, I loop through the results in Template::Toolkit like so: 有一次,我得到了结果集,我在Template :: Toolkit中循环遍历结果,如下所示:

[% WHILE (client = clients.next) %]
    <tr>
        <td>[% client.first_name %] [% client.last_name %]</td>
    </tr>
[% END %]

My question is, does each time I call next issue a new query? 我的问题是,每次我打电话给next问题新的查询吗? It is unclear to me because the all method says it returns all elements in the set, so does the first query not do that? 我不清楚因为all方法说它返回集合中的所有元素,所以第一个查询不这样做吗? I'd like to be as efficient as possible. 我想尽可能高效。 Thanks! 谢谢!

The answer is no: it will not execute a new query each time you call next , as long as you are working with the same resultset instance. 答案是否定的:只要您使用相同的结果集实例,每次调用next时都不会执行新查询。 It is intended to be a fast way of iterating the records. 它旨在成为迭代记录的快速方法。

Search returns a ResultSet object in scalar and a list of Result objects in list context. Search返回标量中的ResultSet对象和列表上下文中的Result对象列表。 You can force returning a ResultSet by using search_rs. 您可以使用search_rs强制返回ResultSet。

Next will use cursors if your database supports them but only fetches one row per call and constructs the Result object from it. 如果数据库支持游标,则下一步将使用游标,但每次调用只获取一行并从中构造Result对象。

If the number of rows is low, doing a single SQL query and constructing all Result objects at once is faster. 如果行数较少,则执行单个SQL查询并一次构造所有Result对象会更快。 That's usually the case when you paginate. 分页时通常就是这种情况。

Next makes sense when the amount of memory to construct all Results objects would be too large, for example when exporting millions of rows. 当构造所有Results对象的内存量太大时(例如导出数百万行时),下一步是有意义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM