MySQL understands the indexing and indexing principles, and mysql understands the indexing principles.

Source: Internet
Author: User

MySQL understands the indexing and indexing principles, and mysql understands the indexing principles.

[Understanding indexes]

To understand indexes, you must first understand how data is stored on the hard disk. Different storage engines may adopt different measures. For example, the MySQL client uses MyISAM by default, and the engine creates separate files for each table.

Whether or not a separate file is created for each table, the operating system reads data from the hard disk to the memory, which is always in the unit of page. Therefore, to obtain a specific record, you must read the page containing the record.

When obtaining a record, MySQL can use two different methods: one-by-one scan or index access. Assume that the first method is used to obtain records with the id of 1234, and the first 1233 records must be accessed sequentially and sequentially. Besides, I/O overhead of each data page read must be considered. If an index is used, you can quickly read the target page to obtain the target Record Based on the page indicated by the index and the location of the record on the page.

Except for getting specific rows, using hash is very convenient. In other cases, both (by default) use B tree to build indexes. Tree B is a balanced multi-Cross-tree. The number of records stored on each node depends on the space occupied by the value. This is the same as the number of records stored on each data page. The values in the node are arranged in non-descending order, and the values in the node are always less than or equal to the values in the nodes pointing to it.

When MySQL uses the B-tree to construct an index, the leaf points to a specific page and record. And a leaf has a pointer pointing to the next leaf.

 

Note the following before using indexes:

(1) only set indexes for the fields to be queried by where and order by to avoid meaningless hard disk overhead;

(2) composite indexes support prefix indexes;

(3) When updating a table, MySQL will automatically update the index to maintain a tree balance if adding or deleting records;

I. Importance of Indexes

The index is used to quickly find rows with a specific value in a column. If no index is used, MySQL must start with 1st records and read the entire table until related rows are found. The larger the table, the more time it takes. If the column to be queried in the table has an index, MySQL can quickly find the data file in the middle of a location, and there is no need to read all the data. Note that if you want to access most rows, sequential reading is much faster, because disk search is avoided.

There are three types of indexes:

Index ---- normal index, data can be repeated

Unique ---- unique index, which must be unique for all records

Primary key ---- primary key index, that is, the corresponding column must be the primary key on the basis of the unique index.


MySQL uses indexes for <, <=, =,>,> =, BETWEEN, IN, and like statements not starting with %.

Ii. Principles

1. If there is too little data in a single table, indexing will affect the speed.

2. When filtering conditions such as 'ORDER BY' and 'group by' after 'where', it is best to add an index to the following fields. Select indexes such as primary key, UNIQUE, and INDEX based on the actual situation, but the more indexes, the better.

3. join queries, subqueries, and other multi-Table operations require indexed joined Fields

Related Article

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.