如何拆分activerecord查询以提高速度

时间:2014-07-24 12:31:53

标签: php mysql codeigniter activerecord

任何帮助都会非常感激。

我有一个正在执行的活动记录查询,由于表中有很多记录,因此运行速度非常慢。

我正在考虑将函数拆分为几个单独的查询,然后再加入它们。虽然我不确定最好的方法。我目前正在搜索用户和个人资料表两次,所以我现在可以做一些改进。

我已经附上了下面的activerecord查询,如果我没有说清楚,请告诉我,我会编辑我的问题。

提前致谢。

$this->db->select(
    'stage_one_application.id as application_id,
    stage_one_application.proposal_title as title, 
    stage_one_application.proposal_abstract, 
    category.faq_category_title as category,
    stage_one_application.proposal_category_alt 
        AS category_freetext, 
    stage_one_application.pooling as pool, 
    user.id as user_id, 
    profile.first_name, 
    profile.last_name, 
    profile.date_of_birth, 
    stage_one_application.admin_feasibility 
        AS feasibility_score, 
    stage_one_application.admin_innovation 
        AS innovation_score, 
    stage_one_application.admin_technical_excellence 
        AS technical_excellence_score, 
    stage_one_application.application_accepted 
        AS through_to_stage_two, 
    rev_profile.first_name AS reviewer_firstname, 
    rev_profile.last_name as reviewer_lastname, 
    application_stage_one_scoring.innovation 
        AS reviewer_score_innovation, 
    application_stage_one_scoring.feasibility 
        AS reviewer_score_feasibility, 
    application_stage_one_scoring.technical_excellence 
        AS reviewer_score_technical_excellence,
    application_stage_one_scoring.application_stage_one_reviewer_comment 
        AS reviewer_comment'
);
$this->db->from('stage_one_application');
$this->db->join('users as user', 'user.id = stage_one_application.candidate_id');
$this->db->join(
    'profiles as profile', 
    'profile.user_id = stage_one_application.candidate_id'
);
$this->db->join(
    'faq_categories as category', 
    'category.id = stage_one_application.proposal_category'
);
$this->db->join(
    'application_stage_one_scoring',
    'application_stage_one_scoring.application_stage_one_id = stage_one_application.id',
    'left'
);
$this->db->join(
    'users as reviewer', 
    'reviewer.id = application_stage_one_scoring.application_stage_one_scoring_reviewer'
);
$this->db->join(
'profiles as rev_profile', 
'rev_profile.user_id=application_stage_one_scoring.application_stage_one_scoring_reviewer'
);
$this->db->order_by("application_id", "desc"); 
$query = $this->db->get();

1 个答案:

答案 0 :(得分:0)

我认为这不是CodeIgniter问题。您需要优化您的数据库,因为在每个查询中SQL-server都会创建大型临时表(加入过程)。尝试检查INDEXes,如果它们使用正确,请在数据库中创建VIEW并询问您需要的内容。