码迷,mamicode.com
首页 > 数据库 > 详细

mysql 索引 使用注意细节

时间:2014-06-27 06:10:07      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:mysql   employees   关键字   

在查询时,如果使用到LIKE关键字,就要注意有没有使用到索引;

 

  1. 没有使用索引前:

    mysql> explain select * from employees where first_name=‘Georgi‘\G;
    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: employees
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 294382
            Extra: Using where
    1 row in set (0.00 sec)

  2. 添加索引: create index first_index on employees (first_name);

  3. 索引后:

     

    mysql> explain select * from employees where first_name=‘Georgi‘\G;
    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: employees
             type: ref
    possible_keys: first_index
              key: first_index
          key_len: 16
              ref: const
             rows: 253
            Extra: Using where
    1 row in set (0.00 sec)

  4. 查询中使用LIKE:

    mysql> explain select * from employees where first_name like ‘%Georgi‘\G;
    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: employees
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 294382
            Extra: Using where
    1 row in set (0.05 sec)

  5. mysql> explain select * from employees where first_name like ‘%Georgi%‘\G;
    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: employees
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 294382
            Extra: Using where
    1 row in set (0.00 sec)

  6. mysql> explain select * from employees where first_name like ‘Georgi%‘\G;
    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: employees
             type: range
    possible_keys: first_index
              key: first_index
          key_len: 16
              ref: NULL
             rows: 253
            Extra: Using where
    1 row in set (0.00 sec)

 

可以发现只有在查询关键字后面的加%才可以用到索引;

还有使用了is not null,<>都不可以用到索引:

mysql> explain select * from employees where first_name is not null\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: employees
         type: ALL
possible_keys: first_index
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 294382
        Extra: Using where
1 row in set (0.00 sec)

 

mysql> explain select * from employees where first_name <> ‘Georgi‘\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: employees
         type: ALL
possible_keys: first_index
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 294382
        Extra: Using where
1 row in set (0.00 sec)

 

如果在索引的列上面用了函数或运算都不能引用到索引的:

mysql> explain select * from employees where UPPER(first_name)=‘Ult‘\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: employees
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 294382
        Extra: Using where
1 row in set (0.00 sec)

 

在使用复合索引的时候也要注意第一列有没有在WHERE中;

正确使用索引的:

mysql> explain select * from employees where first_name=‘Georgi‘ and last_name=‘
Facello‘\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: employees
         type: ref
possible_keys: name_index
          key: name_index
      key_len: 34
          ref: const,const
         rows: 2
        Extra: Using where
1 row in set (0.00 sec)

mysql> explain select * from employees where first_name=‘Georgi‘\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: employees
         type: ref
possible_keys: name_index
          key: name_index
      key_len: 16
          ref: const
         rows: 253
        Extra: Using where
1 row in set (0.00 sec)

 

没有使用到索引的:

mysql> explain select * from employees where last_name=‘Facello‘\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: employees
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 300252
        Extra: Using where
1 row in set (0.00 sec)

 

mysql> explain select * from employees where last_name=‘Facello‘ or first_name=‘
Georgi‘\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: employees
         type: ALL
possible_keys: name_index
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 300252
        Extra: Using where
1 row in set (0.00 sec)

第二个例子因为用了OR所以WHERE中也只是对first_name单独检索;

mysql 索引 使用注意细节,布布扣,bubuko.com

mysql 索引 使用注意细节

标签:mysql   employees   关键字   

原文地址:http://5317968.blog.51cto.com/5307968/1431209

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!