ネイティブSQLクエリとJPAのパフォーマンス

Drkrcrov:

私は、次の方法があります。一つ目はネイティブSQLで2つ目は、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:74S

ネイティブSQLクエリは、私のユースケースでは、より良いJPAよりますか?またはJPAのパフォーマンスを改善するための任意の道があります。(注:私は改ページがあると知って、種類などをフェッチしかし、それは私が探しています何ではありません)

模擬:

DB上で実行効果的なSQLは同じである必要があります。だから、正確に何を測定し、どのようなあなたのために結果を使用するために気を付ける必要があります。

JPAは、更新のために設計され保存手動コレクションと関係のマッピングを実装する必要はありませんので、リレーショナルデータ構造にオブジェクト指向データモデルを。

したがって、あなたのJPAクエリの結果は、EntityManagerの中で管理され、検索結果への変更は簡単に背中を保存することができます。ネイティブクエリがちょうどある場合は、あなたのEntityManagerによって管理されていない結果オブジェクト、に投影して選択します。あなたはネイティブクエリでオーバーヘッドEntityManagerを取り除くので、あなたは、SQLを最適化していません。

最初の質問ではありませんので、「あなたのユースケースが何であるか?」「何?高速である」、しかし。表示したい場合は結果のみを読んで、あなたはネイティブクエリで行くと、すべての上での実行時間が短縮され、いくつかのオーバーヘッドを保存することができます。あなたが戻ってDBへの検索結果への変更を保存したい場合は、JPAクエリに行く必要があります。また、店舗運営を測定したい場合があります。

ユースケースによっては、また、より単純なコードのために行くための有効なオプションです。

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=8239&siteId=1