Mysql index Comparison Analysis

Source: Internet
Author: User

Mysql indexes greatly improve the query efficiency. However, not all cases require mysql indexes. Let's analyze the usage of mysql indexes.

Mysql tables have two types of keys: primary key and secondary key.

For example:

 
 
  1. CREATE TABLE layout_test (  
  2.  
  3.    col1 int NOT NULL,  
  4.  
  5.    col2 int NOT NULL,  
  6.  
  7.    PRIMARY KEY(col1),  
  8.  
  9.    KEY(col2)  
  10.  
  11. );  

Col1 is the primary key, and col2 is the secondary key. They create mysql indexes.

This is short for pk and sk.

Myisam:

In myisam, the primary key and sk data are stored in the same way. It is the row number stored on the leaf node of B-tree, that is, the pointer of the Data row.

Innodb:

It is not the same in innodb.

The primary key exists as a clustered index and stores the primary key value and other column values, transaction IDs, and rollback pointers on the leaf node of B-tee. The primary index of innodb is a clustered index)

The leaf node of sk is also different from myisam. It stores the value of primary key instead of the row pointer of data ).

The index can be used only for queries with the leftmost prefix.

The following section is from the mysql user manual:

 
 
  1. mysql> CREATE TABLE test (  
  2.            id INT NOT NULL,  
  3.            last_name CHAR(30) NOT NULL,  
  4.            first_name CHAR(30) NOT NULL,  
  5.            PRIMARY KEY (id),  
  6.            INDEX name (last_name,first_name)); 

Mysql index name is an index on last_name and first_name. This index will be used to query the specified value within a known range of last_name, last_name, and first_name. Therefore, the name index will be used in the following queries:

 
 
  1. Mysql> SELECT * FROM test WHERE last_name = "Widenius ";
  2.  
  3. Mysql> SELECT * FROM test WHERE last_name = "Widenius"
  4. AND first_name = "Michael ";
  5.  
  6. Mysql> SELECT * FROM test WHERE last_name = "Widenius"
  7. AND (first_name = "Michael" OR first_name = "Monty ");
  8.  
  9. Mysql> SELECT * FROM test WHERE last_name = "Widenius"
  10. AND first_name> = "M" AND first_name <"N ";
  11. However, the name index is not required in the following query:
  12.  
  13. Mysql> SELECT * FROM test WHERE first_name = "Michael ";
  14.  
  15. Mysql> SELECT * FROM test WHERE last_name = "Widenius"
  16. OR first_name = "Michael ";
  17.  

MySQL index usage

Insufficient mysql Indexes

Implement ADO connection to MYSQL

Introduction to mysql trigger new old

Create MySQL trigger syntax

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.