开发者

Does caching object with Fetch in JPA have any performance advantage in the long run?

Does using fetch with jpa queri开发者_运维知识库es have any performance boosts in the long run?

Lets say I want to read 100 objects from a DB, each of these objects have a one-to-many relationship containing two additional objects. These additional objects will be read fairly often. The query to get these objects could be made in two ways:

1) SELECT o FROM Object o 

2) SELECT o FROM Object o FETCH o.relationObjects

I realize fetching the relation objects will load them all instantly instead of lazily, with the advantage of making only a single query against the DB instead of hundreds.

Here comes the question: lets say the program has been running for a day now and the entity manager has cached all objects with their relationships (for a total of 300). I want to list the objects again, with their relationship objects aswell. Will query 2 give me any performance advantage at all now? Does it serve any purpose after the first cache?

EDIT: I'm using EclipseLink


Speaking about caching in general, rather than the JPA-specifics I would have thought that the scenario where we get the win is when we don't get cache hits. My expectation is that it's most usual for a cache to be much smaller than the size of the database, and in such cases reducing the number of queries could be very important.

The JPA standard mandates one level of cache: the Entity Manager Cache, this is a relatively short duration cache, in normal usage it lasts for the life of a single transaction. Hence the scenario you ask about does not really apply, we don't get a gradually filling cache - it's too short lived for that, and so the benefits of eager loading may be quite significant.

Note also that JPA implementers then have the possibility of adding a second level of cache, sometimes referred to as a L2 or "shared" cache. This article describes that. There are cache expiration rules and implementers may also offer APIs to control refreshing this cache for example this article describes one such API.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