索引分类

  • 普通索引index :加速查找
  • 唯一索引
    • 主键索引:primary key :加速查找+约束(不为空且唯一)
    • 唯一索引:unique:加速查找+约束 (唯一)
  • 联合索引
    • -primary key(id,name):联合主键索引
    • -unique(id,name):联合唯一索引
    • -index(id,name):联合普通索引
  • 全文索引fulltext :用于搜索很长一篇文章的时候,效果最好。
  • 空间索引spatial :了解就好,几乎不用


索引类型

索引类型分两类 :

  • hash类型的索引:查询单条快,范围查询慢
  • btree类型的索引:b+树,层数越多,数据量指数级增长(我们就用它,因为innodb默认支持它)

不同的存储引擎支持的索引类型也不一样

  • InnoDB 支持事务,支持行级别锁定,支持 B-tree、Full-text 等索引,不支持 Hash 索引;
  • MyISAM 不支持事务,支持表级别锁定,支持 B-tree、Full-text 等索引,不支持 Hash 索引;
  • Memory 不支持事务,支持表级别锁定,支持 B-tree、Hash 等索引,不支持 Full-text 索引;
  • NDB 支持事务,支持行级别锁定,支持 Hash 索引,不支持 B-tree、Full-text 等索引;
  • Archive 不支持事务,支持表级别锁定,不支持 B-tree、Hash、Full-text 等索引;

    创建/删除索引的语法

    ```sql

    方法一:创建表时

    CREATE TABLE 表名 ( 字段名1 数据类型 [完整性约束条件…], 字段名2 数据类型 [完整性约束条件…], [UNIQUE | FULLTEXT | SPATIAL ] INDEX | KEY [索引名] (字段名[(长度)] [ASC |DESC]) );

方法二:CREATE在已存在的表上创建索引

CREATE [UNIQUE | FULLTEXT | SPATIAL ] INDEX 索引名 ON 表名 (字段名[(长度)] [ASC |DESC]) ;

方法三:ALTER TABLE在已存在的表上创建索引

ALTER TABLE 表名 ADD [UNIQUE | FULLTEXT | SPATIAL ] INDEX 索引名 (字段名[(长度)] [ASC |DESC]) ;

删除索引:DROP INDEX 索引名 ON 表名字;

  1. <a name="mX7gW"></a>
  2. # 帮助文档
  3. ```sql
  4. help create
  5. help create index
  6. ==================
  7. 1.创建索引
  8. -在创建表时就创建(需要注意的几点)
  9. create table s1(
  10. id int ,#可以在这加primary key
  11. #id int index #不可以这样加索引,因为index只是索引,没有约束一说,
  12. #不能像主键,还有唯一约束一样,在定义字段的时候加索引
  13. name char(20),
  14. age int,
  15. email varchar(30)
  16. #primary key(id) #也可以在这加
  17. index(id) #可以这样加
  18. );
  19. -在创建表后在创建
  20. create index name on s1(name); #添加普通索引
  21. create unique age on s1(age);添加唯一索引
  22. alter table s1 add primary key(id); #添加住建索引,也就是给id字段增加一个主键约束
  23. create index name on s1(id,name); #添加普通联合索引
  24. 2.删除索引
  25. drop index id on s1;
  26. drop index name on s1; #删除普通索引
  27. drop index age on s1; #删除唯一索引,就和普通索引一样,不用在index前加unique来删,直接就可以删了
  28. alter table s1 drop primary key; #删除主键(因为它添加的时候是按照alter来增加的,那么我们也用alter来删)

索引遵循原则

  1. #1.最左前缀匹配原则,非常重要的原则,
  2. create index ix_name_email on s1(name,email,)
  3. - 最左前缀匹配:必须按照从左到右的顺序匹配
  4. select * from s1 where name='egon'; #可以
  5. select * from s1 where name='egon' and email='asdf'; #可以
  6. select * from s1 where email='alex@oldboy.com'; #不可以
  7. mysql会一直向右匹配直到遇到范围查询(>、<、betweenlike)就停止匹配,
  8. 比如a = 1 and b = 2 and c > 3 and d = 4 如果建立(a,b,c,d)顺序的索引,
  9. d是用不到索引的,如果建立(a,b,d,c)的索引则都可以用到,a,b,d的顺序可以任意调整。
  10. #2.=和in可以乱序,比如a = 1 and b = 2 and c = 3 建立(a,b,c)索引可以任意顺序,mysql的查询优化器
  11. 会帮你优化成索引可以识别的形式
  12. #3.尽量选择区分度高的列作为索引,区分度的公式是count(distinct col)/count(*),
  13. 表示字段不重复的比例,比例越大我们扫描的记录数越少,唯一键的区分度是1,而一些状态、
  14. 性别字段可能在大数据面前区分度就是0,那可能有人会问,这个比例有什么经验值吗?使用场景不同,
  15. 这个值也很难确定,一般需要join的字段我们都要求是0.1以上,即平均1条扫描10条记录
  16. #4.索引列不能参与计算,保持列“干净”,比如from_unixtime(create_time) = ’2014-05-29’
  17. 就不能使用到索引,原因很简单,b+树中存的都是数据表中的字段值,
  18. 但进行检索时,需要把所有元素都应用函数才能比较,显然成本太大。
  19. 所以语句应该写成create_time = unix_timestamp(’2014-05-29’);

