Mysql introduced common index

  Index is a special file that contains records of all of the data in the table reference pointer. InnoDB database engine, which is an index on a table part of the space.

(1) The index of the advantages and disadvantages

  Advantages: speed up the search, query time reduction

  Disadvantages: the index is stored as a file, if the index too, will take up more disk space. But also affect the execution time of insert, update, delete the.

     Data in the index must be synchronized with personal data in the data table, if the index too, when the update data in the table, the index should be updated simultaneously, which reduces efficiency.

(2) Ordinary Index

  The most basic index, does not have the uniqueness, only to speed up the query speed.

1) Create a regular index

  create table [table] ([field name] [Field Type] [column constraints], ..., [index | key] [index name] ([Field Name]));

mysql> create table com_index1(id int,name varchar(20),key (name));
Query OK, 0 rows affected (0.02 sec)

mysql> desc com_index1;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  | MUL | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> create table com_index2(id int,name varchar(20),index com_name(name));    
Query OK, 0 rows affected (0.11 sec)

mysql> desc com_index2;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  | MUL | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.03 sec)

mysql> create table com_index(id int,name varchar(20),index (name));         
Query OK, 0 rows affected (0.02 sec)

mysql> desc com_index;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  | MUL | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

  Note 1: a table index file is a, so in the case where there is no foreign key constraints, as long as no duplicate names can be indexed in the table.

  Note 2: In the Key column values MUL generally normal index, can show index from [table] confirm the index type, if there is a column index type BTREE it is common index .

  Note 3: If you do not specify an index name, the default name index is consistent with the field name.

2) Add a regular index

  alter table [table] add [index | key] [index name] ([field name]);

mysql> alter table com_index1 add key (id);    
Query OK, 0 rows affected (0.11 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc com_index1;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  | MUL | NULL    |       |
| name  | varchar(20) | YES  | MUL | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> alter table com_index2 add index com_id(id); 
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc com_index2;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  | MUL | NULL    |       |
| name  | varchar(20) | YES  | MUL | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> alter table com_index add index (id);       
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc com_index;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  | MUL | NULL    |       |
| name  | varchar(20) | YES  | MUL | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

3) Delete the general index

  alter table [table name] drop [key | index] [index name];

mysql> alter table com_index1 drop key name;
Query OK, 0 rows affected (0.20 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc com_index1;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  | MUL | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> alter table com_index1 drop index id;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc com_index1;             
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

(3) The only index

  Set in unique index column can appear only once all the values that allow NULL. The only index generally only create data tables and data tables to create or add previously no data, if the data already exists, must ensure that the list of all the unique added value.

1) create a unique index

  create table [table] ([field name] [Field Type] [column constraints], ..., unique [index name] ([Field Name]));

mysql> create table uni_index(id int,name varchar(20),unique (id));
Query OK, 0 rows affected (0.12 sec)

mysql> desc uni_index;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  | UNI | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

  Note: In the Key column values ​​of UNI that is a unique index.

2) adding a unique index (try not to use, use it as much as possible before the data storage)

  alter table [table] add unique [index name] ([field name]);

mysql> alter table uni_index add unique (name);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc uni_index;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  | UNI | NULL    |       |
| name  | varchar(20) | YES  | UNI | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

3) Drop the unique index

  alter table [table name] drop key [index name];

mysql> alter table uni_index drop key name;      
Query OK, 0 rows affected (0.13 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc uni_index;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  | UNI | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

(4) The primary key index

  Query the database query by primary key is the fastest, each data table allows only one primary key, the primary key must be unique, does not allow NULL . Primary key and unique index, as generally only create create or add data before the data table and the table does not have the data, if the data already exists, must ensure that the list of all the unique added value.

1) Create a primary key

  create table [table] ([field name] [Field Type] [column constraints], ..., primary key [index name] ([Field Name]));

  create table [table] ([field name] [Field Type] [Field Constraints] [primary key | key], [field name] [Field Type] [Field Constraints] ...);

mysql> create table pri_index1 (id int,primary key (id));
Query OK, 0 rows affected (0.01 sec)

mysql> desc pri_index1;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | NO   | PRI | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.01 sec)

mysql> create table pri_index2 (id int key);         
Query OK, 0 rows affected (0.01 sec)

