MySQL Quick Search-Index

MySQL quick check

Because I often forget some mysql statements, keywords, operations, etc. in my daily work and study, I recently took some time to write the following content about mysql. It's like a dictionary


Reset mysql password
Data type
operators
Common functions
Data integrity
Basic operations of the database
Operations on the table itself Operations
on data in the table
Subqueries
Multi-table connections Views
in this article Preprocessing SQL statements Custom functions and stored procedures Programming in MySQL




Introduction

An index is a structure that sorts the values ​​of one or more columns in a database table and allows quick access to specific information in a database table. If you want to find a specific employee by his or her last name, an index can help you get the information faster than searching for all the rows in the table.
One of the main purposes of the index is to speed up the retrieval of data in the table, that is, an auxiliary data structure that can help information searchers find record IDs that meet restricted conditions as quickly as possible.
For example, this query: select * from table1 where id=10000. If there is no index, the entire table must be traversed 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 by a certain algorithm, the number of searches is much less. It can be seen that the index is used for positioning.
Source: Baidu Encyclopedia

Indexes should only be created on tables with a large amount of data and many queries on the table but few updates; indexes should be built on tables that often require sorting, grouping, and joint operations.
If it is a table that frequently needs to update content (insert, update, delete), you should avoid using indexes; tables with many repeated records should not create indexes.

Create index

create indexcreate index

CREATE [UNIQUE | FULLTEXT | SPATIAL] INDEX 索引名
    [USING {
   
   BTREE | HASH}]
    ON 表名 (列名 [(长度)] [ASC | DESC],...)

unique:唯一索引,指定unique索引的列的值必须是唯一的,允许空值。如果是组合索引则组合必须唯一。
fulltext:全文索引,制定fulltext索引的列支持值的全文查找,允许重复值和空值。
spatial:空间索引,对数据类型是MySQL支持的4种空间数据类型geometrypointlinestringpolygon的字段建立的索引。
普通索引:不指定上面的关键字的索引(只写create index)。是mysql中的基本索引,允许重复值和空值。
主键索引:使用关键字primary key的列会生成主键索引,不允许空值和重复值。

索引名:为创建的索引取的名字

using {
   
   btree | hash}:使用的索引类型,不写默认是btree

列名[(长度)]:指定对那个列(字段)生成索引;后面可选的长度是指定使用该字段中值前n(长度)个字符当作索引,不写则使用字段值的全部字符。
一般来说,指定长度是为了让生成的索引文件占用空间更小。
指定长度的话会比使用全字符的索引速度慢一些。
如果列中前n(长度)个字符是不同的,则这种指定长度的索引比使用全部字符的索引速度不会差别太大。

[asc | desc]:升序还是降序


# 举例
# 给user表的name字段创建一个普通索引
create index name_index on user (name);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

alter table creates index

alter table 表名
add [unique | ...] index [索引名](列名[(长度)] [{
   
   asc | desc}],...);

# 为commodity表中的name字段创建一个降序的普通索引
alter table commodity
add index (name(10) desc);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

Specify the index when creating the table

# 创建一个test_table表,并为id字段创建主键索引,
# 为tel字段创建唯一索引
# 为height,weight字段创建组合普通索引
create table test_table(
id int,
name char(10),
tel char(11),
height int,
weight int,
primary key (id),
unique index (tel),
index (height,weight)
);
Query OK, 0 rows affected (0.02 sec)

View index

show index from 表名;

# 查看表test_table的索引
show index from test_table;
表名			  是否不是唯一值  索引名      索引中的序列号   列(字段)名 列在索引中的存储方式  索引在唯一    。。。。                      索引类型  。。。。
																A:升序 NULL:无分类  值的数据的
																					估值
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table      | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| test_table |          0 | PRIMARY  |            1 | id          | A         |           0 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
| test_table |          0 | tel      |            1 | tel         | A         |           0 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
| test_table |          1 | height   |            1 | height      | A         |           0 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
| test_table |          1 | height   |            2 | weight      | A         |           0 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
4 rows in set (0.00 sec)

Delete index

# 方法一
drop index 索引名 on 表名;

# 方法二
alter table 表名 drop index 索引名;

More

create index

Guess you like

Origin blog.csdn.net/weixin_45345384/article/details/117134271