将setParameter与createNativeQuery一起使用非常慢

时间:2019-06-29 13:05:02

标签: java mysql hibernate jpa hibernate-entitymanager

我们正在尝试通过JPA和mysql来提高性能,代码如下:

    private String getProfileIdsSQL = "select up.user_profile_fk, up.Profile_id from account_config.user_profile as up " +
            " where up.user_profile_fk IN (%s)";
.
.
.
            String sql = String.format(getProfileIdsSQL, preparePlaceHolders(foreignKeys.length));
            Query query = em.createNativeQuery(sql);
            for (int i = 0; i < foreignKeys.length; i++) {
                query.setParameter("p" + i, foreignKeys[i]);
            }
.
.
.
    protected static String preparePlaceHolders(int length) {
        if (length == 0)
            return "";
        int capacity = length * 4;
        StringBuilder ans = new StringBuilder(capacity);
        for (int i = 0; i < length; i++) {
            ans.append(":p").append(i).append(",");
        }
        ans.setLength(ans.length() - 1);
        return ans.toString();
    }

运行非常缓慢,但是如果我跳过参数绑定并在没有setParameter的情况下在查询字符串中写入值,结果将非常快,但我们无法在生产中做到这一点,我们如何制作{{1} }工作更快?

谢谢。

0 个答案:

没有答案