mysql笔记-索引

本文详细比较了MySQL中两种存储引擎InnoDB与MyISAM的特点与区别,包括事务支持、外键约束、索引类型及性能差异等方面,并通过具体示例说明不同索引的应用效果。

什么是聚簇索引

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

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

转载于:https://www.cnblogs.com/fwdqxl/p/9371956.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值