Williamwen

按照创建时间查询数据,创建时间非索引 ,现在要优化一下 因此要为student_recode 表中的created_time增加索引

MySQL如何为字段添加索引

1.添加主键索引(PRIMARY KEY )

ALTER TABLE `table_name` ADD PRIMARY KEY  ( `column` )

2.添加唯一索引(UNIQUE)

ALTER TABLE `table_name` ADD UNIQUE  ( `column` )

3.添加普通索引(INDEX)

ALTER TABLE `table_name` ADD INDEX  idx_createdtime( `column` )

4.添加全文检索(FULLTEXT)

ALTER TABLE `table_name` ADD FULLTEXT( `column` )

5.添加多列索引(INDEX)

ALTER TABLE `table_name` ADD INDEX  idx_createdtime( `column1`, `column2`, `column3` )

eg:测试一个普通索引

  sql写出来了: ALTER TABLE `bus_shoool.student_recode ` ADD INDEX \'idx_created_time\' ( `created_time` )

最后需要设置一下索引的存储类型:一般MySQL的索引的存储类型分为 BTREE 和 HASH ,具体的和表的存储引擎相关;

  这个是用BTREE来创建索引,提高查询效率

因此sql 可以优化成: ALTER TABLE `bus_shoool.student_recode ` ADD INDEX \'idx_created_time\' ( `created_time` ) using BTREE

 

分类:

技术点:

相关文章:

  • 2021-12-31
  • 2021-09-17
  • 2021-09-25
  • 2021-11-30
  • 2021-11-11
猜你喜欢
  • 2021-11-11
  • 2021-11-21
  • 2021-08-15
  • 2021-09-08
  • 2021-08-15
  • 2021-11-11
  • 2021-08-15
相关资源
相似解决方案