Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unnecessary inner join for MenuMapper.getMenusByRoleIds #65

Open
wtune opened this issue Jan 10, 2020 · 0 comments
Open

Unnecessary inner join for MenuMapper.getMenusByRoleIds #65

wtune opened this issue Jan 10, 2020 · 0 comments

Comments

@wtune
Copy link

wtune commented Jan 10, 2020

The following query defined in MenuMapper.xml is not optimal:

    <select id="getMenusByRoleIds" resultType="cn.stylefeng.guns.base.pojo.node.MenuNode">
        SELECT
        m1.menu_id AS id,
        m1.code AS code,
        m1.icon AS icon,
        (
        CASE
        WHEN (m2.menu_id = 0 OR m2.menu_id IS NULL) THEN
        0
        ELSE
        m2.menu_id
        END
        ) AS "parentId",
        m1.name as name,
        m1.url as url,
        m1.levels as levels,
        m1.menu_flag as ismenu,
        m1.system_type as systemType,
        m1.sort as num
        FROM
        sys_menu m1
        LEFT join sys_menu m2 ON m1.pcode = m2.code
        INNER JOIN (
        SELECT
        menu_id
        FROM
        sys_menu
        WHERE
        menu_id IN (
        SELECT
        menu_id
        FROM
        sys_relation rela
        WHERE
        rela.role_id IN
        <foreach collection="list" index="index" item="i" open="(" separator="," close=")">
            #{i}
        </foreach>
        )
        ) m3 ON m1.menu_id = m3.menu_id
        where m1.menu_flag = 'Y'
        order by levels,m1.sort asc
    </select>

The INNER JOIN for m3 is unnecessary and could be optimized out to the following:

    <select id="getMenusByRoleIds" resultType="cn.stylefeng.guns.base.pojo.node.MenuNode">
        SELECT
        m1.menu_id AS id,
        m1.code AS code,
        m1.icon AS icon,
        (
        CASE
        WHEN (m2.menu_id = 0 OR m2.menu_id IS NULL) THEN
        0
        ELSE
        m2.menu_id
        END
        ) AS "parentId",
        m1.name as name,
        m1.url as url,
        m1.levels as levels,
        m1.menu_flag as ismenu,
        m1.system_type as systemType,
        m1.sort as num
        FROM
        sys_menu m1
        LEFT join sys_menu m2 ON m1.pcode = m2.code
        where m1.menu_flag = 'Y' and
        m1.menu_id IN (
            SELECT menu_id FROM sys_relation rela WHERE rela.role_id IN
                <foreach collection="list" index="index" item="i" open="(" separator="," close=")">
                    #{i}
                </foreach>
        )
        order by levels,m1.sort asc
    </select>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant