MySQL database aspect

Source: Internet
Author: User
The first question first:
In principle, the index can be added to any field, but some fields do not need to be indexed, or, instead, add a bad.

1, the table's primary key, the foreign key must have the index;
2, the data volume of more than 300 of the table should be indexed;
3. Tables that are often connected to other tables should be indexed on the connection field;
4. Fields that often appear in the WHERE clause, especially for large tables, should be indexed;
5, the index should be built on the field of high selectivity;
6, the index should be built on the small section, for large text fields or even long fields, do not build index;
7, the establishment of composite index needs careful analysis; try to consider using single-field indexes instead:
8, frequent data operation of the table, do not set too many indexes;
9. Delete useless indexes and avoid negative impact on execution plan;
These are some common criteria for establishing an index. Word, the establishment of the index must be cautious, the need for each index should be carefully analyzed, to establish the basis. Because too many indexes and inadequate, incorrect indexes are not good for performance: Each index established on the table increases the storage overhead, and the index increases processing overhead for insert, delete, and update operations. In addition, too many composite indexes, in the case of single-field index, generally have no value; Conversely, it also reduces performance when data is being deleted, especially for tables that are frequently updated, with greater negative impact.
Second question:
The index is built on the primary key as a unique index, but the unique index is not necessarily built on the primary key, because there can be multiple unique indexes.
The difference between a primary key and an index is as follows:
A primary key is a constraint, and a unique index is an index, and the two are essentially different.
The primary key must be created with a unique index, and the uniqueness index is not necessarily the primary key.
The uniqueness index column allows null values, while primary key columns are not allowed to be null.
When the primary key column is created, it is not a null value + a unique index by default.
A primary key can be referenced by another table as a foreign key, and a unique index cannot.
A table can create a maximum of one primary key, but multiple unique indexes may be created.
Primary keys are more suitable for unique identities that are not easily changed, such as auto-increment columns, Social Security numbers, and so on.

MySQL database aspect

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.