【问题标题】:Performance of native sql query and JPA本机 sql 查询和 JPA 的性能
【发布时间】:2020-02-26 11:14:55
【问题描述】:

我有以下方法。第一个是原生 sql,第二个是 JPA 查询。

@Query(value = "SELECT  id                  AS id," +
            "       col1                        AS col1," +
            "       col2                        AS col2," +
            "       col3                        AS col3," +
            "       col4                        AS col4," +
            "       col5                        AS col5," +
            "       col6                        AS col6," +
            "       col7                        AS col7," +
            "       col8                        AS col8," +
            "       col9                        AS col9," +
            "       col10                       AS col10," +
            "       col11                       AS col11," +
            "       col12                       AS col12," +
            "       col13                       AS col13" +
            "FROM foo " +
            "WHERE id = :id AND col1 = :col1", nativeQuery = true)
    List<Foo> findFooByIdAndCol1(@Param("id") final Long id, @Param("col1") final Long col1);
List<FooEntity> findByIdAndCol1(Long id, Long col1);

日志

----Native sql -----
2020-02-26 10:17:04.908 DEBUG 3888 --- [           main] org.hibernate.SQL                        : SELECT id AS id, col1 AS col1, col2 AS col2, col3 AS col3, col4 AS col4, col5 AS col5, col6 AS col6, col7 AS col7, col8 AS col8, col9 AS col9, col10 AS col10, col11 AS col11, col12 AS col12, col13 AS col13 FROM foo WHERE id = ? AND col1 = ?
2020-02-26 10:17:04.915 TRACE 3888 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [1]
2020-02-26 10:17:04.915 TRACE 3888 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [BIGINT] - [1]

-----JPA query -----
2020-02-26 10:17:04.956 DEBUG 3888 --- [           main] org.hibernate.SQL                        : select foo0_.id as id1_0_, foo0_.col1 as col1_0_, foo0_.col2 as col2_3_0_, foo0_.col3 as col3_4_0_, foo0_.col4 as col4_5_0_, foo0_.col5 as col5_6_0_, foo0_.col6 as col6_7_0_, foo0_.col7 as col7_8_0_, foo0_.col8 as col8_9_0_, foo0_.col9 as col9_10_0_, foo0_.col10 as col10_20_0_, foo0_.col11 as col11_11_0_, foo0_.col12 as col12_12_0_, foo0_.col13 as col13_13_0_, foo0_.col14 as col14_14_0_, foo0_.col15 as col15_0_, foo0_.col16 as col16_16_0_, foo0_.col18 as col18_17_0_, foo0_.col19 as col19_18_0_, foo0_.col20 as col120_19_0_ from foo foo0_ where foo0_.id=? and foo0_.col1=?
2020-02-26 10:17:04.956 TRACE 3888 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [1]
2020-02-26 10:17:04.956 TRACE 3888 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [BIGINT] - [1]

看起来当它运行它在日志中发出相同的查询时。我用数据库中的 10k 条记录对此进行了测试。 原生查询:54.7s JPA:74秒

在我的用例中,本机 sql 查询是否比 JPA 更好?或者有什么方法可以提高 JPA 的性能。 (注意:我知道有分页和获取类型等。但这不是我要找的)

【问题讨论】:

    标签: java hibernate jpa spring-data-jpa


    【解决方案1】:

    在 DB 上执行的有效 SQL 应该是相同的。因此,您应该特别注意您测量的内容以及您想要将结果用于什么。

    JPA 旨在将面向对象的数据模型更新和存储到关系数据结构中,因此您不必手动实现集合和关系的映射。

    因此,您的 JPA 查询结果在 EntityManager 中进行管理,并且可以轻松地存储对结果的更改。本机查询只是一个选择,并投影到结果对象中,而不由您的 EntityManager 管理。因此,您使用本机查询消除了 EntityManager 开销,您没有优化 SQL。

    所以第一个问题不是“什么更快?”,而是“你的用例是什么?”。如果要显示只读结果,可以使用本机查询并节省一些开销,从而总体上减少执行时间。如果要将结果更改存储回数据库,则应使用 JPA 查询,并且可能还需要测量存储操作。

    根据用例,选择更简单的代码也是一个有效的选择。

    【讨论】:

    • 完美。那讲得通。我的用例只是从数据库中读取并转换对象。所以我不得不牺牲代码复杂度来换取性能,并使用原生查询。
    猜你喜欢
    • 1970-01-01
    • 2016-11-02
    • 2012-10-09
    • 2015-04-11
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 2020-08-03
    • 1970-01-01
    相关资源
    最近更新 更多