MySQL can only use the leftmost part of the definition index

Source: Internet
Author: User

Lab Environment: MySQL 5.7.17

1. Table structure as follows, establish primary key in ID,NAME,TX column

mysql> show create table txtx;+-------+------------------------------------------------ --------------------------------------------------------------------------------------------------------------- ----------------------------------------+| table | create table                                                                                                                                                                                                    |+-------+---- --------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------+| txtx  |  CREATE TABLE  ' Txtx '   (   ' id '  int (one)  NOT NULL,   ' name '  char (2)  NOT NULL,   ' TX '  char (3)  NOT NULL,   ' Id1 '  int ( One)  DEFAULT NULL,  PRIMARY KEY  (' id ', ' name ', ' TX ')  ENGINE=InnoDB  Default charset=gbk |+-------+--------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------+1  row in set  (0.00 SEC)


2. View Execution Plan

Mysql> explain select * from txtx where id=1 and id1 =1  and tx= ' TX ', +----+-------------+-------+------------+------+---------------+---------+---------+--- ----+------+----------+-------------+| id | select_type | table |  Partitions | type | possible_keys | key     | key_ len | ref   | rows | filtered | extra        |+----+-------------+-------+------------+------+---------------+---------+---------+- ------+------+----------+-------------+|  1 | simple      |  txtx  | NULL       | ref  |  primary       | primary | 4        | const |     1 |    33.33 | using where |+----+------------ -+-------+------------+------+---------------+---------+---------+-------+------+----------+-------------+1  row in set, 1 warning  (0.00 SEC)


As you can see from the execution plan above, although the query uses the ID and TX columns in the WHERE statement, only the ID column is used because MySQL can only use the leftmost portion of the definition index.


This article is from the "Corasql" blog, make sure to keep this source http://corasql.blog.51cto.com/5908329/1913142

MySQL can only use the leftmost part of the definition index

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.