查询速度极慢

时间:2014-04-16 02:56:23

标签: mysql performance

我正在运行以下查询,但它极其缓慢。任何人都对如何优化它以提高性能有任何建议。

主用户表只有2700行。

查询是:

SELECT
    (
        SELECT
            core_org_chart.translation
        FROM
            core_org_chart
        WHERE
            (
                core_org_chart.id_dir = t1.idOrg
            )
    ) AS region,
    (
        SELECT
            core_org_chart.translation
        FROM
            core_org_chart
        WHERE
            (
                core_org_chart.id_dir = t2.idOrg
            )
    ) AS level1,
    (
        SELECT
            core_org_chart.translation
        FROM
            core_org_chart
        WHERE
            (
                core_org_chart.id_dir = t3.idOrg
            )
    ) AS level2,
    (
        SELECT
            core_org_chart.translation
        FROM
            core_org_chart
        WHERE
            (
                core_org_chart.id_dir = t4.idOrg
            )
    ) AS level3,
    core_user.firstname AS firstname,
    core_user.lastname AS lastname,
    core_user.email AS email,
    core_user.register_date AS register_date,
    core_user.lastenter AS lastenter,
    (
        SELECT
            core_field_son.translation
        FROM
            (
                core_field_son
                JOIN core_field_userentry ON (
                    (
                        core_field_userentry.user_entry = core_field_son.idSon
                    )
                )
            )
        WHERE
            (
                (
                    core_field_userentry.id_user = core_user.idst
                )
                AND (
                    core_field_userentry.id_common = 4
                )
            )
    ) AS Gender,
    (
        SELECT
            core_field_son.translation
        FROM
            (
                core_field_son
                JOIN core_field_userentry ON (
                    (
                        core_field_userentry.user_entry = core_field_son.idSon
                    )
                )
            )
        WHERE
            (
                (
                    core_field_userentry.id_user = core_user.idst
                )
                AND (
                    core_field_userentry.id_common = 6
                )
            )
    ) AS Race,

IF (
    (core_user.valid = 1),
    'Active',
    'Suspended'
) AS UserStatus,
 (
    SELECT
        jet_designations.designation
    FROM
        jet_designations
    WHERE
        (
            jet_designations.id = core_user.designation
        )
) AS UserDesignation,
 (
    SELECT
        jet_designations.designation
    FROM
        jet_designations
    WHERE
        (
            jet_designations.id = core_user.reports_to
        )
) AS Manager
FROM
    (
        (
            (
                (
                    (
                        core_org_chart_tree t1
                        LEFT JOIN core_org_chart_tree t2 ON (
                            (
                                t2.idParent = t1.idOrg
                            )
                        )
                    )
                    LEFT JOIN core_org_chart_tree t3 ON (
                        (
                            t3.idParent = t2.idOrg
                        )
                    )
                )
                LEFT JOIN core_org_chart_tree t4 ON (
                    (
                        t4.idParent = t3.idOrg
                    )
                )
            )
            JOIN core_group_members ON (
                (
                    core_group_members.idst =
                    IF (
                        isnull(t2.idOrg),
                        t1.idst_ocd,

                    IF (
                        isnull(t3.idOrg),
                        t2.idst_ocd,

                    IF (
                        isnull(t4.idOrg),
                        t3.idst_ocd,
                        t4.idst_ocd
                    )
                    )
                    )
                )
            )
        )
        JOIN core_user ON (
            (
                core_user.idst = core_group_members.idstMember
            )
        )
    )
WHERE
    (t1.lev = 1)

1 个答案:

答案 0 :(得分:1)

您在一个中使用多个查询,加入它们将提高性能。