Rails performance Optimization

Source: Internet
Author: User

1. The importance of performance optimization

Reduce server stress (quantization is the execution time)

Increase the user's goodwill (quick!) )

2. ActiveRecord: Table Association

ActiveRecord can use associations to define the correlation between tables. So far we have learned to use ActiveRecord to manipulate the database, but have not yet fully developed the feature of the relational database, which is to correlate tables through primary key and foreign keys

Primary key This field is in rails, which is conventionally called an ID, and the type is an integer and incremented. The foreign key field is known as the {model_name}_id, and the type is an integer

3. Relationship Associations

Belongs_to,has_one,has_many parameter class_name, Foreign_key,conditions,order, dependent

Dependent can be set to delete its Has_many object when the object is deleted:d Estroy will perform attendee destroy callback:d Elete will not perform attendee destroy callback: Nullify this is the default value, Won't help. Delete Attendee

Many-to-many, trough, such as has_many:firms,: through =: firm_brands

4. Talk about include

The event object captured through joins does not include its associated object. If a record of its associated object is required, includes is used.

Includes can pre-read the associated records to avoid n+1 problems

No relationship, no include, so use Belongs_to,has_one,has_mang more.

5. Habits

Do not use double quotation marks when you can use single quotation marks.

Try to be as simple as find (: All)

Unnecessary return, unnecessary assignment

Methods (especially instance methods) to minimize the query, multi-use relationships, in the include can be very good to avoid n+1

Rails performance Optimization

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.