我正在做一个具有相当复杂的条件集的find('all')
:
array(
'conditions' => array(
(int) 0 => 'Attempt.test_id = 8',
(int) 1 => 'Attempt.score > 60',
(int) 2 => array(),
(int) 3 => array(
(int) 0 => 'Resume.has_file = 1'
)
),
'joins' => array(
(int) 0 => array(
'table' => 'attempts',
'alias' => 'Attempt',
'type' => 'LEFT',
'conditions' => array(
(int) 0 => 'Attempt.user_id = User.id AND Attempt.test_id != 5'
)
)
),
'contain' => array(
(int) 0 => 'Resume',
(int) 1 => 'Attempt',
(int) 2 => 'Tag'
),
'group' => 'User.id',
'limit' => (int) 1,
'fields' => array(
(int) 0 => 'User.id',
(int) 1 => 'Resume.id'
)
)
返回的数据如下所示:
array(
(int) 0 => array(
'User' => array(
'id' => '381'
),
'Resume' => array(
'id' => '15'
),
'Attempt' => array(
(int) 0 => array(
'id' => '16072',
'user_id' => '381',
'test_id' => '8',
'status' => 'complete'
),
(int) 2 => array(
'id' => '16073',
'user_id' => '381',
'test_id' => '8',
'status' => 'complete'
)
这个查询需要很长时间和一天的时间才能运行,我只需要User.id,所以我试图去掉不需要的字段。它适用于hasOne关联,但hasMany与文档中的语法不匹配。例如,'limit'=>array('Attempt.id')
由于没有$user[Attempt']['id']而抛出错误。相反,它是$user[Attempt'][#]['id']。
我该怎么做?我也很乐意听到任何其他可以加快查询速度的建议。
好的,这是我要做的。
首先,我看到你只需要User.id,但我看到你在contain中添加了更多的关联,因此我会删除"Tag"。
我看到你正在进行左侧类型的手动连接,这是CakePHP默认使用的,是的,我看到你在那里设置了一个附加条件,即使没有任何问题,我也会简单地将飞行中的尝试模型与上述条件绑定,即
$this->User->unbindModel(array('hasMany' => array('Attempt')));
//I dont know if its a hasMany, I just assumed that
$this->User->bindMode(array('hasMany'=> array(
'Attempt' => array('conditions' => array('Attempt.test_id <>' => 5)))));
使用Resume模型重复此操作。
我看到你在分组,但我没有看到任何聚合函数,你为什么在那里分组?你需要什么?可以用Hash::extrat()解决吗?
另一件需要注意的事情是:直接在数据库控制台中运行查询,看看是否有索引可以帮助您。
最后,我不确定你的要求,但这两个条件(在尝试和简历模型中)都应该满足吗?如果是这种情况,那么您需要"内部联接"