我注意到,当我在DBIx :: Class中的select中进行联接时,会得到大量的SELECT语句。例如,我有一个查询,看起来像:
my $testSetRs = $db->resultset('Testset')->search(
{%searchCriteria},
{
prefetch => [
{
'ax_group_testsets' => {'ax_group' => 'ax_user_groups'},
},
{'ax_user_testsets' => 'ax_user'},
],
'+columns' => [
{
'me.ax_group_testsets.ax_group_id' => 'ax_group_testsets.ax_group_id',
'me.ax_group_testsets.ax_group.public' => 'ax_group.public',
'me.ax_group_testsets.ax_group.name' => 'ax_group.name',
},
],
collapse => 1,
group_by => ['me.ts_id', 'me.ts_name'],
cache => 1,
}
);
产生:
SELECT me.ts_id, me.ts_name
FROM testset me
LEFT JOIN ax_group_testset ax_group_testsets ON ax_group_testsets.ts_id = me.ts_id
LEFT JOIN ax_group ax_group ON ax_group.ax_group_id = ax_group_testsets.ax_group_id
LEFT JOIN ax_user_group ax_user_groups ON ax_user_groups.ax_group_id = ax_group.ax_group_id
LEFT JOIN ax_user_testset ax_user_testsets ON ax_user_testsets.ts_id = me.ts_id
LEFT JOIN ax_user ax_user ON ax_user.ax_user_id = ax_user_testsets.ax_user_id
WHERE ( ( ax_user.ax_user_id = ? OR ax_user_groups.ax_user_id = ? ) )
GROUP BY me.ts_id, me.ts_name: '349', '349'
SELECT me.ax_group_id, ax_group.public, ax_group.name FROM ax_group_testset me JOIN ax_group ax_group ON ax_group.ax_group_id = me.ax_group_id WHERE ( me.ts_id = ? ): '4476'
SELECT me.ax_group_id, ax_group.public, ax_group.name FROM ax_group_testset me JOIN ax_group ax_group ON ax_group.ax_group_id = me.ax_group_id WHERE ( me.ts_id = ? ): '4566'
SELECT me.ax_group_id, ax_group.public, ax_group.name FROM ax_group_testset me JOIN ax_group ax_group ON ax_group.ax_group_id = me.ax_group_id WHERE ( me.ts_id = ? ): '4701'
等等
我想要第一个SELECT。它之后的那些困扰着我。每组有一个(超过40个)。这在我所有的联接上都会发生,并且可以通过正确的查询跳入数百个联接。为什么会生成这么多查询,我该怎么办?
我正在运行Perl ActiveState 5.16和DBIx .08250。
最佳答案
您的答案在于DBIx::Class::Manual::Joining和DBIx::Class::ResultSet文档中所述的prefetch属性。
加
prefetch => {'ax_group_testsets' => {'ax_group' => 'ax_user_groups'} },
并且,假设您想要所有三个相关记录列:
'+columns' => [
{
'me.ax_group_testsets.ax_group_id' => 'ax_group_testsets.ax_group_id',
'me.ax_group_testsets.ax_group.public' => 'ax_group.public',
'me.ax_group_testsets.ax_group.name' => 'ax_group.name',
},
],
collapse => 1,
查询属性哈希值。
关于perl - DBIx::Class联接产生大量的选择语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22559937/