最左前范式

  1. mysql> select * from s1 where id>3 and name='egon' and email='alex333@oldboy.com' and gender='male';
  2. Empty set (0.39 sec)
  3. mysql> create index idx on s1(id,name,email,gender); #未遵循最左前缀
  4. Query OK, 0 rows affected (15.27 sec)
  5. Records: 0 Duplicates: 0 Warnings: 0
  6. mysql> select * from s1 where id>3 and name='egon' and email='alex333@oldboy.com' and gender='male';
  7. Empty set (0.43 sec)
  8. mysql> drop index idx on s1;
  9. Query OK, 0 rows affected (0.16 sec)
  10. Records: 0 Duplicates: 0 Warnings: 0
  11. mysql> create index idx on s1(name,email,gender,id); #遵循最左前缀
  12. Query OK, 0 rows affected (15.97 sec)
  13. Records: 0 Duplicates: 0 Warnings: 0
  14. mysql> select * from s1 where id>3 and name='egon' and email='alex333@oldboy.com' and gender='male';
  15. Empty set (0.03 sec)

最左前缀匹配

  1. 最左前缀匹配
  2. index(id,age,email,name)
  3. #条件中一定要出现id(只要出现id就会提升速度)
  4. id
  5. id age
  6. id email
  7. id name
  8. email #不行 如果单独这个开头就不能提升速度了
  9. mysql> select count(*) from s1 where id=3000;
  10. +----------+
  11. | count(*) |
  12. +----------+
  13. | 1 |
  14. +----------+
  15. 1 row in set (0.11 sec)
  16. mysql> create index xxx on s1(id,name,age,email);
  17. Query OK, 0 rows affected (6.44 sec)
  18. Records: 0 Duplicates: 0 Warnings: 0
  19. mysql> select count(*) from s1 where id=3000;
  20. +----------+
  21. | count(*) |
  22. +----------+
  23. | 1 |
  24. +----------+
  25. 1 row in set (0.00 sec)
  26. mysql> select count(*) from s1 where name='egon';
  27. +----------+
  28. | count(*) |
  29. +----------+
  30. | 299999 |
  31. +----------+
  32. 1 row in set (0.16 sec)
  33. mysql> select count(*) from s1 where email='egon3333@oldboy.com';
  34. +----------+
  35. | count(*) |
  36. +----------+
  37. | 1 |
  38. +----------+
  39. 1 row in set (0.15 sec)
  40. mysql> select count(*) from s1 where id=1000 and email='egon3333@oldboy.com';
  41. +----------+
  42. | count(*) |
  43. +----------+
  44. | 0 |
  45. +----------+
  46. 1 row in set (0.00 sec)
  47. mysql> select count(*) from s1 where email='egon3333@oldboy.com' and id=3000;
  48. +----------+
  49. | count(*) |
  50. +----------+
  51. | 0 |
  52. +----------+
  53. 1 row in set (0.00 sec)
  54. 建联合索引,最左匹配

索引无法命中的情况需要注意

  1. - like '%xx'
  2. select * from tb1 where email like '%cn';
  3. - 使用函数
  4. select * from tb1 where reverse(email) = 'wupeiqi';
  5. - or
  6. select * from tb1 where nid = 1 or name = 'seven@live.com';
  7. 特别的:当or条件中有未建立索引的列才失效,以下会走索引
  8. select * from tb1 where nid = 1 or name = 'seven';
  9. select * from tb1 where nid = 1 or name = 'seven@live.com' and email = 'alex'
  10. - 类型不一致
  11. 如果列是字符串类型,传入条件是必须用引号引起来,不然...
  12. select * from tb1 where email = 999;
  13. 普通索引的不等于不会走索引
  14. - !=
  15. select * from tb1 where email != 'alex'
  16. 特别的:如果是主键,则还是会走索引
  17. select * from tb1 where nid != 123
  18. - >
  19. select * from tb1 where email > 'alex'
  20. 特别的:如果是主键或索引是整数类型,则还是会走索引
  21. select * from tb1 where nid > 123
  22. select * from tb1 where num > 123
  23. #排序条件为索引,则select字段必须也是索引字段,否则无法命中
  24. - order by
  25. select name from s1 order by email desc;
  26. 当根据索引排序时候,select查询的字段如果不是索引,则不走索引
  27. select email from s1 order by email desc;
  28. 特别的:如果对主键排序,则还是走索引:
  29. select * from tb1 order by nid desc;
  30. - 组合索引最左前缀
  31. 如果组合索引为:(name,email)
  32. name and email -- 使用索引
  33. name -- 使用索引
  34. email -- 不使用索引
  35. - count(1)或count(列)代替count(*)在mysql中没有差别了
  36. - create index xxxx on tb(title(19)) #text类型,必须制定长度

注意:

  1. - 避免使用select *
  2. - count(1)或count(列) 代替 count(*)
  3. - 创建表时尽量时 char 代替 varchar
  4. - 表的字段顺序固定长度的字段优先
  5. - 组合索引代替多个单列索引(经常使用多个条件查询时)
  6. - 尽量使用短索引
  7. - 使用连接(JOIN)来代替子查询(Sub-Queries)
  8. - 连表时注意条件类型需一致
  9. - 索引散列值(重复少)不适合建索引,例:性别不适合
  10. - 联合索引一般都是and连接的时候,用or的话提升不了速度

慢查询优化的基本步骤

  1. 1.先运行看看是否真的很慢,注意设置SQL_NO_CACHE
  2. 2.where条件单表查,锁定最小返回记录表。这句话的意思是把查询语句的where都应用到表中返回的记录数最小的表开始查起,单表每个字段分别查询,看哪个字段的区分度最高
  3. 3.explain查看执行计划,是否与1预期一致(从锁定记录较少的表开始查询)
  4. 4.order by limit 形式的sql语句让排序的表优先查
  5. 5.了解业务方使用场景
  6. 6.加索引时参照建索引的几大原则
  7. 7.观察结果,不符合预期继续从0分析