Rails creates database Indexes

Source: Internet
Author: User

Take the classic customer-order as an Example

1. directly create an index when creating a data table


Check the migration file of order. rails automatically adds the index for us:


2. Manually attach an index

In this case, the data table is created as a foreign key created by a common field.


To create an index, You need to manually create a migration to add the index:

Modify the migration file and manually add the index


3. Add an index to the "to-be" relationship.

Taking man-address as an example, directly creating an intermediate table does not automatically add indexes. Therefore, you need to manually add indexes to the intermediate table:


4. Suggestions

It is best to add indexes for all foreign keys.

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.