✓ I'm available for hire! Check out my open source work on Github or drop me an email

Douglas F Shearer

Posts Tagged with mysql

There are 4 matching posts.

Friendly URL Performance In Rails

During a discussion with Nick we wondered what clean URLs cost in terms of performance. It’s certainly nice to use clean URLs such as /blog/my_post_title instead of numerical URLs such as /blog/123, for ease of remembering, and verbosity. So how to go about testing this?

Test Setup

  • Macbook Pro 2.16Ghz C2D. 2GB RAM.
  • Rails 1.2.3
  • MySQL 5.0.37 (Query Cache turned off)

I started by creating a simple Rails model with only an id and a permalink of the style shown above, created by iterating over the OS X words file. Over a million instances of this were then created in the database. I then randomly selected 10,000 of these to be found by by my tests.

Non-Indexed = Slow!

By timing this using the Rails benchmarker script, and taking away the 6s of setup involved in each test, I got the following times from finding the 10,000 records:

  • find(id) : 12.0s
  • find_by_permalink(permalink) : 4453s (Over an hour)

I didn’t think these results were even worth generating a graph for, the difference is startling. Using the simple permalinks is almost 400 times slower than using the numerical IDs. But all is not lost.

Indexing = Fast

So how can we make this faster? The fastest way to do this is to add an index to the permalink field, you can do this in a Rails migration with the following:



  add_index :samples, :permalink

If you want an index on more than just one field, feed the second attribute an array of field names.

The graph below shows just how close using an index on the permalink gets us. 1.8s extra for our 10,000 finds, with nice URLs rather than numeric IDs. That’s pretty acceptable, especially for a production app that needs to be fast. But can we make it faster?

Configuration Over Convention

The rails find_by_foo methods are nice, but there are faster options. How about manually setting the conditions?



  find( :first, :conditions => [ ":permalink = ?", permalink ] )

Or how about doing all of the SQL manually?



  find_by_sql(["SELECT * FROM samples WHERE permalink = ?", permalink])

So what does this give us? By just setting the :conditions manually, we get down to 12.1s, scratching at the door of the ID based find. And the entirely manual SQL? 10.9s, a whole second faster than our original query. (Obviously we can manually rewrite the find query to use SQL, making it as fast, if not faster)

Conclusion

If you are going to use permalinks or search on some other field of your tables, make sure to use an index. Other speedups could be obtained by writing the SQL yourself, but these are too small to be worth the hassle if your queries become more complicated in the future.

P.S. Loving the WIFI on GNER Trains, posted at over 100mph!

 
 

Downtime

My webserver was down for about 20 hours last night and today, this was while I updated it to Fedora Core 5. The update was successful and everything is back online.

I did have a few difficulties though; MySQL wasn’t liking one of the queries I use on my blog, i figured it must be something to do with the new version not dealing with lower and uppercase named columns in the same way. I re-wrote the query and now it works fine. Secondly, every time I logged into this site, it logged me straight back out again! I soon discovered that this was due to the server not being able to save PHP Sessions, as the folder pointed to by the session path didn’t exist! After creating the folder and setting the correct permissions, everything worked fine.

While I was at it, I took the liberty of naming my iBook, my workstation and my server, so as I can log into them without having to remember their individual IP addresses.

Update

For some reason YUM isn’t playing nice with me, so I don’t have GD image resize support at the moment. If any images are missing on any of my pages, it is because of this. I’m working to fix the problem now.

Update 2

Found a workaround for the problem (For those interested, the $releasever variable was returning Null to the .repos files, causing Yum to return errors. Solution was to replace every instance of the variable with a 5 in the repos), which has now allowed me to install GD. All running fine and dandy now.

 
 

August 29th 2005

Add your comment

Massive Re-write!

Had a massive re-write of my base code today. The most significant change is the reduction in mySQL database queries on the blog page from a maximum possible of 102 to 2! I can’t quite believe my code was this inefficient. WIll be sure to use the power of SQL in future rather than multiple queries with PHP middlework.

Thanks to Ben for his testing of stuff while I made these changes, and also for testing the swear filter (again!).

 
 

First Exam

Had my first August exam today, went really well. I think all the work I did for building this site helped me out on at least the SQL and XML front. Computer Science down, maths tomorrow! Woo! :-/

Cycled up to the exam today, and cycled back, both times I had large coughing fits upon stopping. Maybe time to go to the doctors I think. Been two weeks off the bike now, but I haven’t had any of last weeks fevers again thankfully

Downloaded a free open source 3D modeling app today called Blender 3D. Works on WIndows, OS X and Linux, so something for everyone. The 5 minutes I played with it it seems similar to maya in terms of controls. Will play around with it more when I have time, but expect some 3D artwork soon.