title: JPA 2 - How to cache/optimize count queries?
body: <p>I have Java EE application, where I have list of groups. I want to display number of items in every group, so I do:</p>
  
  <pre><code>SELECT COUNT(p) FROM Product p WHERE LOWER(p.group) = LOWER(:group)
  </code></pre>
  
  <p>Group is plain String. </p>
  
  <p>Problem is when I have 50+ groups, time to load page is about 10s, which is unacceptable. When I remove counting time drops to ~1s. How can I optimize/cache this? </p>
  
  <p>My first idea was to use EJB singleton to keep counts locally and refresh them with EJB timer.</p>
  
  <p>I'm using glassfish v3, eclipselink and oracle 10g database on same machine.
  I'm using cache in eclipselink, but it seems not to cache count queries.</p>
  
  <pre><code>&lt;property name="eclipselink.cache.shared.default" value="false"/&gt;
  &lt;property name="eclipselink.cache.size.default" value="500"/&gt;
  </code></pre>