第三個模塊 MySQL 5.7之多列[單]索引實踐

索引:是當(dāng)你的業(yè)務(wù)完成后,跟據(jù)查詢條件來建立的。當(dāng)你的數(shù)據(jù)量大(一般是10萬條數(shù)據(jù))了之后,我們會再把普通索引刪除,使用自建索引表。因為數(shù)據(jù)量大的時候你要批量修改(索引表也會修改)會變的非常的慢!

加索引的時候,先建議使用單列索引一個一個加!然后再改進使用聯(lián)合索引!

本文是針對mysql 5.7多列[單]索引進行驗證測試!
版本:mysql 5.7
測試結(jié)果日期:2017-01-05

表結(jié)構(gòu):

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

結(jié)果:

1.[1 and 2 命中最嚴(yán)格的一個]
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 命中最嚴(yán)格的一個]

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)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 論mysql5.7.13性能優(yōu)化之索引優(yōu)化 索引:是當(dāng)你的業(yè)務(wù)完成后,跟據(jù)查詢條件來建立的。當(dāng)你的數(shù)據(jù)量大(一般是...
    霄峰閱讀 2,281評論 3 1
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,834評論 18 399
  • 50個常用的sql語句Student(S#,Sname,Sage,Ssex) 學(xué)生表Course(C#,Cname...
    哈哈海閱讀 1,335評論 0 7
  • 其實對于這個問題我很早就曾想過,只是當(dāng)時還沒有把它列為自己成長所遇問題的重災(zāi)區(qū),直到最近一段時間才覺得它是很重要的...
    夏沫錦書閱讀 1,188評論 0 0
  • 兒子上晚自習(xí)但每天還是在家吃了飯再去。也不知如兒子所說在家吃可以省點生活費或還是想回家玩會電腦。 今天兒子在玩游戲...
    馮梅fm閱讀 177評論 1 4

友情鏈接更多精彩內(nèi)容