mysql> desc pri_index2;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | NO   | PRI | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> create table pri_index3 (id int primary key);
Query OK, 0 rows affected (0.02 sec)

mysql> desc pri_index3;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | NO   | PRI | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

  Note: In the column of Key value, i.e. PRI is the primary key index.

2) adding a primary key (try not to use, prior to use it as far as the data storage)

  alter table [table] add primary key [index name] ([field name]);

mysql> create table pri_index4 (id int);
Query OK, 0 rows affected (0.02 sec)

mysql> desc pri_index4;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> alter table pri_index4 add primary key (id);
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc pri_index4;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | NO   | PRI | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

3) Drop Primary

  alter table [表名] drop primary key;

mysql> alter table pri_index4 drop primary key;
Query OK, 0 rows affected (0.13 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc pri_index4;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | NO   |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.01 sec)

(5) The composite index

  An index may comprise one or more columns, when the index comprising two or more columns, at this time the index is called a composite index. Basic create, add, delete mode does not change, only the field name portion may be more than one, separated by commas between.

  create

mysql> create table comp_index(id int,name varchar(20), primary key (id,name));
Query OK, 0 rows affected (0.01 sec)

mysql> desc comp_index;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| name  | varchar(20) | NO   | PRI | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

  Add to

mysql> create table comp_index1(id int,name varchar(20));
Query OK, 0 rows affected (0.25 sec)

mysql> alter table comp_index1 add primary key (id,name);
Query OK, 0 rows affected (0.17 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc comp_index1;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| name  | varchar(20) | NO   | PRI | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

  delete

mysql> alter table comp_index drop primary key;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc comp_index;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   |     | NULL    |       |
| name  | varchar(20) | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

(6). Full-Text Indexing

  Full-text index, also known as full-text search is a critical technology in the search engine. It can use a variety of intelligent algorithms to analyze the frequency and importance of the text in the keyword text, and then filter out the desired results according to certain rules intelligent algorithm. 3.2 began to support full-text indexing, but can not support the Chinese correctly; 5.7.6 built ngram start full-text search plug-in, to support Chinese.

  Legacy of the full-text index can only be used on a table MyISAM database engine, but on 5.6.24 InnoDB also joined the full-text index. It is not only supported field type char, varchar, and text of.

1) Create a full-text index

  create table [table] ([field name] [Field Type] [column constraints], ..., fulltext key [index name] ([Field Name]));

mysql> create table ful_index(id int,name varchar(20),age varchar(20),fulltext key (name));
Query OK, 0 rows affected (0.25 sec)

mysql> desc ful_index;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  | MUL | NULL    |       |
| age   | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> show index from ful_index;
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table     | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| ful_index |          1 | name     |            1 | name        | NULL      |           0 |     NULL | NULL   | YES  | FULLTEXT   |         |               |
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

  Note: Key value here though is MUL, but using the show index from [table name] can see the actual type of index is FULLTEXT.

2) add a full-text index

  alter table [table] add fulltext key [index name] ([field name]);

mysql> alter table ful_index add fulltext key (age);
Query OK, 0 rows affected (0.26 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from ful_index;
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table     | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| ful_index |          1 | name     |            1 | name        | NULL      |           0 |     NULL | NULL   | YES  | FULLTEXT   |         |               |
| ful_index |          1 | age      |            1 | age         | NULL      |           0 |     NULL | NULL   | YES  | FULLTEXT   |         |               |
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.01 sec)

3) delete the full-text index

  alter table [table name] drop key [index name];

mysql> alter table ful_index drop key age;
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from ful_index;
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table     | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| ful_index |          1 | name     |            1 | name        | NULL      |           0 |     NULL | NULL   | YES  | FULLTEXT   |         |               |
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

(7) The index founding principles

  The index is not better: 1 do not need to create an index when the amount of data;.

            2. The change in the value of listed species do not need much to create the index;

            3. Sort frequently (order by) and packet (group by) the need to index the column;

            4. The values ​​in column requires a unique resistance, can be used a unique primary key index or indexes, but each table can have only one primary key index, and can not be NULL.

Guess you like

Origin www.cnblogs.com/diantong/p/10985718.html