Re: JPA n+1 problem

Summers Pittman ℝ <[email protected]> Fri, 11 May 2018 18:53:37 -0400
Newsgroups gmane.org.user-groups.ajug.members
Message-ID <CAHK_qPvXqpgf6=HodKi8H+DwS9gEsonM79qx0gETQHFMc65ZjA@mail.gmail.com>
--===============0409646671181080090==
Content-Type: multipart/alternative; boundary="00000000000055f630056bf6025c"

--00000000000055f630056bf6025c
Content-Type: text/plain; charset="UTF-8"

Without any context I I will give you some generic advice:

If you can eagerly fetch the objects giving you trouble, eagerly fetch
them.  Your JPA provider should be smart enough to batch them up and give
you more than one at a time.

If you are iterating over a bunch of objects to aggregate some data, I
would just write a sql query to do that aggregation in the database and
return the result.
If you are generating data for a report that is rendered to a PDF I would
write a bunch of SQL statements to remove iteration.

Have you used any profiling tools?  Are they saying you need indexes in
your database?  If so I would make the suggestions they ask.

Congrats on getting far enough/having data big enough you get to think
about optimization :)

Summers Pittman
>>Phone:404 941 4698
>>Java is my crack.



On Fri, May 11, 2018 at 1:31 PM Mike Barnes <mike.barnes-f9aWwpgOZj5Wk0Htik3J/[email protected]>
wrote:

> I am using spring boot with JPA for an application that I am developing. I
> have created all the entities and wired them in to the application through
> a service layer. Now I need to resolve performance problems and  have run
> into the famous N+1 problem.
>
> After scouring the web for the last week, I am now confused as to the best
> way to solve this problem. So, I am reaching out to this community to get
> any suggests as to the best way to solve the problem.
>
> Thanks
>
> --
>
> Michael D. Barnes
>
> Sr Software Engineer
>
> Office: +1 404-960-9621
>
> E-mail: mike.barnes-f9aWwpgOZj5Wk0Htik3J/[email protected]
>
> Web: www.site1001.com
>
> Follow Site 1001 on: LinkedIn <https://linkedin.com/company/site1001> |
> Instagram <http://instagram.com/site1001> | Twitter
> <https://twitter.com/site1001> | Facebook
> <http://www.facebook.com/site1001> | YouTube
> <https://www.youtube.com/channel/UCmQBqlisUgdd0HjQRVFD6WQ>
> 20772 Carmen Loop, Suite 130
> <https://www.google.com/maps/place/20772+Carmen+Loop,+Suite+130+Bend,+OR+97702>
>
> Bend, OR 97702
> <https://www.google.com/maps/place/20772+Carmen+Loop,+Suite+130+Bend,+OR+97702>
> _______________________________________________
> ajug-members mailing list
> [email protected]
> Manage your subscription and unsubscribe
> http://lists.ajug.org/mailman/listinfo/ajug-members

--00000000000055f630056bf6025c
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>Without any context I I will give you some generic ad=
vice:</div><div><br></div><div>If you can eagerly fetch the objects giving =
you trouble, eagerly fetch them.=C2=A0 Your JPA provider should be smart en=
ough to batch them up and give you more than one at a time.</div><div><br><=
/div><div>If you are iterating over a bunch of objects to aggregate some da=
ta, I would just write a sql query to do that aggregation in the database a=
nd return the result.</div><div>If you are generating data for a report tha=
t is rendered to a PDF I would write a bunch of SQL statements to remove it=
eration.</div><div><br></div><div>Have you used any profiling tools?=C2=A0 =
Are they saying you need indexes in your database?=C2=A0 If so I would make=
 the suggestions they ask.</div><div><br></div><div>Congrats on getting far=
 enough/having data big enough you get to think about optimization :)</div>=
<br clear=3D"all"><div><div dir=3D"ltr" class=3D"gmail_signature" data-smar=
tmail=3D"gmail_signature"><div dir=3D"ltr">Summers Pittman<br><span>&gt;&gt=
;Phone:<a>404 941 4698</a></span><br>&gt;&gt;Java is my crack.<br><div><br>=
</div></div></div></div><br></div><br><div class=3D"gmail_quote"><div dir=
=3D"ltr">On Fri, May 11, 2018 at 1:31 PM Mike Barnes &lt;<a href=3D"mailto:=
mike.barnes-f9aWwpgOZj5Wk0Htik3J/[email protected]">mike.barnes-f9aWwpgOZj5Wk0Htik3J/[email protected]</a>&gt; wrote:<br></div>=
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex"><div dir=3D"ltr">I am using spring boot with=
 JPA for an application that I am developing. I have created all the entiti=
