DBIx :: Class连接生成大量的select语句

时间:2014-03-21 13:19:34

标签: perl join dbix-class prefetch

我注意到当我在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。

1 个答案:

答案 0 :(得分:0)

您的答案取决于DBIx::Class::Manual::JoiningDBIx::Class::ResultSet文档中描述的预取属性。

添加

  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,

查询属性hash。