MYSQL内部加入速度问题

时间:2015-05-11 22:24:14

标签: php mysql inner-join

我一直有这个问题的大问题。

它是一个每24小时由cron运行的文件,并对数据库中的所有成员运行条件检查。

显然它崩溃了MYSQL服务器(需要2个小时才能执行?)。

每个表都有行foo_id&行上设置的主键。 foo_uid所以联接在PRIMARY下索引,因此应该有利于速度。

请帮助,这是在杀我。

  $members = new WA_MySQLi_RS("members", $alpha, 1);
    $members->setQuery("SELECT
    registration.*,
    child_base_survey.*,
    child_base_scas.*,
    child_base_smqf.*,
    parent_base_survey.*,
    parent_base_ippa.*,
    parent_base_eac.*,
    parent_base_scas.*,
    parent_base_smqf.*,
    parent_base_eval.*,
    user_access_level.*,
    parent_one_month_survey.*,
    parent_one_month_ippa.*,
    parent_one_month_eac.*,
    parent_one_month_eval.*,
    child_three_month_survey.*,
    child_three_month_scas.*,
    child_three_month_smqf.*,
    parent_three_month_survey.*,
    parent_three_month_scas.*,
    parent_three_month_smqf.*,
    parent_three_month_eval.*,
    cron.*
    FROM registration
    INNER JOIN child_base_survey ON registration.rego_parent_uid = child_base_survey.child_base_survey_uid
    INNER JOIN child_base_scas ON child_base_survey.child_base_survey_uid = child_base_scas.child_base_scas_uid
    INNER JOIN child_base_smqf ON child_base_scas.child_base_scas_uid = child_base_smqf.child_base_smqf_uid
    INNER JOIN parent_base_survey ON child_base_smqf.child_base_smqf_uid = parent_base_survey.parent_base_survey_uid
    INNER JOIN parent_base_ippa ON parent_base_survey.parent_base_survey_uid = parent_base_ippa.parent_base_ippa_uid
    INNER JOIN parent_base_eac ON parent_base_ippa.parent_base_ippa_uid = parent_base_eac.parent_base_eac_uid
    INNER JOIN parent_base_scas ON parent_base_eac.parent_base_eac_uid = parent_base_scas.parent_base_scas_uid
    INNER JOIN parent_base_smqf ON parent_base_scas.parent_base_scas_uid = parent_base_smqf.parent_base_smqf_uid
    INNER JOIN parent_base_eval ON parent_base_smqf.parent_base_smqf_uid = parent_base_eval.parent_base_eval_uid
    INNER JOIN user_access_level ON parent_base_eval.parent_base_eval_uid = user_access_level.user_access_level_uid
    INNER JOIN parent_one_month_survey ON user_access_level.user_access_level_uid = parent_one_month_survey.parent_one_month_survey_uid
    INNER JOIN parent_one_month_ippa ON parent_one_month_survey.parent_one_month_survey_uid = parent_one_month_ippa.parent_one_month_ippa_uid
    INNER JOIN parent_one_month_eac ON parent_one_month_ippa.parent_one_month_ippa_uid = parent_one_month_eac.parent_one_month_eac_uid
    INNER JOIN parent_one_month_eval ON parent_one_month_eac.parent_one_month_eac_uid = parent_one_month_eval.parent_one_month_eval_uid
    INNER JOIN child_three_month_survey ON parent_one_month_eval.parent_one_month_eval_uid = child_three_month_survey.child_three_month_survey_uid
    INNER JOIN child_three_month_scas ON child_three_month_survey.child_three_month_survey_uid = child_three_month_scas.child_three_month_scas_uid
    INNER JOIN child_three_month_smqf ON child_three_month_scas.child_three_month_scas_uid = child_three_month_smqf.child_three_month_smqf_uid
    INNER JOIN parent_three_month_survey ON child_three_month_smqf.child_three_month_smqf_uid = parent_three_month_survey.parent_three_month_survey_uid
    INNER JOIN parent_three_month_scas ON parent_three_month_survey.parent_three_month_survey_uid = parent_three_month_scas.parent_three_month_scas_uid
    INNER JOIN parent_three_month_smqf ON parent_three_month_scas.parent_three_month_scas_uid = parent_three_month_smqf.parent_three_month_smqf_uid
    INNER JOIN parent_three_month_eval ON parent_three_month_smqf.parent_three_month_smqf_uid = parent_three_month_eval.parent_three_month_eval_uid
    INNER JOIN cron ON parent_three_month_eval.parent_three_month_eval_uid = cron.cron_uid WHERE registration.rego_parent_uid = ?");
    $members->bindParam("s", "" . ((isset($_SESSION["rego_parent_uid"])) ? $_SESSION["rego_parent_uid"] : "") . "", "-1"); //WAQB_Param1
    $members->execute();
    ?>

1 个答案:

答案 0 :(得分:1)

将它们作为单独的查询运行,从粗略的一瞥,看起来你只是加入每个可能的表,其中的数据与id值的引用相关联。您的结果将成为每个表中每个匹配行的叉积。对于N个表,表X中的每一行将重复r0 * r1 * r2 * .... rX-1 * rX + 1 * ... rN。在那里有23个表,如果每个表只有2行,你的结果中几乎有 840万行。