Mysql table index (ordinary index)

Table of contents

foreword

First, define the index when creating the table

2. Create an index on an existing table

1. Point to the create statement

2. Point to the alter table statement

3. Check the index execution status

Summarize


foreword

        The so-called ordinary index means that there are no additional restrictions (unique, non-empty, etc.) when creating an index. This type of index can be created on fields of any data type.


First, define the index when creating the table

CREATE TABLE tablename(

    propname1 type1,

    propname2 type2,

    ……

    propnamen type..n,

     INDEX | KEY

    [indexname] (propname [(length)] [ ASC | DESC ] ) );

        Among them, the parameters INDEX and KEY are used to specify the field as an index, and you can choose one of them, and the effect is the same; the parameter indexname is the name of the index, which can be omitted; the parameter propnamen is the name of the field corresponding to the index, the The field must be a defined field; the parameter length is an optional parameter, which refers to the length of the index, and must be a string type to be used; the parameters ASC and DESC are both optional parameters, ASC means ascending order, DESC means descending order , or ascending order if not specified.

mysql> create database school;   #create database school 

mysql> use school;   #select database school 

mysql> create table class(id int, name varchar(128) UNIQUE, teacher varchar(64), INDEX index_no(id DESC)); #Create       table class , and create it as id field index 

mysql> show create table class;  #View table structure

mysql> insert into class values(1, ' Class One ', 'Martin');   #insert record 1

mysql> insert into class values(1, ' Second Class ', 'Rock');    #insert record 2

mysql> select * from class where id > 0 ;    #Query records according to id , the results will be sorted in descending order 

2. Create an index on an existing table

1. Point to the create statement

CREATE INDEX indexname 

    ON tablename (propname [(length)] [ASC|DESC]); 

        The parameter INDEX is used to specify the field as an index, which cannot be KEY here; the parameter indexname is the name of the newly created index; the parameter tablename refers to the name of the table that needs to create an index, the table must already exist, if it does not exist, It needs to be created first; the parameter propname specifies the name of the field corresponding to the index, and the field must be a previously defined field; the parameter length is an optional parameter, indicating the length of the index, and must be a string type to be used; the parameters ASC and DESC are both It is an optional parameter, ASC means ascending order, DESC means descending order, and the default is ascending order.

mysql> create database school;   #create database school 

mysql> use school;   #select database school 

mysql> create table class(id int, name varchar(128) UNIQUE, teacher varchar(64));       #Create table class, and create it as id field index 

mysql> create index index_id on class(id ASC  ) ; 

mysql> show create table class;  #View table definition  

mysql> insert into class values(1, ' Class One ', 'Martin');   #insert record 1

mysql> insert into class values(1, ' Second Class ', 'Rock');    #insert record 2

mysql> select * from class where id > 0 ;    #Query records according to id , the results will be sorted in descending order 

2. Point to the alter table statement

ALTER TABLE tablename ADD INDEX | KEY indexname 

     (propname [(length)] [ASC|DESC]); 

        In the above statement, the parameter tablename is the table that needs to be indexed; the keyword IDNEX or KEY is used to specify the creation of a common index; the parameter indexname is used to specify the name of the created index; the parameter propname is used to specify the name of the field associated with the index; The parameter length is used to specify the length of the index; the parameter ASC is used to specify ascending sort; the parameter DESC is used to specify descending sort.

3. Check the index execution status

EXPLAIN query statement

Output result:

key : The actual index to use. If NULL, no index is used

possible_keys : Displays the indexes that may be applied to this table, one or more. If there is an index on the fields involved in the query, the index will be listed, but not necessarily actually used by the query

key_len : Indicates the number of bytes used in the index, through which the length of the index used in the query can be calculated. The shorter the value, the better!


Summarize

How to create a common index statement:

1. When creating a table, create:

        create table table name (field name data type, ... ..., index index name (field name descending order);

2. After the table is created:

        1) create index index name on table name (field name descending order);

        2) alter table table name add index index name (field name descending order);

View index performance:

explain select * from table name where condition

Guess you like

Origin blog.csdn.net/m0_65635427/article/details/130308038
Recommended