title: Is there a way within hibernate to retrieve fast non-blocking row counts?
body: <p>The following query generated by hibernate takes 13+ seconds and locks the table: </p>
  
  <pre><code>SELECT COUNT(auditentit0_.audit_id) AS col_0_0_ FROM Audit auditentit0_ WHERE 1=1; 
  </code></pre>
  
  <p>The growing Microsoft SQL server database table contains 90+ million rows. </p>
  
  <p>For Microsoft SQL server, I have found an accurate meta data way of getting the same information very quickly. </p>
  
  <p><em>However, I would rather not write custom code for Microsoft sql server and oracle (the next database) if hibernate has a way of getting this information.</em></p>
  
  <p>Here is an example meta data query for Microsoft sql server that is accurate and almost instant:</p>
  
  <pre><code>SELECT SUM (row_count) FROM sys.dm_db_partition_stats WHERE object_id=OBJECT_ID('huge_audit_table') AND (index_id=0 or index_id=1);
  </code></pre>
  
  <p>Is there a way to have hibernate issue a similar query for a table row count?</p>
  
  <hr>
  
  <p>One posted answer has indicated that a view could be of use. I'm investigating this post to see if it can solve the issue:</p>
  
  <p><a href="https://vladmihalcea.com/map-jpa-entity-to-view-or-sql-query-with-hibernate/" rel="nofollow noreferrer">https://vladmihalcea.com/map-jpa-entity-to-view-or-sql-query-with-hibernate/</a></p>