Performance

Software Performance

Stop calculating dates for your queries in MySQL

Recently I was working on a couple reports that were running slow. I was looking for indexes that could be created or modified in order to speed them up and provide a quicker response on my SellersToolbox.com website. I was able to find a couple but then the issue of dates popup up. I provide monthly, quarterly, and yearly reports and trying to select the right dates is very important. I have some logic there that determines the right range to be put into my queries.

What I learned recently though is that using functions such as YEAR(), MONTH(), and DAY() defeat the use of indexes and primary keys most of the time. Because these functions have to be performed on the date value, each record in the table is usually processed. This slows the query down greatly and causes a lot of needless computation to result.

What I found was the BETWEEN option in MySQL. Using this allows the query optimizer to utilize indexes fully, and eliminates the need for many date computations which can be expensive.

An added benefit I found was how to handle the beginning and end dates for my queries. Instead of calculating what the end date was, I could use the INTERVAL term and let MySQL figure it out for me. This also allowed me to use the same query for my monthly, quarterly, and yearly time ranges very simply. All I had to do was take the date and the interval such as the 3 examples below.

    BETWEEN '2011-10-01' AND '2011-10-01' + INTERVAL 1 MONTH
    BETWEEN '2011-10-01' AND '2011-10-01' + INTERVAL 3 MONTH
    BETWEEN '2011-10-01' AND '2011-10-01' + INTERVAL 1 YEAR

In this way I didn't have to do any extra work in my application, and I was assured that MySQL would choose records in the correct date range and take advantage of my indexes and primary keys. In this case, trying to do some performance improvements taught me a better way to write my SQL and resulted in less code which was easier to understand.

As you can see, modifying the query with the right amount of time is quite trivial. I have dropdown values in the SELECT on my web pages and all I have to do is append "-01" and then append the interval the user has chosen. Its great when code can be made simpler and more powerful at the same time.

Java 7 Delayed again. Considering Scala…

Well, it looks like JDK 7 is going to be delivered sometime in 2011 and JDK 8 will be in 2012. I’m all for smaller releases and shorter time between them. The only way to get adoption of new technologies is to get them out into the hands of developers and see how it plays out. The community wants a release so bad they are ready to take anything at this point. It has been 5 years since the release of JDK 6 and while it is a very good release, there are many things promised that would improve the platform and give it life again.

Waiting another 2 years to get features that are in Scala today seems like a waste of time. Sure, there are many developers who can wait since they are still working on projects with JDK 4 or 5. These shops will be ready for the new features when they are good and ready. The rest of us want the new JDK now though, and it looks like the wait is going to be even longer.

I have not looked at Scala before now, but I’m going to. My development work is still in JDK 6 for the Android platform. I don’t see that changing in the near future, but as far as languages go, Scala seems to have the mind power behind it to make it. The other languages I have looked at such as Groovy, JRuby, et. al. are nice, but it appears that momentum is behind Scala and so that is where I’m going to be focusing my learning. I just hope that JDK 7 comes out sooner than later.