Correlated Subqueries

8:58 PM EST Sunday, January 6 2008

The Rails' ORM ActiveRecord has support for counter caches. Say you have a blog, like this one, which has articles, and each article has many comments. If you are going to show a list of articles with the number of comments each article has, without a counter cache, you will end up with N+1 SQL queries. One query to get the articles, and one select count(*) from comments where article_id = ? query for each article.

To avoid the N+1 select, you can add a column in the articles table that contains the number of comments the article has. With some ORM frameworks, this becomes somewhat of a pain, because you have to keep that count column updated, but ActiveRecord makes it easy. If you just create a table called <table_name>_count on a parent table, it will keep the total of the records that belong to that record in that column. So then when you call article.comments_count, there is no SQL query executed.

This is nice, but what happens if you have a site in production and you want to add a counter cache column? You've got to create the column and then get the count updated. You can do this in a migration, but you can also do it in SQL using a correlated subquery. Using MySQL, you can do it like this:

alter table articles add column comments_count int(11) default 0;
update articles set articles.comments_count = 
  (select count(comments.id) from comments where articles.id = comments.article_id);

Posted in  | Tags ActiveRecord, MySQL | 0 Comments

PaulBarry.com 3.0

7:51 PM EST Sunday, January 28 2007

So I've finally finished the latest version of my site. The first version of my site went online on March 3rd of 2006 using WordPress. Then on my last birthday, I switched to Typo. Now, I've got up the first version of my site that I have built myself, using Java, Stripes, Spring and Hibernate. I'm using Resin as the Web/Application server and MySQL for the database. The OS is Ubuntu and it is hosted with VPSLand.

I hoping that this version will be more stable than the previous version. If you are having any problems using the site, let me know.

Posted in  | Tags Resin, Hibernate, Spring, Ubuntu, MySQL, Java, Stripes | 3 Comments