es and wired them in to the application through a service layer. Now I need=
 to resolve performance problems and=C2=A0 have run into the famous N+1 pro=
blem.<div><br></div><div>After scouring the web for the last week, I am now=
 confused as to the best way to solve this problem. So, I am reaching out t=
o this community to get any suggests as to the best way to solve the proble=
m.</div><div><br></div><div>Thanks</div><div><div><br></div>-- <br><div cla=
ss=3D"m_20723782032308732gmail_signature" data-smartmail=3D"gmail_signature=
"><div dir=3D"ltr"><p dir=3D"ltr" style=3D"line-height:1.38;margin-top:0pt;=
margin-bottom:0pt"><span style=3D"font-size:9.5pt;font-family:Verdana;color=
:#444444;background-color:#ffffff;font-weight:700;font-style:normal;font-va=
riant:normal;text-decoration:none;vertical-align:baseline">Michael D. Barne=
s<br></span></p><p dir=3D"ltr" style=3D"line-height:1.38;margin-top:0pt;mar=
gin-bottom:0pt"><span style=3D"font-size:9.5pt;font-family:Verdana;color:#4=
44444;background-color:transparent;font-weight:400;font-style:normal;font-v=
ariant:normal;text-decoration:none;vertical-align:baseline">Sr Software Eng=
ineer<br></span></p><p dir=3D"ltr" style=3D"line-height:1.38;margin-top:0pt=
;margin-bottom:0pt"><span style=3D"font-size:9.5pt;font-family:Verdana;colo=
r:#444444;background-color:transparent;font-weight:400;font-style:normal;fo=
nt-variant:normal;text-decoration:none;vertical-align:baseline"><img src=3D=
"https://lh4.googleusercontent.com/Sp9xnvepxJBESkxLRN40IGyXFPHgUMicv8nVNkx8=
viNjvY7U7ITrO50HyJSkzL-8Z1WjvjTrBm2sS6f-aiJw4Yh5EYTYJW3Cn41oKj5tYSERDj2Valj=
zuffry1aNsBvjbIdCC4c4" style=3D"border:none" width=3D"146" height=3D"35"></=
span></p><p dir=3D"ltr" style=3D"line-height:1.38;margin-top:0pt;margin-bot=
tom:0pt"><span style=3D"font-size:9.5pt;font-family:Verdana;color:#444444;b=
ackground-color:transparent;font-weight:400;font-style:normal;font-variant:=
normal;text-decoration:none;vertical-align:baseline">Office: +1 404-960-962=
1<br></span></p><p dir=3D"ltr" style=3D"line-height:1.38;margin-top:0pt;mar=
gin-bottom:0pt"><span style=3D"font-size:9.5pt;font-family:Verdana;color:#4=
44444;background-color:transparent;font-weight:400;font-style:normal;font-v=
ariant:normal;text-decoration:none;vertical-align:baseline">E-mail: </span>=
<span style=3D"font-size:9.5pt;font-family:Verdana;color:#1155cc;background=
-color:transparent;font-weight:400;font-style:normal;font-variant:normal;te=
xt-decoration:none;vertical-align:baseline"><a href=3D"mailto:mike.barnes@s=
ite1001.com" target=3D"_blank">mike.barnes-f9aWwpgOZj5Wk0Htik3J/[email protected]</a></span></p><p di=
r=3D"ltr" style=3D"line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span=
 style=3D"font-size:9.5pt;font-family:Verdana;color:#444444;background-colo=
r:transparent;font-weight:400;font-style:normal;font-variant:normal;text-de=
coration:none;vertical-align:baseline">Web: </span><a href=3D"http://www.si=
te1001.com/" style=3D"text-decoration:none" target=3D"_blank"><span style=
=3D"font-size:9.5pt;font-family:Verdana;color:#1155cc;background-color:tran=
sparent;font-weight:400;font-style:normal;font-variant:normal;text-decorati=
on:underline;vertical-align:baseline">www.site1001.com</span></a></p><br><p=
 dir=3D"ltr" style=3D"line-height:1.38;margin-top:0pt;margin-bottom:0pt"><s=
