Mysql --- functions of index and explain _ MySQL

Source: Internet
Author: User
Mysql --- functions of index and explain MySQLexplain

Mysql explain uses explain SQL statements to help you select better indexes and write more optimized query statements. Of course, if you use like '% username %', the index will not be used. you only use like 'username % 'to use the index. of course, for common <, <=, =,>,> =, BETWEEN, IN is indexed

The explain statement shows how mysql uses indexes to process select statements and connect tables. you can add the explain statement before the select statement:

Example: explain select surname form user, company where user. id = company. id
  • The analysis result is as follows:
  • Table | type | possible_keys | key | key_len | ref | rows | Extra
  • Description of the EXPLAIN column:
  • Table
  • Which table is the data of this row?
  • Type
  • This is an important column that shows the type used by the connection. The connection types from the best to the worst are const, eq_reg, ref, range, index, and ALL.

  • 1. Full table scan 2. index scan 3. range scan 4. unique index scan 5. constant scan
    The access speed of these data access methods increases sequentially.
  • Possible_keys
  • Displays indexes that may be applied to this table. If it is null, there is no possible index. You can select an appropriate statement from the WHERE statement for the relevant domain.
  • Key
  • Actually used index. If it is NULL, no index is used. In rare cases, MYSQL selects an optimized index. In this case, you can use index (indexname) in the SELECT statement to force an INDEX or use ignore index (indexname) to force MYSQL to IGNORE the INDEX.
  • Key_len
  • The length of the index used. The shorter the length, the better.
  • Ref
  • It indicates which column of the index is used. if possible, it is a constant.
  • Rows
  • MYSQL considers that the number of rows that must be checked to return the requested data
  • Extra
  • Additional information about how MYSQL parses the query. Using temporary and Using filesort, meaning that MYSQL cannot use indexes at all, and the result is that the retrieval will be slow.


Meaning of the description returned by the extra column
Distinct
Once MYSQL finds the row that matches the row, it does not search any more.
Not exists
MYSQL optimizes left join. once it finds a row that matches the left join standard,

I. index is divided into single-column indexes and composite indexes
Single-column index: an index only contains a single column. a table can have multiple single-column indexes, but this is not a combination index.
Composite index: a cable contains multiple columns.

II. introduce the index type

1. common indexes.
This is the most basic index with no restrictions. It can be created in the following ways:
(1) create an INDEX: create INDEX indexName ON tableName (tableColumns (length); for CHAR and VARCHAR types, the length can be smaller than the actual length of the field; for BLOB and TEXT types, length must be specified, the same below.
(2) modify the table structure: alter tableName add index [indexName] ON (tableColumns (length ))
(3) when creating a TABLE, specify: create TABLE tableName ([...], INDEX [indexName] (tableColumns (length ));

2. unique index.
It is similar to the previous "normal Index". The difference is that the value of the index column must be unique, but null values are allowed. If it is a composite index, the combination of column values must be unique. It can be created in the following ways:

Java code

  1. (1) create an index:
  2. // Create an index
  3. Create unique index indexName ON tableName (tableColumns (length ))
  4. // (2) modify the table structure:
  5. Alter tableName add unique [indexName] ON (tableColumns (length ))
  6. // (3) specify the following when creating a table:
  7. Create TABLE tableName ([...], UNIQUE [indexName] (tableColumns (length ));
  8. Create TABLE testIndex (
  9. I _testID INT NOT NULL AUTO_INCREMENT,
  10. Vc_Name VARCHAR (16) not null,
  11. Primary key (I _testID ));

(1) create an INDEX: // create an INDEX create unique index indexName ON tableName (tableColumns (length) // (2) modify the table structure: alter tableName add unique [indexName] ON (tableColumns (length) // (3) when creating a TABLE, specify: create TABLE tableName ([...], UNIQUE [indexName] (tableColumns (length); create TABLE testIndex (I _testID int not null AUTO_INCREMENT, vc_Name VARCHAR (16) not null, primary key (I _testID ));


4. full-text index

Syntax for deleting an INDEX: drop INDEX index_name ON tableName

To further extract MySQL efficiency, we need to consider establishing a composite index. Vc_Name, vc_City, and I _Age are built into an index:
Alter TABLE myIndex add index name_city_age (vc_Name (10), vc_City, I _Age); -- note that when creating a TABLE, the length of vc_Name is 50. why is 10 used here? In general, the length of the name will not exceed 10, which will accelerate the index query speed, reduce the size of the index file, and increase the insert update speed.

The establishment of such a composite index is actually equivalent to the establishment
Vc_Name, vc_City, I _Age
Vc_Name, vc_City
Vc_Name

4. using indexes
So far, should you create and use indexes? But under what circumstances do indexes need to be created? IN general, you need to create an index for the columns that appear IN the where and join operations, but this is not the case because MySQL only applies to <, <=, =,>,> =, BETWEEN, IN, and sometimes like (as described later) will use the index.
Select t. vc_Name FROM testIndex t LEFT join myIndex m ON t. vc_Name = m. vc_Name where m. I _Age = 20 AND m. when vc_City = 'zhengzhou ', you need to create an index for the vc_City and I _Age of the myIndex table. because the vc_Name of the testIndex table is opened in the join clause, it is also necessary to create an index for it.

As I mentioned earlier, only like needs to be indexed in some cases? Yes. Because MySQL does not use an index when it starts with the wildcard "%" and "_", as shown in
Select * FROM myIndex where vc_Name like 'erquan %'
Will use the index, and
Select * FROM myIndex wheret vc_Name like '% erquan'
The index will not be used.

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.