MySQL database index

Overview of database indexes

An index is a structure for sorting the values ​​of one or more columns in a database table. Using an index can quickly access specific information in a database table. If you want to find a specific employee by his or her last name, the index helps to obtain information faster than searching all rows in the table.
One of the main purposes of the index is to speed up the retrieval of the data in the table, that is, to assist the information searcher to find the auxiliary data structure of the record ID that meets the restriction conditions as soon as possible.

For example, such a query: select * from table1 where id=10000. If there is no index, you must traverse the entire table until the row with ID equal to 10000 is found; after having an index (it must be an index established on the ID column), you can search in the index. Since the index is optimized through a certain algorithm, the number of searches is much less. It can be seen that the index is used for positioning.
From the perspective of data search implementation, an index is another type of file/record, which contains various records that can indicate related data records. Among them, each index has a corresponding search code, and any subset of the character segment can form a search code. In this way, the index is equivalent to a collection of all data directory items, and it can provide all kinds of effective support required for positioning for all data directory items of a given search code value.

The database index can be understood as a table of contents of a book, and the contents of the book can be searched using the table of contents.

Indexing

After setting the appropriate index, the database uses various fast positioning technologies to greatly speed up the query rate.
Especially when the table is very large, or when the query involves multiple tables, using the index can speed up the query and
reduce the database. The IO cost of the database, and the index can also reduce the cost of sorting the database.
By creating a unique index to ensure the uniqueness of the data in the data
table, the connection between the table and the table can be accelerated.
When grouping and sorting are used, the grouping and sorting time can be greatly reduced

Index pros and cons

Advantages
can quickly find the required resources
Disadvantages Take
up space

Index classification

Ordinary index
This is the most basic type of index, and it has no restrictions such as
uniqueness. The unique
index is basically the same as the previous "ordinary index" but there is one difference: all values ​​of the index column can only appear once. The
primary key must be unique. The
primary key is a unique index, but it must be specified as "PRIMARY KEY
full-text index
MySQL supports full-text index and full-text search from version 32323. The index type of full-text index in MySQL is FULLTEXT, and the full-text index can be in ARCHAR Or create a
single-column index and a multi-column index on a column of type TEXT. The
index can be an index created on a single column or an index created on multiple columns.
The leftmost principle is executed from left to right.

Index creation principle

The primary key and foreign key of the table must have an index (the foreign key of the table is the primary key of another table, and the two tables are related to some extent). The table with
more than 300 rows of data should have an index. The table is
often connected with other tables. Fields should be indexed
. Fields with poor uniqueness are not suitable for indexing. Fields that are
updated too frequently are not suitable for indexing. Fields that
often appear in the Where clause, especially fields of large tables, should be indexed.
Indexes should be built on selectivity.
Indexes on tall fields should be built on small fields. Do not build indexes for large text fields or even long fields.

bring it on! Show!

Create a normal index

方法一:可以在创建表时制作,也可以修改表
mysql> create index sy1 on kelong(name);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from kelong\G;
*************************** 2. row ***************************
        Table: kelong
   Non_unique: 1
     Key_name: sy1
 Seq_in_index: 1
  Column_name: name
    Collation: A
  Cardinality: 4
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: BTREE
      Comment: 
Index_comment: 
2 rows in set (0.00 sec)

方法二:直接修改
mysql> alter table kelong add index sy2(jiti);
Query OK, 0 rows affected (0.01 sec)
mysql> desc kelong;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(5)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(50) | NO   | MUL | NULL    |                |
| year  | int(5)      | NO   |     | NULL    |                |
| jiti  | varchar(20) | YES  | MUL | 未知    |                |
+-------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

Create a unique index

mysql> create unique index s1 on kelong(year);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc kelong;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(5)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(50) | NO   | MUL | NULL    |                |
| year  | int(5)      | NO   | UNI | NULL    |                |
| jiti  | varchar(20) | YES  | MUL | 未知    |                |
+-------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

我这边新创了一个表,接着演示
mysql> alter table banji add unique s3(name);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc banji;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| xuehao  | int(3)      | NO   | PRI | NULL    | auto_increment |
| name    | varchar(8)  | NO   | UNI | NULL    |                |
| address | varchar(60) | YES  |     | 未知    |                |
+---------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

Delete index

mysql> drop index s3 on banji;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> drop index sy2 on kelong;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table kelong drop index sy1;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

Create a composite index

mysql> create table biao(id int(3) not null,name varchar(20) not null,address varchar(60) not null,age int(3) not null,index biao(id,name,address,age));
Query OK, 0 rows affected (0.01 sec)

mysql> show index from biao\G;
*************************** 1. row ***************************
        Table: biao
   Non_unique: 1
     Key_name: biao
 Seq_in_index: 1
  Column_name: id
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: BTREE
      Comment: 
Index_comment: 
*************************** 2. row ***************************
        Table: biao
   Non_unique: 1
     Key_name: biao
 Seq_in_index: 2
  Column_name: name
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: BTREE
      Comment: 
Index_comment: 
*************************** 3. row ***************************
        Table: biao
   Non_unique: 1
     Key_name: biao
 Seq_in_index: 3
  Column_name: address
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: BTREE
      Comment: 
Index_comment: 
*************************** 4. row ***************************
        Table: biao
   Non_unique: 1
     Key_name: biao
 Seq_in_index: 4
  Column_name: age
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: BTREE
      Comment: 
Index_comment: 

Create a full-text index

方法一,创建表时创建
mysql> create table lie(
    -> id int(3) not null auto_increment,
    -> ming varchar(10) not null,
    -> xing varchar(6) not null,
    -> primary key(id),
    -> fulltext key ming_xing_fulltext(ming,xing)
    -> )engine=myisam default charset=utf8;
Query OK, 0 rows affected (0.00 sec)

方法二,在已存在表上创建
mysql> alter table dam add fulltext qw(id,name);
Query OK, 0 rows affected, 1 warning (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 1

View index

mysql> show index from dam\G;
*************************** 1. row ***************************
        Table: dam
   Non_unique: 1
     Key_name: qw
 Seq_in_index: 1
  Column_name: id
    Collation: NULL
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: FULLTEXT
      Comment: 
Index_comment: 
*************************** 2. row ***************************
        Table: dam
   Non_unique: 1
     Key_name: qw
 Seq_in_index: 2
  Column_name: name
    Collation: NULL
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: FULLTEXT
      Comment: 
Index_comment: 
2 rows in set (0.00 sec)

mysql> show keys from dam\G;
*************************** 1. row ***************************
        Table: dam
   Non_unique: 1
     Key_name: qw
 Seq_in_index: 1
  Column_name: id
    Collation: NULL
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: FULLTEXT
      Comment: 
Index_comment: 
*************************** 2. row ***************************
        Table: dam
   Non_unique: 1
     Key_name: qw
 Seq_in_index: 2
  Column_name: name
    Collation: NULL
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: FULLTEXT
      Comment: 
Index_comment: 
2 rows in set (0.00 sec)

Guess you like

Origin blog.csdn.net/Ora_G/article/details/108073118