MySQL-索引的增删改

1、索引的分类

  • 功能逻辑上划分:
    • 普通索引 :创建索引时不加任何限制条件,只是用来提高查询效率。可以创建在任何数据类型中,其值是否唯一和非空由字段本身的完整性约束条件决定。
    • 唯一索引:使用UNIQUE参数可以设置索引为唯一索引。创建该索引时限制该索引的值必须是唯一的,但允许由空值,一张表里可以有多个唯一索引
    • 主键索引:是一种特殊的唯一索引,在唯一索引的基础上添加了不为空的约束,一张表里至多只有一个主键索引
    • 全文索引:是目前搜索引擎中使用的一种关键技术,能够利用 分词技术 等多种算法智能分析出文本文字中关键词的频率和重要性,然后按照一定的算法规则智能的筛选出我们想要的搜索结果。适合大型数据集
  • 物理实现方式:
    • 聚簇索引
    • 非聚簇索引
  • 作用字段个数:
    • 单列索引:在表中单个字段创建索引。单列索引只根据该字段进行索引,可以是普通索引、唯一索引、全文索引。一张表可以有多个单列索引。
    • 联合索引:是在表的多个字段组合上创建一个索引。该索引指向创建时对应的多个字段,可以通过这些字段进行查询,但是只有查询条件中使用了这些字段中的第一个字段时才会被使用。遵循最左前缀集合

2、创建索引

2.1、创建表时创建索引

  • 语法格式如下:create table table_name [ col_name data_type ] [ unique | fulltext | spatial ] [ index | key ] [ index_name ] (col_name [ length ]) [ asc | desc ] invisible

    • unique | fulltext | spatial:可选参数,分别标识唯一索引,全文索引,空间索引
    • index | key : 指定创建索引
    • index_name : 指定创建索引名称,为可选参数,不指定则默认为 col_name为索引名
    • col_name : 为需要创建索引的字段列,该列必须在数据表中定义的列中选择
    • length : 可选参数,标识索引的长度,只有字符串类型的字段才能指定索引长度
    • asc | desc : 指定升序或降序的索引值存储
    • invisible :设置索引的可见性
  • SQL语句如下:

mysql> create table test_index(id int,name varchar(10) ,age int ,info varchar(30), index(name));
Query OK, 0 rows affected (2.38 sec)

  • 查看创建索引
#方式一
mysql> show create table test_index\G
*************************** 1. row ***************************
       Table: test_index
Create Table: CREATE TABLE `test_index` (
  `id` int DEFAULT NULL,
  `name` varchar(10) DEFAULT NULL,
  `age` int DEFAULT NULL,
  `info` varchar(30) DEFAULT NULL,
  KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (3.32 sec)

#方式二:
mysql> show index from test_index\G
*************************** 1. row ***************************
        Table: test_index
   Non_unique: 1
     Key_name: name
 Seq_in_index: 1
  Column_name: name
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment: 
Index_comment: 
      Visible: YES
   Expression: NULL
1 row in set (4.37 sec)
  • 查看索引是否使用,关键字explain:
mysql> explain select * from test_index where name = 'rqtanc';
+----+-------------+------------+------------+------+---------------+------+---------+-------+------+----------+-------+
| id | select_type | table      | partitions | type | possible_keys | key  | key_len | ref   | rows | filtered | Extra |
+----+-------------+------------+------------+------+---------------+------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | test_index | NULL       | ref  | name          | name | 43      | const |    1 |   100.00 | NULL  |
+----+-------------+------------+------------+------+---------------+------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.37 sec)

2.2、通过alter 语句修改索引

  • 语法格式 : alter table (table_name) add index [index_name] (col_name [length])
mysql> alter table test_index add index age;

mysql> show index from test_index\G
*************************** 1. row ***************************
        Table: test_index
   Non_unique: 1
     Key_name: name
 Seq_in_index: 1
  Column_name: name
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment: 
Index_comment: 
      Visible: YES
   Expression: NULL
*************************** 2. row ***************************
        Table: test_index
   Non_unique: 1
     Key_name: age
 Seq_in_index: 1
  Column_name: age
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment: 
Index_comment: 
      Visible: YES
   Expression: NULL
2 rows in set (0.08 sec)

2.3、通过create index 语句创建索引

  • 语法格式: create index (index_name) on table_name(col_name [length])
mysql> create index idx_info on test_index(info);
Query OK, 0 rows affected (0.18 sec)

Records: 0  Duplicates: 0  Warnings: 0
mysql> show index from test_index\G
*************************** 1. row ***************************
        Table: test_index
   Non_unique: 1
     Key_name: name
 Seq_in_index: 1
  Column_name: name
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment: 
Index_comment: 
      Visible: YES
   Expression: NULL
