{"id":4461,"date":"2011-11-07T13:08:22","date_gmt":"2011-11-07T07:38:22","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=4461"},"modified":"2016-12-19T15:19:23","modified_gmt":"2016-12-19T09:49:23","slug":"grails-find-number-of-queries-executed-for-a-particular-request","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/grails-find-number-of-queries-executed-for-a-particular-request\/","title":{"rendered":"Grails: Find number of queries executed for a particular request"},"content":{"rendered":"<div id=\"_mcePaste\">When response time of a page is slow, we might be interested in viewing database queries executed on that page. Enabling SQL Logging shows all queries but we are interested in queries for that particular request only.<\/div>\n<div>We can get this information by just simply adding the following filter to our application(Filters in Grails)<\/div>\n<p>[groovy]<br \/>\n\t        logHibernateStats(controller: &#8216;*&#8217;, action: &#8216;*&#8217;) {<br \/>\n\t\t        before = {<br \/>\n\t\t\t\tStatistics stats = sessionFactory.statistics;<br \/>\n\t\t\t\tif(!stats.statisticsEnabled) {stats.setStatisticsEnabled(true)}<br \/>\n                       }<\/p>\n<p>\t\t\tafterView = {<br \/>\n\t\t\t\tStatistics stats = sessionFactory.getStatistics()<br \/>\n\t\t\t\tdouble queryCacheHitCount  = stats.getQueryCacheHitCount();<br \/>\n\t\t\t\tdouble queryCacheMissCount = stats.getQueryCacheMissCount();<br \/>\n\t\t\t\tdouble queryCacheHitRatio = (queryCacheHitCount \/ ((queryCacheHitCount + queryCacheMissCount) ?: 1))<br \/>\n\t\t\t\tlog.info &amp;quot;&amp;quot;&amp;quot;<br \/>\n######################## Hibernate Stats ##############################################<br \/>\nTransaction Count:${stats.transactionCount}<br \/>\nFlush Count:${stats.flushCount}<br \/>\nTotal Collections Fetched:${stats.collectionFetchCount}<br \/>\nTotal Collections Loaded:${stats.collectionLoadCount}<br \/>\nTotal Entities Fetched:${stats.entityFetchCount}<br \/>\nTotal Entities Loaded:${stats.entityFetchCount}<br \/>\nTotal Queries:${stats.queryExecutionCount}<br \/>\nqueryCacheHitCount:${queryCacheHitCount}<br \/>\nqueryCacheMissCount:${queryCacheMissCount}<br \/>\nqueryCacheHitRatio:${queryCacheHitRatio}<br \/>\n######################## Hibernate Stats ##############################################<br \/>\n&amp;quot;&amp;quot;&amp;quot;<br \/>\n\t\t\t\tstats.clear()<br \/>\n\t\t\t}<\/p>\n<p>        }<br \/>\n[\/groovy]<\/p>\n<p>There are a <a href=\"http:\/\/www.javalobby.org\/java\/forums\/t19807.html\" target=\"_blank\">bunch of other stats<\/a> that we can get to do more analysis \u00a0If you see a large amount of queries, you can simply turn sql logging on for the particular action in <a href=\"http:\/\/www.tothenew.com\/blog\/log-sql-in-grails-for-a-piece-of-code\/\">this way<\/a><\/p>\n<p>Gotchas:<\/p>\n<ol>\n<li>Since we are getting the stats from sessionFactory, they just do not pertain to any particular request. So this will not work in a multiuser enviornment(prod\/qa)<\/li>\n<li>Also these stats may not be correct if you have any background jobs running simultaneously<\/li>\n<\/ol>\n<p>This is basically one of the things that we do when we want to &#8216;tune&#8217; the performance of an application.<\/p>\n<p>PS: Performance tuning comprises of many more things. Peter ledbrook gave an excellent session on it in last year&#8217;s SpringOne2GX. You can find a summary <a href=\"http:\/\/www.tothenew.com\/blog\/springone2gx-peter-ledbrooks-session-on-performance-tuning\/\" target=\"_blank\">here<\/a><\/p>\n<p>Regards<br \/>\n~~Himanshu Seth~~<\/p>\n<p>http:\/\/www.tothenew.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When response time of a page is slow, we might be interested in viewing database queries executed on that page. Enabling SQL Logging shows all queries but we are interested in queries for that particular request only. We can get this information by just simply adding the following filter to our application(Filters in Grails) [groovy] [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4461"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=4461"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4461\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=4461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=4461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=4461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}