1

我在下面的代码中到底在哪里使用 EXPLAIN 并且任何人都可以建议如何加速/优化此代码(对于 jos_users 表中的 400 万行 Jobseeker 表)

$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 结构2结构3

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 回答