需要针对每个分支中具有不同Limit设置的多个Union进行优化

时间:2014-06-03 04:43:32

标签: mysql sql

我有一个类似这种结构的查询。有多个联合对RTL得分的不同限制。需要帮助进行优化,因为它非常大且未经优化。请帮助。

SELECT *
FROM (
    SELECT pcd.FIRST_NAME, pcd.LAST_NAME, pcd.HEADLINE, pp.COMPANY, pcd.LOCATION, pcd.RTL_SCORE, pcd.COUNTRY, pu.SKILLS, IF (
            LENGTH(pcd.PICTURE_URL) = 0
            OR pcd.PICTURE_URL IS NULL, NULL, pcd.PICTURE_URL
            ) AS PICTURE_URL, pcd.PUBLIC_URL, 'I', pu.USER_ID, pu.INSERT_TIME FROM DEMO_UPLOADED_CANDIDATE pu INNER JOIN DEMO_CANDIDATES_DATA pcd ON (
            pu.PUBLIC_URL = pcd.PUBLIC_URL
            AND pcd.RTL_SCORE = 98
            ) LEFT JOIN (
        SELECT *
        FROM (
            SELECT *
            FROM DEMO_CANDIDATES_PROFESSIONAL_DATA
            ORDER BY start_date, pcd_id
            ) s
        GROUP BY pcd_id
        ) pp ON (pp.pcd_id = pcd.pcd_id) LIMIT 10

    UNION

        SELECT pcd.FIRST_NAME, pcd.LAST_NAME, pcd.HEADLINE, pp.COMPANY, pcd.LOCATION, pcd.RTL_SCORE, pcd.COUNTRY, pu.SKILLS,
        IF (
            LENGTH(pcd.PICTURE_URL) = 0
            OR pcd.PICTURE_URL IS NULL, NULL, pcd.PICTURE_URL
            ) AS PICTURE_URL, pcd.PUBLIC_URL, 'I', pu.USER_ID, pu.INSERT_TIME FROM DEMO_UPLOADED_CANDIDATE pu INNER JOIN DEMO_CANDIDATES_DATA pcd ON (
            pu.PUBLIC_URL = pcd.PUBLIC_URL
            AND pcd.RTL_SCORE = 97
            ) LEFT JOIN (
        SELECT *
        FROM (
            SELECT *
            FROM DEMO_CANDIDATES_PROFESSIONAL_DATA
            ORDER BY start_date, pcd_id
            ) s
        GROUP BY pcd_id
        ) pp ON (pp.pcd_id = pcd.pcd_id) LIMIT 15

    UNION

        SELECT pcd.FIRST_NAME, pcd.LAST_NAME, pcd.HEADLINE, pp.COMPANY, pcd.LOCATION, pcd.RTL_SCORE, pcd.COUNTRY, pu.SKILLS,
        IF (
            LENGTH(pcd.PICTURE_URL) = 0
            OR pcd.PICTURE_URL IS NULL, NULL, pcd.PICTURE_URL
            ) AS PICTURE_URL, pcd.PUBLIC_URL, 'I', pu.USER_ID, pu.INSERT_TIME FROM DEMO_UPLOADED_CANDIDATE pu INNER JOIN DEMO_CANDIDATES_DATA pcd ON (
            pu.PUBLIC_URL = pcd.PUBLIC_URL
            AND pcd.RTL_SCORE = 96
            ) LEFT JOIN (
        SELECT *
        FROM (
            SELECT *
            FROM DEMO_CANDIDATES_PROFESSIONAL_DATA
            ORDER BY start_date, pcd_id
            ) s
        GROUP BY pcd_id
        ) pp ON (pp.pcd_id = pcd.pcd_id) LIMIT 20

    UNION

        SELECT pcd.FIRST_NAME, pcd.LAST_NAME, pcd.HEADLINE, pp.COMPANY, pcd.LOCATION, pcd.RTL_SCORE, pcd.COUNTRY, pu.SKILLS,
        IF (
            LENGTH(pcd.PICTURE_URL) = 0
            OR pcd.PICTURE_URL IS NULL, NULL, pcd.PICTURE_URL
            ) AS PICTURE_URL, pcd.PUBLIC_URL, 'I', pu.USER_ID, pu.INSERT_TIME FROM DEMO_UPLOADED_CANDIDATE pu INNER JOIN DEMO_CANDIDATES_DATA pcd ON (
            pu.PUBLIC_URL = pcd.PUBLIC_URL
            AND pcd.RTL_SCORE = 95
            ) LEFT JOIN (
        SELECT *
        FROM (
            SELECT *
            FROM DEMO_CANDIDATES_PROFESSIONAL_DATA
            ORDER BY start_date, pcd_id
            ) s
        GROUP BY pcd_id
        ) pp ON (pp.pcd_id = pcd.pcd_id) LIMIT 25

    UNION

        SELECT pcd.FIRST_NAME, pcd.LAST_NAME, pcd.HEADLINE, pp.COMPANY, pcd.LOCATION, pcd.RTL_SCORE, pcd.COUNTRY, pu.SKILLS,
        IF (
            LENGTH(pcd.PICTURE_URL) = 0
            OR pcd.PICTURE_URL IS NULL, NULL, pcd.PICTURE_URL
            ) AS PICTURE_URL, pcd.PUBLIC_URL, 'I', pu.USER_ID, pu.INSERT_TIME FROM DEMO_UPLOADED_CANDIDATE pu INNER JOIN DEMO_CANDIDATES_DATA pcd ON (
            pu.PUBLIC_URL = pcd.PUBLIC_URL
            AND pcd.RTL_SCORE = 94
            ) LEFT JOIN (
        SELECT *
        FROM (
            SELECT *
            FROM DEMO_CANDIDATES_PROFESSIONAL_DATA
            ORDER BY start_date, pcd_id
            ) s
        GROUP BY pcd_id
        ) pp ON (pp.pcd_id = pcd.pcd_id) LIMIT 30

    UNION

        SELECT pcd.FIRST_NAME, pcd.LAST_NAME, pcd.HEADLINE, pp.COMPANY, pcd.LOCATION, pcd.RTL_SCORE, pcd.COUNTRY, pu.SKILLS,
        IF (
            LENGTH(pcd.PICTURE_URL) = 0
            OR pcd.PICTURE_URL IS NULL, NULL, pcd.PICTURE_URL
            ) AS PICTURE_URL, pcd.PUBLIC_URL, 'I', pu.USER_ID, pu.INSERT_TIME FROM DEMO_UPLOADED_CANDIDATE pu INNER JOIN DEMO_CANDIDATES_DATA pcd ON (
            pu.PUBLIC_URL = pcd.PUBLIC_URL
            AND pcd.RTL_SCORE = 93
            ) LEFT JOIN (
        SELECT *
        FROM (
            SELECT *
            FROM DEMO_CANDIDATES_PROFESSIONAL_DATA
            ORDER BY start_date, pcd_id
            ) s
        GROUP BY pcd_id
        ) pp ON (pp.pcd_id = pcd.pcd_id) LIMIT 35

    UNION

        SELECT pcd.FIRST_NAME, pcd.LAST_NAME, pcd.HEADLINE, pp.COMPANY, pcd.LOCATION, pcd.RTL_SCORE, pcd.COUNTRY, pu.SKILLS,
        IF (
            LENGTH(pcd.PICTURE_URL) = 0
            OR pcd.PICTURE_URL IS NULL, NULL, pcd.PICTURE_URL
            ) AS PICTURE_URL, pcd.PUBLIC_URL, 'I', pu.USER_ID, pu.INSERT_TIME FROM DEMO_UPLOADED_CANDIDATE pu INNER JOIN DEMO_CANDIDATES_DATA pcd ON (
            pu.PUBLIC_URL = pcd.PUBLIC_URL
            AND pcd.RTL_SCORE = 92
            ) LEFT JOIN (
        SELECT *
        FROM (
            SELECT *
            FROM DEMO_CANDIDATES_PROFESSIONAL_DATA
            ORDER BY start_date, pcd_id
            ) s
        GROUP BY pcd_id
        ) pp ON (pp.pcd_id = pcd.pcd_id) LIMIT 40

    UNION

        SELECT pcd.FIRST_NAME, pcd.LAST_NAME, pcd.HEADLINE, pp.COMPANY, pcd.LOCATION, pcd.RTL_SCORE, pcd.COUNTRY, pu.SKILLS,
        IF (
            LENGTH(pcd.PICTURE_URL) = 0
            OR pcd.PICTURE_URL IS NULL, NULL, pcd.PICTURE_URL
            ) AS PICTURE_URL, pcd.PUBLIC_URL, 'I', pu.USER_ID, pu.INSERT_TIME FROM DEMO_UPLOADED_CANDIDATE pu INNER JOIN DEMO_CANDIDATES_DATA pcd ON (
            pu.PUBLIC_URL = pcd.PUBLIC_URL
            AND pcd.RTL_SCORE = 91
            ) LEFT JOIN (
        SELECT *
        FROM (
            SELECT *
            FROM DEMO_CANDIDATES_PROFESSIONAL_DATA
            ORDER BY start_date, pcd_id
            ) s
        GROUP BY pcd_id
        ) pp ON (pp.pcd_id = pcd.pcd_id) LIMIT 45 ) AS T GROUP BY PUBLIC_URL ORDER BY RTL_SCORE DESC, FIRST_NAME, LAST_NAME;
    )

0 个答案:

没有答案