{"id":16436,"date":"2014-12-20T16:02:37","date_gmt":"2014-12-20T10:32:37","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=16436"},"modified":"2014-12-20T16:16:08","modified_gmt":"2014-12-20T10:46:08","slug":"grails-performance-optimization-unique-constraint","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/grails-performance-optimization-unique-constraint\/","title":{"rendered":"Grails performance optimization &#8211; Unique constraint"},"content":{"rendered":"<p>Grails unique constraint is very powerful way to check duplicate keys in any DBMS, but it has some impact on application performance. Lets check it by one example:<\/p>\n<p>Suppose we have one domain having the following structure:<\/p>\n<p>[code]<\/p>\n<p>class User {<\/p>\n<p>String firstName<br \/>\nString lastName<br \/>\nString emailId<\/p>\n<p>static constraints = {<br \/>\n     emailId nullable: false, blank: true, unique: true, email: true<br \/>\n  }<br \/>\n}<\/p>\n<p>[\/code]<\/p>\n<p>To check database queries for this transaction enable the logging sql into Datasource.groovy. This will print all\u00a0database\u00a0queries trigger by grails application.<\/p>\n<p>[code]<\/p>\n<p>dataSource {<br \/>\nusername = &#8221;<br \/>\npassword = &#8221;<br \/>\ndbCreate = &#8216;create-drop&#8217; \/\/ one of &#8216;create&#8217;, &#8216;create-drop&#8217;, &#8216;update&#8217;, &#8216;validate&#8217;, &#8221;<br \/>\nurl = &#8216;jdbc:mysql:\/\/localhost:3306\/db_test?autoReconnect=true&#8217;<br \/>\nloggingSql\u00a0= true<br \/>\n}<\/p>\n<p>[\/code]<\/p>\n<p>We can also log sql for a piece of code as well, read this <a href=\"http:\/\/www.tothenew.com\/blog\/log-sql-in-grails-for-a-piece-of-code\/\" title=\"Log Sql for piece of code\" target=\"_blank\">blog<\/a>.<br \/>\nNow create object of User domain and save it.<\/p>\n<p>[code]<br \/>\nUser user = new User(firstName: &#8216;Deepak&#8217;, lastName: &#8216;Mittal&#8217;, emailId: &#8216;deepak.krmittal@intelligrape.com&#8217;)<br \/>\nuser.save(flush: true)<br \/>\n[\/code]<br \/>\nIn case, your unique validation pass and\u00a0if you check your console, then there are 3 queries triggered by grails application :<\/p>\n<p>[code]<\/p>\n<p>Hibernate: select this_.id as id1_3_0_, this_.version as version2_3_0_, this_.email_id as email_id3_3_0_, this_.first_name as first_na4_3_0_, this_.last_name as last_nam5_3_0_ from user this_ where this_.email_id=?<br \/>\nHibernate: select this_.id as id1_3_0_, this_.version as version2_3_0_, this_.email_id as email_id3_3_0_, this_.first_name as first_na4_3_0_, this_.last_name as last_nam5_3_0_ from user this_ where this_.email_id=?<br \/>\nHibernate: insert into user (version, email_id, first_name, last_name) values (?, ?, ?, ?)<\/p>\n<p>[\/code]<\/p>\n<p>This might be a bug but we can fix it as well using following solution code:<\/p>\n<p>[code]<\/p>\n<p>User user = new User(firstName: &#8216;Deepak&#8217;, lastName: &#8216;Mittal&#8217;, emailId: &#8216;deepak.krmittal@intelligrape.com&#8217;)<\/p>\n<p>if(user.validate() &amp;&amp; user.save(flush: true, validate: false)) {<br \/>\n    println(&#8216;Success&#8217;)<br \/>\n} else {<br \/>\n    println(&#8216;Error&#8217;)<br \/>\n}<\/p>\n<p>[\/code]<\/p>\n<p>When you run this code, then this will trigger only 2 queries like:<\/p>\n<p>[code]<\/p>\n<p>Hibernate: select this_.id as id1_3_0_, this_.version as version2_3_0_, this_.email_id as email_id3_3_0_, this_.first_name as first_na4_3_0_, this_.last_name as last_nam5_3_0_ from user this_ where this_.email_id=?<br \/>\nHibernate: insert into user (version, email_id, first_name, last_name) values (?, ?, ?, ?)<\/p>\n<p>[\/code]<\/p>\n<p>Now your purpose is solved with a little bit optimized code. We can further optimize this code in the next blog.<\/p>\n<p>Code written with Grails 2.4.3.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Grails unique constraint is very powerful way to check duplicate keys in any DBMS, but it has some impact on application performance. Lets check it by one example: Suppose we have one domain having the following structure: [code] class User { String firstName String lastName String emailId static constraints = { emailId nullable: false, blank: [&hellip;]<\/p>\n","protected":false},"author":145,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":9},"categories":[7],"tags":[29,1576,1579,1577,442,648,1578],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/16436"}],"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\/145"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=16436"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/16436\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=16436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=16436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=16436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}