JPAã¨N+1åé¡
JPAã¨ã¯
JPAï¼Java Persistence APIï¼ã¨ã¯JavaEEã®ããã«å®ç¾©ãããæ°¸ç¶åï¼persistenceï¼ã«é¢ããAPIä»æ§ã§ããJPAã¯APIä»æ§ãªã®ã§JPAåä½ã§ã¯åãã¾ãããJPAãå®è£ ããHibernateãEclipseLinkãªã©ã®O/R Mapperãå¿ è¦ã«ãªãã¾ãã
N+1åé¡ã¨ã¯
N+1åé¡ã¨ã¯O/R Mapperãå©ç¨ããéã«SQLæãæå³ãã大éã«çºè¡ããã¦ãã¾ãåé¡ã§ãããã®åé¡ã¯O/R Mapperã«ããSQLæã®èªåçæã«èµ·å ãã¾ããObjectå´ã®é¢é£ãå«ãã¦ãã¼ã¿ãåå¾ããå ´åã«ãO/R Mapperã¯ä¸è¨ã®ããã«N+1åSELECTæãçºè¡ãã¦ãã¾ãã¾ãã
- TableããNåã®Recordãåå¾ããããã«SELECTæã1åçºè¡
- Nåã®Recordãé¢é£ãããã¼ã¿ãåå¾ããããã«SELECTæã1åãã¤ãè¨Nåçºè¡
N+1åé¡ã®è§£æ±ºç
N+1åé¡ã解決ããã«ã¯ãã¼ãã«ãJOINãã¦ãã¼ã¿ãåå¾ããå¿ è¦ãããã¾ããå ·ä½çãªè§£æ±ºçã¯å¤§ããåãã¦2ã¤ã§ãã
- JPQLã§JOIN FETCHã使ç¨ãã
- @Fetchã¢ããã¼ã·ã§ã³ã使ç¨ãã
ãã ããå¾è ã®æ¹ã¯JPAã®ä»æ§ã«ã¯ãªããå®è£ ã§ããHibernateã«ä¾åãã¾ããEclipseLinkã®å ´åã¯@JoinFetchã使ç¨ãã¾ãã
注æï¼ä¸è¨ã§èª¬æãã解決çã¯JPA + Hibernateç°å¢ã®å ´åã®ãã®ã§ããEclipseLinkã使ç¨ããå ´åã§ãåæ§ã®æ¹æ³ã§è§£æ±ºã§ããã¨ã¯éãã¾ããã
1. JPQLã§JOIN FETCH ã使ç¨ãã
JPAã§ã¯ä¸è¨ã®4ã¤ã®æ¹æ³ã§DBã«ã¢ã¯ã»ã¹ãããã¨ãã§ãã¾ãã
- Entityã®ã¡ã½ãã(persist(), find(), remove(), merge())
- JPQLï¼JPAç¨ã®SQLãSQLæã®æ¹è¨ãå¸åããããã®ãã®ï¼
- Criteriaï¼Javaã®åãå©ç¨ãã¦ã¯ã¨ãªãä½æããããã®ãã®ï¼
- Native SQL
ãã®ä¸ã®JPQLã使ã£ã解決çã§ããJPQLã§ã§ãããã¨ã¯Criteiaã§ãã§ãã¾ãããCriteriaã§ã®è§£æ±ºæ¹æ³ã¯ããã§ã¯è§¦ãã¾ããã
åæ¹åé¢é£ã®å ´å
N+1åé¡ã¯é¢é£ãããããã«çºçãã¾ããããã§è¨ãé¢é£ã¯Objectå´ã®é¢é£ã§ãã å ·ä½çã«ã¯ä¸è¨ã®ãããªã³ã¼ãã§ãã
public class Parent { private List<Child> children; ... } public class Child { ... }
ä¸è¨ã§ã¯Parentã ããChildãåç §ãã¦ããããåæ¹åé¢é£ã¨å¼ã³ã¾ããããã«Parentã¯ChildãListåã§è¤æ°æã£ã¦ããããã1対å¤ï¼One to Manyï¼ã®é¢é£ã¨å¼ã³ã¾ãã
åæ¹åé¢é£ã®å ´åã«N+1åé¡ãçºçããã®ã¯ãParentã¨é¢é£ãã¦ããchildrenãO/R Mapperãåå¾ããã¨ãã§ãããããã£ã¦ãLazy Fetchã®å ´åã¯Parentãªãã¸ã§ã¯ããåå¾ããã ãã§ã¯SELECTæã¯çºè¡ããã¾ãããParentãªãã¸ã§ã¯ãããchildrenãåå¾ãããã¨ããå ´åã«åãã¦Nåã®SELECTæãçºè¡ããã¾ãã
JPQLã§ä¸è¨ã®ããã«JOIN FETCH
ã使ããã¨ã§N+1åé¡ã解決ã§ãã¾ãã
SELECT p FROM Parent p JOIN FETCH p.children
ãã®ã¨ãçºè¡ãããSELECTæã¯1åã§ããJOIN FETCH
ã¯INNER JOINã®SELECTæã¨ãã¦çºè¡ããã¾ããOUTER JOINã使ç¨ãããå ´åã¯ä¸è¨ã®ããã«ãªãã¾ãã
SELECT p FROM Parent p LEFT OUTER JOIN FETCH p.children
åæ¹åé¢é£ã®å ´å
åæ¹åé¢é£ã¨ã¯2ã¤ã®ãªãã¸ã§ã¯ãããäºãã«åç
§ãã¦ããç¶æ
ã®ãã¨ã§ãã
å
·ä½çã«ã¯ä¸è¨ã®ãããªã³ã¼ãã§ãã
public class Parent { private List<Child> children; // One to Many ... } public class Child { private Parent parent; // Many to One ... }
ä¸è¨ã¯1対å¤ã®é¢é£ã§ãããChildããè¦ãå ´åã¯å¤å¯¾1ï¼Many to Oneï¼ã®é¢é£ã¨å¼ã³ã¾ãã
åæ¹åé¢é£ã®å ´åã§ã¯Parentãåå¾ããå ´åã®N+1åé¡ã ããèããã°è¯ãã£ãã®ã§ãããåæ¹åé¢é£ã®å ´åã¯Childãåå¾ããå ´åã®N+1åé¡ãèããªããã°ããã¾ãããChildã¯Parentãä¿æãã¦ããããã®Parentã¯Childãè¤æ°ä¿æãã¦ãã¾ãããããã£ã¦ãChildãæã¤Parentã®é¢é£ãJOIN FETCH
ã§åå¾ãããã¨ã§Childã®N+1åé¡ã解決ã§ãã¾ãã
SELECT c FROM Child c JOIN FETCH c.parent p JOIN FETCH p.children
2. @Fetchã¢ããã¼ã·ã§ã³ã使ç¨ãã
@Fetchã¢ããã¼ã·ã§ã³ã¯ä¸è¨ã®ããã«ä½¿ç¨ãã¾ãã
@Entity public class Parent { @OneToMany(mappedBy="parent") @Fetch(FetchMode.SUBSELECT) private List<Child> children; ... }
@Fetchã«ã¯è¤æ°ã®FetchModeãããã¾ããFechModeã®ç¨®é¡ã«ãã£ã¦SELECTæã®çºè¡æ°ãç°ãªãã¾ãã
FetchMode | SELECTæã®çºè¡æ° |
---|---|
SELECT | N+1 |
JOIN | 1 |
SUBSELECT | 2 |
FechModeã®è©³ç´°ã¯ä¸è¨ãªã³ã¯ãåç
§ãã¦ãã ããã
http://www.solidsyntax.be/lessons-learned/hibernate-fetchmode-explained-example/
ãã ããFetchMode.JOINã¯JPQLã使ç¨ããå ´åã¯æ£ããåä½ãã¾ãããCriteriaã使ç¨ããå ´åã¯ä¸è¨ã®ããã«1åã ãSELECTæãçºè¡ãã¾ãããJPQLã®å ´åã¯N+1åãSELECTæãçºè¡ãã¦ãã¾ãã¾ããããã¯Hibernateã®æ¢ç¥ã®ãã°ã®ããã§ãã
http://stackoverflow.com/questions/18891789/fetchmode-join-makes-no-difference-for-manytomany-relations-in-spring-jpa-reposi
@Fetchã使ç¨ããã®ã§ããã°FetchModeã¯SUBSELECTãé¸æããæ¹ãè¯ãã¨æãã¾ãã
ãã ããSUBSELECTã«ãåé¡ã¯ããã¾ãããã®åé¡ã¯SUBSELECTãåæ¹åé¢é£ã§ä½¿ç¨ããå ´åã«çºçãã¾ããMany to Oneã®ãªãã¸ã§ã¯ãï¼ä¸è¨ä¾ã§ã¯Childï¼ãåå¾ãããã¨ããå ´åãSUBSELECTã使ç¨ããã¨N+M+1åã®SELECTæãçºè¡ããã¦ãã¾ãã¾ããMã¯One to Manyï¼ä¸è¨ä¾ã§ã¯Parentï¼ã®ã¬ã³ã¼ãæ°ã§ãã
ãã®ç¾è±¡ã¯ãã®ã³ã¼ãã使ç¨ãã¦ç¢ºèªãã¾ãããããããããããããäºãã«é¢é£ãåå¾ãããã¨ãã¦ããã®ãåå ã ã¨æãã¾ããã詳ããã¯ãããã¾ããã
ã¾ã¨ã
N+1åé¡ã解決ãããå ´åã¯JOIN FETCH
ã使ç¨ãã¹ãã§ãã@Fetchã¢ããã¼ã·ã§ã³ã¯JPAã§ã¯å®ç¾©ããã¦ããããHibernateã®ãã°ãããã®ã§ä½¿ç¨ããªãæ¹ãç¡é£ã§ãã
ã¾ããå¯è½ã§ããã°åæ¹åé¢é£ã®ãªãã¯ã©ã¹è¨è¨ãããæ¹ãè¯ãã¨æãã¾ããåæ¹åé¢é£ã¯åæ¹åé¢é£ã«æ¯ã¹ã¦N+1åé¡ãçºçããããããããé²ãããã«èæ ®ãã¹ãç®æãå¢ããããã³ã¼ãã£ã³ã°ãé£ãããªãã¾ãã