Under what circumstances the database is suitable for adding indexes

Source: Internet
Author: User

1. A field of a table is worth a higher degree of dispersion, which is more appropriate for the keyword chosen for indexing. The primary key field and the Uniqueness Constraint field are suitable for keywords that are selected for indexing because the values of these fields are very discrete. Especially when you create an index on a primary key field, the value of cardinality (cardinality, set) is equal to the number of rows in that table. MySQL is thoughtful when it comes to dealing with primary KEY constraints and uniqueness constraints. While the database user creates the primary key constraint, MySQL automatically creates the primary index (primary index), and the index name is primary; When a database user creates a unique index, MySQL automatically creates a uniqueness index (unique index), and by default, the index is named unique The name of the field for the sex index.

2. A field that occupies less storage space is more suitable for keywords that are selected for indexing. For example, an integer field occupies less storage space than a string, so it is preferable to select the index keyword.

3. The field with fixed storage space is more suitable for the keyword chosen as index. Fields of type char are better suited to be selected as Index keywords than fields of type text.

4. The fields that are used frequently in the Where clause should be indexed, the grouping field or the Sort field should be indexed, and the connection fields of the two tables should create an index.

5. Fields that are frequently updated are not suitable for creating indexes, and fields that do not appear in the WHERE clause should not be created.

6, the leftmost prefix principle.

7, try to use prefix index.

Under what circumstances the database is suitable for adding indexes

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.