Under what circumstances do I need to create a MySQL index? _ MySQL

Source: Internet
Author: User
Tags mysql index
This article mainly introduces how to create a MySQL index? This article also describes which situations are not suitable for creating MySQL indexes. if you need an index, you can refer to the index to improve data retrieval efficiency and reduce the I/O cost of the database, in addition, indexes can reduce database sorting costs. Sorting group operations mainly consume CPU resources and memory. Therefore, using indexes in sorting group operations can greatly reduce CPU resource consumption.

How can I determine whether to create an index?

1. fields frequently used as query conditions

We all know this. What is frequent teaching? Analyze all the SQL statements you run. It is best to list them one by one. After analysis, we find that some of the fields are used in most SQL statement queries, so we can create an index for them.

2. fields with poor uniqueness are not suitable for indexing.

What is a field with poor uniqueness. Such as status field and type field. Fields that only store fixed values, such as user logon status and message status. This involves the features of index scanning. For example, you can use an index to search for data with key values A and B, and use A to find the data that matches A. the data is displayed on page X and then continues scanning, the storage engine then discards the data on the X page and stores the data on the Y page, until all the data corresponding to A is searched, and then field B is searched, and the data corresponding to field B is found on page X. then, the system scans the page X again, the X page is scanned twice or more times. Similarly, the same data page may be read and discarded multiple times, and read. This will undoubtedly add a great IO burden to the storage engine.

3. fields updated too frequently are not suitable for index creation

When you create an index for this field, when you update the field data again, the database automatically updates its index, so when this field is updated too frequently, the index is constantly updated, and the impact of performance can be imagined. The fields updated after dozens of queries are compared to the index creation rules. If a field is updated multiple times in the same time period, you cannot create an index for it.

4. fields that do not appear in the where condition should not be indexed

I believe everyone knows this.

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.