(8) MySQL index operation

(1) Prepare the environment

mysql> create table t1(id int,name varchar(50));
mysql> \d $$
mysql> create procedure autoinsert_t1()
    begin
    declare i int default 1;
    while(i<200000)do
    insert into school.t1 values(i,'ccc');
    set i=i+1
    ;
    end while;
    END $$
mysql> \d ;
mysql> show create procedure autoinsert_t1\G
mysql> call autoinsert_t1();

(2) Create an index

1) Create an index when creating a table

Create a common index: mysql> create table departname(id int, name varchar(50), comment varchar(50), index(name));
create a unique index: mysql> create table department2(id int, name varchar(50), comment varchar(50),unique index(name));
Create a full-text index: mysql> create table department3(id int,name varchar(50),log text,fulltext index(log)); \Index the content of the article for the article
Create a multi-column index: mysql> create table department5(id int,name varchar(50),comment varchar(50),index index_name_comment(name,comment));
\Create a multi-column index It is recommended to give the index a name

2) Create an index on an existing table: create

Syntax: create [unique|fulltext|spatial] index index name on table name (field [(length)] [ASC|DESC])
Create ordinary index: mysql> create index index_name on department(name); \index keyword MUL
create Unique index: mysql> create unique index index_id on department(id);
create full-text index: mysql> create fulltext index index_name on department(name);
create multi-column index: mysql> create index index_name_comment on department(name, comment);

3) Create an index on an existing table: alter table

Syntax: alter table table name add [unique|fulltext|spatial] index index name (field [(length)] [ASC|DESC])
Create a common index: alter table department add index index_name(name);
create a unique index: alter table department add unique index index_name(name);
Create a full-text index: alter table department add fulltext index index_name(name);
Create a multi-column index: alter table department add index index_name_comment(name,comment);

(3) Manage indexes

1) View index

Syntax: show create table table name\G

2) Test index

Syntax: explain select * from table name where condition

3) delete the index

Syntax: drop index index name on table name;
mysql> drop index index_name on department6;
Note: first use show create table table name\G to view the index name, for example KEY index_name( name) index_name is the index name

(4) Test index:

Before indexes are not used: pay attention to keys and rows

mysql> explain  select * from t1 where id=190000;
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows   | filtered | Extra       |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+
|  1 | SIMPLE      | t1    | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 200186 |    10.00 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+

After creating the index:

mysql> explain  select * from t1 where id=190000;
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key      | key_len | ref   | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | t1    | NULL       | ref  | index_id      | index_id | 5       | const |    1 |   100.00 | NULL  |
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

Use an index: The query statement uses a where condition, and the column used by the condition must be an index

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324761415&siteId=291194637