*************************** 2. row ***************************
        Table: test_index
   Non_unique: 1
     Key_name: age
 Seq_in_index: 1
  Column_name: age
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment: 
Index_comment: 
      Visible: YES
   Expression: NULL
*************************** 3. row ***************************
        Table: test_index
   Non_unique: 1
     Key_name: idx_info
 Seq_in_index: 1
  Column_name: info
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment: 
Index_comment: 
      Visible: YES
   Expression: NULL
3 rows in set (0.05 sec)

3、删除索引

3.1、使用alter table 删除索引
  • 语法格式: alter table table_name drop index index_name
mysql> show index from test_index;
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table      | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| test_index |          1 | name     |            1 | name        | A         |           0 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
| test_index |          1 | age      |            1 | age         | A         |           0 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
| test_index |          1 | idx_info |            1 | info        | A         |           0 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
3 rows in set (0.10 sec)

mysql> alter table test_index drop index idx_info;
Query OK, 0 rows affected (0.80 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from test_index;
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table      | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| test_index |          1 | name     |            1 | name        | A         |           0 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
| test_index |          1 | age      |            1 | age         | A         |           0 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
2 rows in set (0.02 sec)

3.2、使用 drop index 语句删除索引
  • 语法格式:drop index index_name on table_name;
mysql> show index from test_index;
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table      | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| test_index |          1 | name     |            1 | name        | A         |           0 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
| test_index |          1 | age      |            1 | age         | A         |           0 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
2 rows in set (0.02 sec)

mysql> drop index age on test_index;
Query OK, 0 rows affected (0.52 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from test_index;
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table      | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| test_index |          1 | name     |            1 | name        | A         |           0 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
1 row in set (0.03 sec)

4、隐藏索引

  • MySQL 8开始支持隐藏索引,只需要将待删除的索引设置隐藏索引,使查询优化器不在使用该索引(即使使用 force index(强制使用索引),优化器也不会使用该索引),确认将索引设置为隐藏索引后不受任何影响,则可以删除索引
  • 先将索引设置为隐藏索引,在删除索引的方式就是软删除

注意点:
主键不能被设置为隐藏索引。
当表中没有显式主键时,表中第一个唯一非空索引会称为隐式主键,也不能设置为隐藏索引

4.1创建表时创建隐藏索引

  • 基本格式:create table table_name [ col_name data_type ] [ unique | fulltext | spatial ] [ index | key ] [ index_name ] (col_name [ length ]) [ asc | desc ] invisible
  • SQL语句如下:
mysql> create table test1_index(id int,name varchar(10) ,age int ,info varchar(30), index idx_invisible (name) invisible);
Query OK, 0 rows affected (0.70 sec)

mysql> show index from test1_index;
+-------------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table       | Non_unique | Key_name      | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-------------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| test1_index |          1 | idx_invisible |            1 | name        | A         |           0 |     NULL |   NULL | YES  | BTREE      |         |               | NO      | NULL       |
+-------------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
1 row in set (0.55 sec)

### 4.2、通过alter 语句修改索引

  • 语法格式 : alter table (table_name) add index [index_name] (col_name [length]) invisible
mysql> alter table test1_index add index idx_age(age) invisible;
Query OK, 0 rows affected (0.58 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from test1_index;
+-------------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table       | Non_unique | Key_name      | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-------------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| test1_index |          1 | idx_invisible |            1 | name        | A         |           0 |     NULL |   NULL | YES  | BTREE      |         |               | NO      | NULL       |
| test1_index |          1 | idx_age       |            1 | age         | A         |           0 |     NULL |   NULL | YES  | BTREE      |         |               | NO      | NULL       |
+-------------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
2 rows in set (0.00 sec)

4.3、通过create index 语句创建索引

  • 语法格式: create index (index_name) on table_name(col_name [length]) invisible
mysql> create index idx_info on test1_index(info) invisible;
Query OK, 0 rows affected (0.21 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from test1_index;
+-------------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table       | Non_unique | Key_name      | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-------------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| test1_index |          1 | idx_invisible |            1 | name        | A         |           0 |     NULL |   NULL | YES  | BTREE      |         |               | NO      | NULL       |
| test1_index |          1 | idx_age       |            1 | age         | A         |           0 |     NULL |   NULL | YES  | BTREE      |         |               | NO      | NULL       |
| test1_index |          1 | idx_info      |            1 | info        | A         |           0 |     NULL |   NULL | YES  | BTREE      |         |               | NO      | NULL       |
+-------------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
3 rows in set (0.03 sec)

4.4、修改索引的可见性

  • 语法格式: alter table table_name alter index index_name (visible | invisible );
mysql> alter table test1_index alter index idx_info visible;
Query OK, 0 rows affected (0.21 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from test1_index;
+-------------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table       | Non_unique | Key_name      | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-------------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| test1_index |          1 | idx_invisible |            1 | name        | A         |           0 |     NULL |   NULL | YES  | BTREE      |         |               | NO      | NULL       |
| test1_index |          1 | idx_age       |            1 | age         | A         |           0 |     NULL |   NULL | YES  | BTREE      |         |               | NO      | NULL       |
| test1_index |          1 | idx_info      |            1 | info        | A         |           0 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
+-------------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
3 rows in set (0.01 sec)

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: MySQL索引可以通过CREATE INDEX语句创建。例如,要在名为table_name的表中创建名为index_name的索引,可以使用以下语法: CREATE INDEX index_name ON table_name (column_name); 其中,column_name是要在其上创建索引的列名。此外,还可以在多个列上创建复合索引,如下所示: CREATE INDEX index_name ON table_name (column1, column2); 这将在column1和column2上创建一个复合索引索引可以提高查询性能,但过多的索引可能会降低写入性能。因此,需要根据实际情况选择适当的索引。 ### 回答2: MySQL索引是通过在表中创建特殊的数据结构来加快对表中数据的查找和访问的速度。创建索引可以提高查询的效率,但也会增加数据的存储需求和插入、更新、删除数据的时间成本。 在MySQL中,创建索引可以使用以下的方式: 1. 创建主键索引:主键索引用于唯一标识表中的每一行数据,保证数据的唯一性和一致性。可以在创建表的时候通过PRIMARY KEY关键字来指定主键索引,也可以通过ALTER TABLE语句来添加主键索引。 2. 创建唯一索引:唯一索引用于保证某一列或多列的数据的唯一性。可以在创建表的时候通过UNIQUE关键字来指定唯一索引,也可以通过ALTER TABLE语句来添加唯一索引。 3. 创建普通索引:普通索引是最基本的索引类型,用于加速对表中列的查询。可以在创建表的时候通过INDEX关键字来指定普通索引,也可以通过ALTER TABLE语句来添加普通索引。 4. 创建全文索引:全文索引用于对表中文本列进行全文搜索,如对文章的内容进行搜索。可以在创建表的时候通过FULLTEXT关键字来指定全文索引,也可以通过ALTER TABLE语句来添加全文索引索引的创建可以使用CREATE INDEX和ALTER TABLE语句来完成,并且可以指定索引名称、所在的表名、要索引的列等信息。 需要注意的是,虽然创建索引可以提高查询效率,但过多的索引也会导致增删操作的性能下降,因此在创建索引时需要谨慎考虑,尽量选择适合实际查询需求的列进行索引。 ### 回答3: 在MySQL中,索引是一种数据结构,用于加快数据库查询操作的速度。创建索引可以提高查询效率,但同时也会增加插入、更新和删除操作的开销。以下是创建MySQL索引的一般步骤: 1. 首先,确定需要创建索引的表和字段。一般来说,常用于筛选和排序的字段是索引的好选择。 2. 使用CREATE INDEX语句创建索引。语法如下: CREATE [UNIQUE] INDEX 索引名 ON 表名 (字段1, 字段2, ...); 索引名是索引的名称,可以根据需要自定义。 表名是要创建索引的表的名称。 字段1,字段2等是要在索引中包含的字段的名称。可以指定多个字段,用逗号分隔。 如果在CREATE INDEX语句之前已经存在同名的索引,可以使用ALTER TABLE语句来更已存在的索引。 3. 可以使用DESCRIBE语句来检查索引是否创建成功。例如,DESCRIBE 表名;将显示表的结构和已创建的索引。 需要注意以下几点来优化索引的效果: 1. 对于经常使用的查询条件,创建索引能够显著提高查询性能。但过多的索引会带来额外的存储开销和更新开销,因此需要谨慎权衡。 2. 在创建索引时,可以根据字段的值分布情况和查询需求来选择合适的索引类型。常见的索引类型有B-树索引和哈希索引。 3. 经常进行数据更新的表,索引的维护成本会比较高。因此,在进行索引设计时,应综合考虑查询性能和数据更新的平衡。 总之,创建索引是提高数据库查询效率的重要手段。合理设计索引,并根据数据的特点和查询需求来创建索引,可以最大程度地提高数据库的性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值