Rails in the real world, part 3rd: Optimizing ActiveRecord

Source: Internet
Author: User
Tags empty object model ruby on rails

Troubleshoot common performance issues

Introduction: ActiveRecord is an excellent persistence framework, but because the framework often hides some of the low-level details, it is easy to generate performance problems. Through this article, we understand some of the most common problems and how to solve them.

Ruby on Rails programming often spoils you. This evolving framework frees you from the tedium of other frameworks. You can express your intent with a few lines of code that you are accustomed to. You can also use ActiveRecord.

For an old Java™ programmer like me, ActiveRecord is a little rusty. Through the Java framework, I usually build a mapping between independent models and schemas. A framework like this is the mapping framework. With ActiveRecord, I only define the database schema: either SQL or a Ruby class called migration (migration). The frameworks that set object model design on top of the database structure are called wrapper frameworks. Unlike most packaging frameworks, Rails can discover the characteristics of an object model by querying database tables. Unlike building complex queries, I use models to traverse relationships in Ruby, not SQL. As a result, I have both the simplicity of the packaging framework and most of the functionality of the mapping framework. ActiveRecord is easy to use and extend. Sometimes, even some are too simple.

Like any database framework, ActiveRecord makes it very easy for me to make a lot of trouble. I can get too many columns and easily omit important structured database features, such as indexes or empty constraints. I'm not saying that ActiveRecord is a bad frame. Only if you need to expand, you need to know how to strengthen your application. In this article, I'll take you through some of the important optimizations that you might need when using the unique persistence framework of Rails.

Basic management

Generating model-supported models is exceptionally easy, requiring little code, that is, script/generate model Model_name. As you know, the command can generate models, migrations, unit tests, and even a default fixture. It's tempting to fill in some data columns in the migration and enter some test data, write several tests, and add a few validations. But please reconsider. You should consider the overall database design, paying special attention to these things:

Rails will not allow you to get rid of basic database performance issues. The database needs information that is often indexed for good performance.

Rails will not let you get rid of data integrity issues. Although most Rails developers don't like to keep restrictions in the database, you should consider things like empty columns.

Rails provides convenient default properties for many elements. Sometimes, default properties such as the length of a text field can be too large for most practical applications.

Rails does not force you to create an effective database design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.