Let me start by saying that I understand that the query cache is meant to work in conjunction with the entity cache, in that the query cache only caches IDs, which should then hopefully retrieve the corresponding entities from the entity cache rather than hitting the database. Every entity cache miss results in a SQL statement being executed to load the entity. Obviously this could lead to a lot of SQL being executed due to query caching.
What I want to ask about it is: why doesn't the query cache use the same batch-initialization mechanism that our proxies do? Is this a design decision, an accidental omission, or a future feature?
If you like I can post my code, but my tests appear to show that if I set batch-size=3 for my User class, then load 8 instances by their IDs, then loop through printing out the username, I do indeed see 3 batches of SQL queries.
However, if I load those same 8 users with a cached CreateQuery("from User") or CreateCriteria(typeof(User)), the original query is indeed cached, but for initializing the entities, neither of those operations uses batch-size and instead issues 8 SELECT statements.
Also it appears that with entity caching enabled, the cached HQL IQuery puts the entities into the cache the first time it is listed, but the cached Criteria query does not. With ICriteria I had to do 3 runs to get full caching whereas with IQuery I only had to do 2. * The first loaded all the entities but appears not to have cached them * The second was able to get the cached IDs, but had to load the entities (and didn't batch them) * On the third run, both the query results and the IDs were in the cache
|