Posts filed under ‘Ruby’

Eager Loading

I have an unusual Rails application. This web app has about 1000+ rows of data that it loads each time someone hits the main page. This is pretty much the entire database. It takes around 9 seconds to load this data and draw the page, about 2 seconds of that is database access (about 400 SQL queries). Fortunately, this isn’t a public web app, as those load times would kill it.

I recently read about eager loading, a Rails feature that loads your data in with a single database call. The default database method Rails uses is to only pull data as it is needed. So, as you actually start using the data, it then performs the database queries to grab it. This is normally for the best so you don’t waste time loading data that you don’t even use. With this particular web application, the entire database is brought up when you hit the main page of this site, so eager loading seemed to make sense.

(Note: For those of you worried about the users of this app, most operations that they need to perform are done using AJAX after the initial page load, so the users aren’t suffering needlessly.)

Here’s the data model I’m using (though I’ve pulled the validation related code):

class CoreRouter < ActiveRecord::Base
has_many :ports
end

class Port < ActiveRecord::Base
belongs_to :core_router
has_many :addresses
end

class Address < ActiveRecord::Base
belongs_to :port
end

My first attempt at eager loading:
@core_routers = CoreRouter.find(:all, :include => [ :ports, :addresses])

But that fails:

ActiveRecord::ConfigurationError (Association named ‘addresses’ was not
found; perhaps you misspelled it?):

After a bit of thought, it makes sense. The CoreRouter class doesn’t have a direct association with the Address class, so it doesn’t know how to associate the data.

Fortunately, you can nest this:

@core_routers = CoreRouter.find(:all, :include => {:ports => :addresses})

This essentially joins the Ports table with the Addresses table, then joins that result to the CoreRouter table.

That shaved the time for SQL on this web app from about 2 seconds to less than 0.5 seconds.

Now, I just have to find the time to go back and rewrite the code that takes about 6 seconds to format the data for the website…

Advertisements

June 15, 2007 at 7:42 pm Leave a comment

Nice to meet you, Ruby

My introduction to Ruby started last year. I had read about Ruby on Rails and thought it sounded like a really interesting language. About August a relatively minor hurricane had just blown through Florida. Our Network Operations department monitors our store network links. Another department (the help desk) takes calls from the stores when they have issues, and are generally in charge of tracking what is going on with the stores. Since both groups had different sources of information about what was happening at the locations, they were sending excel spreadsheets back and forth to update each other on the status of the stores. The Help Desk would then send out an email with the spreadsheet attached to the management team two to three times a day. This would work, if there were only a few stores affected, but when 20-30 stores or more are having to close because of a storm, it breaks down. More often than not, the updates the Network Operations department put in their copy of the spreadsheet didn’t make it into the one sent to management, which made the NetOps team look bad.

When I heard about this, it sounded like the perfect use for a database driven web application. Members of both teams could update the same database at the same time and management could take a look at it whenever they wanted to get an up-to-date consolidated view of exactly what both teams know about the stores.

I had looked at Ruby on Rails before and was kind of excited about it. Unfortunately, I’m not the kind of person that can write an app just to learn a language. I really need to have a real end product that I need to get motivated on something like this. I essentially volunteered to write it to force myself to learn Ruby on Rails. My manager was sold on the idea and gave me permission to do it, as long as I could have it completed very quickly. Thus, StormTrack was born.

About a week later, I was done, even after a few more features were added (like exporting to csv, adding a news table, and adding a user system so there would be accountability). Yes, feature creep even happens to very small apps! Of course, this was almost a year ago, and it has not been used since, because there haven’t been any major storms. If we are very lucky, we won’t use StormTrack again this hurricane season.

May 5, 2007 at 6:02 pm Leave a comment

Blogging about programming, networking, and computers in general

Welcome to my new blog. Previously, I blogged about PHP at phpsourcery.com. As it happens, I’m doing somewhat less PHP programming now. I’ve also began to do some Ruby programming. I work with just about anything network related (Cisco Routers, Cabletron Switches, Cisco Switches, etc.) Since then, I’ve also switched from a Windows machine as my primary box to a Mac Mini… Then, I switched to an Intel iMac, just after they were introduced. So, there’s no telling what I’ll find interesting to blog about… That’s why I started the Jack of All I.T. blog.

May 2, 2007 at 4:49 pm Leave a comment


Calendar

November 2017
S M T W T F S
« May    
 1234
567891011
12131415161718
19202122232425
2627282930  

Posts by Month

Posts by Category