1

以下のコードで EXPLAIN を正確に使用する場所と、このコードを高速化/最適化する方法を提案できますか (jobseeker テーブルと jos_users テーブルの 400 万行の場合)

$query =  " SELECT DISTINCT a.*,b.name,dl.id id_deg,dl.degree_level,
               (SELECT r.hits FROM #__jbjobs_resume_view r WHERE r.jseeker_id=a.user_id AND r.employer_id=$user->id) AS hits, 
               (SELECT featured FROM #__jbjobs_resume WHERE is_active='y' AND featured=1 AND featured_expire > NOW() AND jseeker_id=a.user_id) as featuredres FROM #__jbjobs_jobseeker a".
              " LEFT JOIN #__users b ON a.user_id=b.id ".
              " LEFT JOIN #__jbjobs_degree_level dl ON a.id_degree_level = dl.id".
              " LEFT JOIN #__jbjobs_custom_field_value c ON c.userid=a.user_id".
              " WHERE ".$where."AND b.block='0'".
              " ORDER BY featuredres DESC";

構造 1 構造体2struct3

4

1 に答える 1

2

resume_view.hits と resume.featured は 0 行または 1 行しか返さないのですか? もしそうなら、LEFT JOINを介してすべて実行できるはずです。行がゼロの場合、値は NULL になります。

SELECT DISTINCT a.*,b.name,dl.id id_deg,dl.degree_level, 
      r.hits,  
      r2.featured AS featuredres
 FROM #__jbjobs_jobseeker a 
    LEFT JOIN #__users b ON a.user_id=b.id  
    LEFT JOIN #__jbjobs_degree_level dl ON a.id_degree_level = dl.id 
    LEFT JOIN #__jbjobs_custom_field_value c ON c.userid=a.user_id 

    LEFT JOIN #__jbjobs_resume_view r ON r.jseeker_id=a.user_id AND r.employer_id = $user_id
    LEFT JOIN #__jbjobs_resume r2 ON r2.is_active='y' AND r2.featured=1 AND r2.featured_expire > NOW() AND r2.jseeker_id=a.user_id

    WHERE $where AND b.block='0' 
    ORDER BY featuredres DESC
于 2012-09-11T04:38:20.387 に答える