美文网首页全栈工程师通往架构师之路
第三个模块 MySQL 5.7之多列[单]索引实践

第三个模块 MySQL 5.7之多列[单]索引实践

作者: 霄峰 | 来源:发表于2017-01-05 10:42 被阅读94次

索引:是当你的业务完成后,跟据查询条件来建立的。当你的数据量大(一般是10万条数据)了之后,我们会再把普通索引删除,使用自建索引表。因为数据量大的时候你要批量修改(索引表也会修改)会变的非常的慢!

加索引的时候,先建议使用单列索引一个一个加!然后再改进使用联合索引!

本文是针对mysql 5.7多列[单]索引进行验证测试!
版本:mysql 5.7
测试结果日期:2017-01-05

表结构:

mysql> show create table m_user2\G;
*************************** 1. row ***************************
       Table: m_user2
Create Table: CREATE TABLE `m_user2` (
  `id` int(10) unsigned NOT NULL DEFAULT '0',
  `name` char(32) NOT NULL,
  `age` tinyint(4) NOT NULL,
  `school` char(128) NOT NULL,
  `status` tinyint(4) NOT NULL DEFAULT '1',
  KEY `name` (`name`),
  KEY `age` (`age`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

多列[单]索引字段:
KEY name (name),
KEY age (age)

序号 字段名 类型
1 name char
2 age tinyint

结果:

1.[1 and 2 命中最严格的一个]
select * from m_user2 where name='feng1' and age=10

2.[1 or 2 两个都命中]
select * from m_user2 where name='feng1' or age=10

3.[1 or 2 order by 1 命中]
select * from m_user2 where name='feng1' or age=10 order by name desc

4.[between 命中]
select * from m_user2 where name between 'feng1' and 'feng123' order by name desc
select * from m_user2 where age between 10 and 20 order by name desc

5.[in 命中]
select * from m_user2 where name in ('feng1','feng123') order by name desc

6.[无where条件 直接order by 不命中]
select * from m_user2 order by name desc


测试过程

1.[1 and 2 命中最严格的一个]

mysql> explain select * from m_user2 where name='feng1' and age=10\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: m_user2
   partitions: NULL
         type: ref
possible_keys: name,age
          key: name
      key_len: 96
          ref: const
         rows: 1
     filtered: 5.00
        Extra: Using where
1 row in set, 1 warning (0.00 sec)

2.[1 or 2 两个都命中]

mysql> explain select * from m_user2 where name='feng1' or age=10\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: m_user2
   partitions: NULL
         type: index_merge
possible_keys: name,age
          key: name,age
      key_len: 96,1
          ref: NULL
         rows: 17
     filtered: 100.00
        Extra: Using union(name,age); Using where
1 row in set, 1 warning (0.01 sec)

3.[1 or 2 order by 1 命中]

mysql> explain select * from m_user2 where name='feng1' or age=10 order by name desc\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: m_user2
   partitions: NULL
         type: index_merge
possible_keys: name,age
          key: name,age
      key_len: 96,1
          ref: NULL
         rows: 17
     filtered: 100.00
        Extra: Using union(name,age); Using where; Using filesort
1 row in set, 1 warning (0.00 sec)

4.[between 命中]

mysql> explain select * from m_user2 where name between 'feng1' and 'feng123' order by name desc\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: m_user2
   partitions: NULL
         type: range
possible_keys: name
          key: name
      key_len: 96
          ref: NULL
         rows: 29
     filtered: 100.00
        Extra: Using index condition
1 row in set, 1 warning (0.00 sec)
---------------------------------------------------------------------
mysql> explain select * from m_user2 where age between 10 and 20 order by name desc\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: m_user2
   partitions: NULL
         type: range
possible_keys: age
          key: age
      key_len: 1
          ref: NULL
         rows: 134
     filtered: 100.00
        Extra: Using index condition; Using filesort
1 row in set, 1 warning (0.01 sec)

5.[in 命中]

mysql> explain select * from m_user2 where name in ('feng1','feng123') order by name desc\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: m_user2
   partitions: NULL
         type: range
possible_keys: name
          key: name
      key_len: 96
          ref: NULL
         rows: 2
     filtered: 100.00
        Extra: Using index condition
1 row in set, 1 warning (0.01 sec)

6.[无where条件 直接order by 不命中]

mysql> explain select * from m_user2 order by name desc\G; 
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: m_user2
   partitions: NULL
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 1001
     filtered: 100.00
        Extra: Using filesort
1 row in set, 1 warning (0.00 sec)

相关文章

  • 第三个模块 MySQL 5.7之多列[单]索引实践

    索引:是当你的业务完成后,跟据查询条件来建立的。当你的数据量大(一般是10万条数据)了之后,我们会再把普通索引删除...

  • Mysql索引与锁

    本文以Mysql5.7为例测试。 1:mysql索引方法 Mysql的索引方法分为btree索引和hash索引。 ...

  • 第三个模块 MySQL 5.7之联合(复合)索引实践

    论mysql5.7.13性能优化之索引优化 索引:是当你的业务完成后,跟据查询条件来建立的。当你的数据量大(一般是...

  • 索引

    MySQL 索引 https://dev.mysql.com/doc/refman/5.7/en/create-i...

  • 索引

    MYSQL索引 MYSQL中索引文件以B树结构存储,索引可分为单列索引和多列索引。 对于多列索引中,当一个SQL语...

  • Mysql索引的设计、使用和优化

    Mysql索引概述 所有MySQL列类型可以被索引。对相关列使用索引是提高SELECT操作性能的最佳途径。根据存储...

  • MySql 数据查询优化

    1. MySQL索引类型: mysql的索引有5种:主键索引、普通索引、唯一索引、全文索引、聚合索引(多列索引)。...

  • 生产环境实践Mysql5.7主从+Atlas实现读写分离

    《生产环境实践Mysql5.7主从+Atlas实现读写分离》

  • 数据库

    • MySQL 索引使用的注意事项 MySQL 索引使用的注意事项 索引不会包含有NULL值的列使用短索引...

  • MySQL 多列索引

    MySQL 多列索引 多列索引也叫联合索引又叫复合索引也就是把多个字段按顺序连起来创建一个索引 ( 最多16列 ...

网友评论

    本文标题:第三个模块 MySQL 5.7之多列[单]索引实践

    本文链接:https://www.haomeiwen.com/subject/attzvttx.html