pan style=3D"font-size:10pt;font-family:Arial;color:#222222;background-colo=
r:transparent;font-weight:700;font-style:normal;font-variant:normal;text-de=
coration:none;vertical-align:baseline">Follow Site 1001 on: </span><a href=
=3D"https://linkedin.com/company/site1001" style=3D"text-decoration:none" t=
arget=3D"_blank"><span style=3D"font-size:10pt;font-family:Arial;color:#115=
5cc;background-color:transparent;font-weight:700;font-style:normal;font-var=
iant:normal;text-decoration:underline;vertical-align:baseline">LinkedIn</sp=
an></a><span style=3D"font-size:10pt;font-family:Arial;color:#222222;backgr=
ound-color:transparent;font-weight:700;font-style:normal;font-variant:norma=
l;text-decoration:none;vertical-align:baseline"> | </span><a href=3D"http:/=
/instagram.com/site1001" style=3D"text-decoration:none" target=3D"_blank"><=
span style=3D"font-size:10pt;font-family:Arial;color:#1155cc;background-col=
or:transparent;font-weight:700;font-style:normal;font-variant:normal;text-d=
ecoration:underline;vertical-align:baseline">Instagram</span></a><span styl=
e=3D"font-size:10pt;font-family:Arial;color:#222222;background-color:transp=
arent;font-weight:700;font-style:normal;font-variant:normal;text-decoration=
:none;vertical-align:baseline"> | </span><a href=3D"https://twitter.com/sit=
e1001" style=3D"text-decoration:none" target=3D"_blank"><span style=3D"font=
-size:10pt;font-family:Arial;color:#1155cc;background-color:transparent;fon=
t-weight:700;font-style:normal;font-variant:normal;text-decoration:underlin=
e;vertical-align:baseline">Twitter</span></a><span style=3D"font-size:10pt;=
font-family:Arial;color:#222222;background-color:transparent;font-weight:70=
0;font-style:normal;font-variant:normal;text-decoration:none;vertical-align=
:baseline"> | </span><a href=3D"http://www.facebook.com/site1001" style=3D"=
text-decoration:none" target=3D"_blank"><span style=3D"font-size:10pt;font-=
family:Arial;color:#1155cc;background-color:transparent;font-weight:700;fon=
t-style:normal;font-variant:normal;text-decoration:underline;vertical-align=
:baseline">Facebook</span></a><span style=3D"font-size:10pt;font-family:Ari=
al;color:#222222;background-color:transparent;font-weight:700;font-style:no=
rmal;font-variant:normal;text-decoration:none;vertical-align:baseline"> | <=
/span><a href=3D"https://www.youtube.com/channel/UCmQBqlisUgdd0HjQRVFD6WQ" =
style=3D"text-decoration:none" target=3D"_blank"><span style=3D"font-size:1=
0pt;font-family:Arial;color:#1155cc;background-color:transparent;font-weigh=
t:700;font-style:normal;font-variant:normal;text-decoration:underline;verti=
cal-align:baseline">YouTube</span></a></p><a href=3D"https://www.google.com=
/maps/place/20772+Carmen+Loop,+Suite+130+Bend,+OR+97702" style=3D"text-deco=
ration:none" target=3D"_blank"><span style=3D"font-size:9pt;font-family:Ver=
dana;color:#1155cc;background-color:transparent;font-weight:400;font-style:=
normal;font-variant:normal;text-decoration:underline;vertical-align:baselin=
e">20772 Carmen Loop, Suite 130</span></a><p dir=3D"ltr" style=3D"line-heig=
ht:1.38;margin-top:0pt;margin-bottom:0pt"><a href=3D"https://www.google.com=
/maps/place/20772+Carmen+Loop,+Suite+130+Bend,+OR+97702" style=3D"text-deco=
ration:none" target=3D"_blank"><span style=3D"font-size:9pt;font-family:Ver=
dana;color:#1155cc;background-color:transparent;font-weight:400;font-style:=
normal;font-variant:normal;text-decoration:underline;vertical-align:baselin=
e">Bend, OR 97702</span></a></p></div></div>
</div></div>
_______________________________________________<br>
ajug-members mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">ajug-membe=
[email protected]</a><br>
Manage your subscription and unsubscribe <a href=3D"http://lists.ajug.org/m=
ailman/listinfo/ajug-members" rel=3D"noreferrer" target=3D"_blank">http://l=
ists.ajug.org/mailman/listinfo/ajug-members</a></blockquote></div>

--00000000000055f630056bf6025c--

--===============0409646671181080090==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: inline

X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KYWp1Zy1tZW1i
ZXJzIG1haWxpbmcgbGlzdAphanVnLW1lbWJlcnNAbGlzdHMuYWp1Zy5vcmcKTWFuYWdlIHlvdXIg
c3Vic2NyaXB0aW9uIGFuZCB1bnN1YnNjcmliZSBodHRwOi8vbGlzdHMuYWp1Zy5vcmcvbWFpbG1h
bi9saXN0aW5mby9hanVnLW1lbWJlcnM=

--===============0409646671181080090==--