Annyce Davis

Davis Technology Consulting

  • Home
  • About Me
  • Blog
  • Courses
  • Newsletter

Improve Performance of Grails Queries in MySQL

January 19, 2010 by Annyce Davis

A web application that I was developing would become extremely slow as the number of requests increased per minute. I couldn’t figure out why because with my local database everything seemed fine. Here is the process that I used in MySQL to determine the bottleneck in my database. The final problem was a missing index on a table that was used in the where clause of a select statement over and over again. I included a snapshot the MySQL Process list at the end of the post. Hope this helps someone.

Steps to Follow:

  • show full processlist;
    • shows each database connection that has been allocated for your database
    • contains the time a connection has been Sleep
    • contains how long a Query is taking to execute, you want this to be less than a second ideally
  • describe <query>;
    • this gives you the database’s plan for executing the query
    • you want the number of rows that will be looked at to be as small as possible
  • show index from <table>;
    • this will list all indices contained on a table
    • this is a quick way to discover if you are missing a key index that could improve query performance
  • create index <tablename_fieldname_idx> on <table>(field);
    • this will add a new index to a table

Mocking Hibernate Criteria in Grails App

January 14, 2010 by Annyce Davis

Testing is always a sore spot in many applications, and Grails is no exception, but mocking objects and methods is actually not as bad as you think. Here is a simple way to mock the Hibernate Criteria object that is available from Grails domain classes.

Example:
If this is the code that you want to test, and for this particular test you want to have no results, this how you would mock it out.

Code:

Test Code:

The key piece of code is the creation of the bookCriteria object, in this case it is a member of the test class that is why it is not declared locally in the function. However, you will notice that the bookCriteria is simply a HashMap that returns a empty list for the ‘list’ parameter. This way when testing ‘c.list’ a empty list will be returned. Simple hunh!

Using ConfigSlurper Scripts in Grails

October 21, 2009 by Annyce Davis

In Java .properties files are used to configure an application. You can use similar notation to configure Grails applications as well.
A sample appName-config.groovy file that sets up the configuration items that we need.


abtesting {
amazon = new Double(0.5)
ebay = new Double(0.5)
}

A sample function in a controller that will access the configuration properties and modify them.


def abtesting = {
def propValue = grailsApplication.config.abtesting.amazon
println propValue


grailsApplication.config.abtesting.amazon = new Double(1.0)
println grailsApplication.config.abtesting.amazon
}

This code produces the following output:
>> 0.5
>> 1.0

Using Grails’ JSON and HTML Builders

October 20, 2009 by Annyce Davis

In order to generate JSON in groovy that can then be passed to a Grails controller you should use the JSONBuilder class. This class is part of the Grails util package. The code below demonstrates how to generate JSON and HTML.

Resulting JSON that is produced:

ResultSet {['text':'google', 'url':'http://www.google.com'], ...}

def writer = new StringWriter();
new grails.util.JSonBuilder(writer).json {
ResultSet {
links.each { link ->
Links(text: link.text, url: link.url)
}
}
}
writer.toString()

This is the code to do the same with HTML, you would use the Groovy XML MarkupBuilder instead.


StringWriter w = new StringWriter()
def builder = new groovy.xml.MarkupBuilder(w)

builder.div (class: 'className'){
links.each { link ->
p {
b 'Title: '
builder.yield link.text
}
}
}
w.toString()

Resulting HTML that is produced:
<div class=”className”>
<p><b>Title: </b>google</p>
</div>

« Previous Page
Next Page »

Follow Me

  • Bluesky

Categories

  • Android (61)
  • Career (5)
  • Communication (4)
  • Flutter (1)
  • Git (4)
  • Gradle (4)
  • Grails (23)
  • iOS (1)
  • Java (8)
  • JavaScript (6)
  • Kotlin (17)
  • Life (5)
  • Public Speaking (26)
  • Revenue (2)
  • RxJava (1)
  • Software Development (14)
  • Twitter (3)
  • Uncategorized (11)
  • Video Course (5)

Follow Me

  • Bluesky

Copyright © 2025 · All Rights Reserved · Log in