mysql笔记-索引

什么是聚簇索引

聚簇索引:索引的叶节点就是数据节点(索引值)。而非聚簇索引的叶节点仍然是索引节点(告诉你怎么在表中查找这一记录),只不过有一个指针指向对应的数据块。

Innodb和MyIsam区别

转载自 (https://www.zhihu.com/question/20596402)

  1. InnoDB支持事务,MyISAM不支持,对于InnoDB每一条SQL语言都默认封装成事务,自动提交,这样会影响速度,所以最好把多条SQL语言放在begin和commit之间,组成一个事务;
  2. InnoDB支持外键,而MyISAM不支持。对一个包含外键的InnoDB表转为MYISAM会失败;
  3. InnoDB是聚集索引,数据文件是和索引绑在一起的,必须要有主键,通过主键索引效率很高。但是辅助索引需要两次查询,先查询到主键,然后再通过主键查询到数据。因此,主键不应该过大,因为主键太大,其他索引也都会很大。而MyISAM是非聚集索引,数据文件是分离的,索引保存的是数据文件的指针。主键索引和辅助索引是独立的。
  4. InnoDB不保存表的具体行数,执行select count(*) from table时需要全表扫描。而MyISAM用一个变量保存了整个表的行数,执行上述语句时只需要读出该变量即可,速度很快;
  5. Innodb不支持全文索引,而MyISAM支持全文索引,查询效率上MyISAM要高;

普通索引

1.创建一张用户表,建立普通索引 idx_name

      CREATE TABLE `user_tbl` (
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `name` varchar(255) NOT NULL DEFAULT '',
      `age` int(11) NOT NULL,
      `score` float NOT NULL,
      PRIMARY KEY (`id`),
      KEY `idx_name` (`name`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
   #插入两条数据 
   INSERT INTO `user_tbl` (`id`, `name`, `age`, `score`)
   VALUES
(1, 'www', 22, 66),
(2, 'com', 23, 77);

2.使用like查询
explain select * from user_tbl where name like 'w%';

        执行结果
        1	SIMPLE	user_tbl	NULL	range	idx_name	idx_name	767	NULL	1	100.00	Using index condition
        有用到索引
explain select * from user_tbl where name like '%w';
 执行结果
   1	SIMPLE	user_tbl	NULL	ALL	NULL	NULL	NULL	NULL	2	50.00	Using where
没有用到索引

联合索引

建立联合索引

KEY `indx_age_score` (`age`,`score`)

以下sql是否有用到索引

  • explain select * from user_tbl where age<22

           //有用到部分索引
           1	SIMPLE	user_tbl	NULL	range	indx_age_score	indx_age_score	4	NULL	1	100.00	Using index condition
    
  • explain select * from user_tbl where age=22

           // 用到部分索引
           1	SIMPLE	user_tbl	NULL	ref	indx_age_score	indx_age_score	4	const	1	100.00	NULL
    
  • explain select * from user_tbl where age<>22

           // 没有用到索引
           1	SIMPLE	user_tbl	NULL	ALL	indx_age_score	NULL	NULL	NULL	2	100.00	Using where
    
  • explain select * from user_tbl where age=22 and score=66

         // 有用到索引
                 1	 SIMPLE	user_tbl	NULL	ref	indx_age_score	indx_age_score	8	const,const	1	100.00	Using index condition
    
  • explain select * from user_tbl where age=22 and score>66

           // 有用到索引
                   1	SIMPLE	user_tbl	NULL	range	indx_age_score	indx_age_score	8	NULL	1	100.00	Using index condition
    
  • explain select * from user_tbl where age<22 and score>66

       // 有用到索引
       	1	SIMPLE	user_tbl	NULL	range	indx_age_score	indx_age_score	4	NULL	1	50.00	Using index condition   
    
  • explain select * from user_tbl where age<22 and score<66

           // 有用到索引
        	1	SIMPLE	user_tbl	NULL	range indx_age_score indx_age_score	4	NULL	1	50.00	Using index condition
    
  • explain select * from user_tbl where score=66

      // 没有用到索引   
         1	SIMPLE	user_tbl	NULL	ALL	NULL	NULL	NULL	NULL	2	50.00	Using where
    
  • explain select * from user_tbl where score<66

     // 没有用到索引
              1	SIMPLE	user_tbl	NULL	ALL	NULL	NULL	NULL	NULL	2	50.00	Using where
    
posted @ 2018-07-26 15:15  孤独风中一匹狼  阅读(126)  评论(0编辑  收藏  举报