认识索引

索引 - 图1

存储结构

查看文件路径

show variables like ‘datadir’;
InnoDB:frm(表结构)、ibd(索引和数据)
MyISAM:frm(表结构)、MYI(索引)、MYD(数据)

InnoDB结构

image.png
image.png

MyISAM结构

image.png
image.png

使用索引

  1. --创建普通索引
  2. create table score(
  3. id int(11) auto_increment primary key,
  4. name varchar(50) not null,
  5. math int(5) not null,
  6. index(字段)
  7. );
  8. --创建唯一索引
  9. create table score(
  10. id int(11) auto_increment primary key,
  11. name varchar(50) not null,
  12. math int(5) not null,
  13. unique index 索引名(字段)
  14. );
  15. --创建全文索引
  16. create table score(
  17. id int(11) auto_increment primary key,
  18. name varchar(50) not null,
  19. math int(5) not null,
  20. fulltext key 索引名(字段)
  21. );
  22. --查询全文索引内容
  23. select * from fulltext_table
  24. where match(fulltext_col)
  25. against('字符串' in natural language mode);
  26. --创建单列索引
  27. create table score(
  28. id int(11) auto_increment primary key,
  29. name varchar(50) not null,
  30. math int(5) not null,
  31. index 索引名(字段(索引长度))
  32. );
  33. --创建多列索引
  34. create table score(
  35. id int(11) auto_increment primary key,
  36. name varchar(50) not null,
  37. math int(5) not null,
  38. index 索引名(字段1,字段2)
  39. );
  40. --创建空间索引
  41. create table score(
  42. id int(11) auto_increment primary key,
  43. name varchar(50) not null,
  44. math int(5) not null,
  45. spatial index 索引名(字段)
  46. );
  47. --创建索引
  48. create [unique][fulltext][spatial]index 索引名 on 表名(字段);
  49. --删除索引
  50. drop index 索引名 on 表名;

索引失效

使用分析工具

explain sql 进行分析

失效类型

  1. 联合索引最右匹配