mysql优化之搜索引擎_MySQL之索引优化

一、Table Demo

CREATE TABLE `employees` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`name` varchar(24) NOT NULL DEFAULT '' COMMENT '姓名',

`age` int(11) NOT NULL DEFAULT '0' COMMENT '年龄',

`position` varchar(20) NOT NULL DEFAULT '' COMMENT '职位',

`hire_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '入职时间',

PRIMARY KEY (`id`),

KEY `idx_name_age_position` (`name`,`age`,`position`) USING BTREE

) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='员工记录表';

INSERT INTO employees(name,age,position,hire_time) VALUES('LiLei',22,'manager',NOW());

INSERT INTO employees(name,age,position,hire_time) VALUES('HanMeimei', 23,'dev',NOW());

INSERT INTO employees(name,age,position,hire_time) VALUES('Lucy',23,'dev',NOW());

这里边加入了联合索引

KEY `idx_name_age_position` (`name`,`age`,`position`) USING BTREE

全值匹配

mysql> explain select * from employees where name = 'LiLei';

+----+-------------+-----------+------------+------+-----------------------+-----------------------+---------+-------+------+----------+-------+

| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |

+----+-------------+-----------+------------+------+-----------------------+-----------------------+---------+-------+------+----------+-------+

| 1 | SIMPLE | employees | NULL | ref | idx_name_age_position | idx_name_age_position | 74 | const | 1 | 100.00 | NULL |

+----+-------------+-----------+------------+------+-----------------------+-----------------------+---------+-------+------+----------+-------+

1 row in set, 1 warning (0.00 sec)

算算这个key_len

key_len : 显示了mysql在索引里使用的字节数,通过这个值可以算出具体使用了索引中的哪些列。

【字符串】

char(n):n字节长度

varchar(n):如果是utf-8,则长度 3n + 2 字节,加的2字节用来存储字符串长度

【数值类型】

tinyint:1字节

smallint:2字节

int:4字节

bigint:8字节

【时间类型】

date:3字节

timestamp:4字节

datetime:8字节

如果字段为null,需要1字节记录是否为null。

索引最大长度768字节,字符过长时,mysql默认调用类似左前缀索引的处理,将前半部分的字符提取出来做索引

name varchar(24)—> 3 * 24 + 2 = 74 , 用了联合索引中的name

mysql> explain select * from employees where name = 'LiLei' and age= 22;

+----+-------------+-----------+------------+------+-----------------------+-----------------------+---------+-------------+------+----------+-------+

| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |

+----+-------------+-----------+------------+------+-----------------------+-----------------------+---------+-------------+------+----------+-------+

| 1 | SIMPLE | employees | NULL | ref | idx_name_age_position | idx_name_age_position | 78 | const,const | 1 | 100.00 | NULL |

+----+-------------+-----------+------------+------+-----------------------+-----------------------+---------+-------------+------+----------+-------+

1 row in set, 1 warning (0.00 sec)

ken_len 长度78 ?

第二个是int , int 占 4个字节 , 74 + 4 = 78 ,这个SQL用了联合索引中的 name + age

mysql> explain select * from employees where name = 'LiLei' and age= 22 and position = 'manager';

+----+-------------+-----------+------------+------+-----------------------+-----------------------+---------+-------------------+------+----------+-------+

| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |

+----+-------------+-----------+------------+------+-----------------------+-----------------------+---------+-------------------+------+----------+-------+

| 1 | SIMPLE | employees | NULL | ref | idx_name_age_position | idx_name_age_position | 140 | const,const,const | 1 | 100.00 | NULL |

+----+-------------+-----------+------------+------+-----------------------+-----------------------+---------+-------------------+------+----------+-------+

1 row in set, 1 warning (0.00 sec)

mysql>

key_len = 74 + 4 + 72 = 140

mysql> explain select * from employees where name = 'LiLei' and position = 'manager';

3a48457e521a95f5ed88ceef52191d0b.png

只用了联合索引中的name

最左前缀

如果索引了多列,要遵守最左前缀法则 , 指的是查询从索引的最左前列开始并且不跳过索引中的列。

mysql> explain select * from employees where name = 'LiLei' and age= 22;

69eb40ea11d25a2a57f3bcb0a12e9956.png

符合最左前缀。

mysql> explain select * from employees where age= 22 and position='manager';

ce07cee2eaada3e8a9faab44329c7655.png

不符合最左前缀。

user where : 使用 where 语句来处理结果,并且查询的列未被索引覆盖

1、禁止索引列上做任何操作(计算、函数、(自动or手动)类型转换),会导致索引失效而转向全表扫描

2、存储引擎不能使用索引中范围条件右边的列,尽量使用覆盖索引(只访问索引的查询(索引列包含查询列)),减少 select * 语句

3、mysql在使用不等于(!=或者<>)的时候无法使用索引会导致全表扫描

4、is null,is not null 一般情况下也无法使用索引

5、like以通配符开头(’$abc…’)mysql索引失效会变成全表扫描操作。

%在前,还是要回想那个索引B+Tree ,

意味着前面可能还有其他的字符串, 那在树中的有序性没法保证啊!继续回想那个索引B+Tree , % 不在前面 意味着%前面的字符串固定, 那在树中的就是有序的,当然可以走索引

6、like 的优化

【问题:解决like’%字符串%'索引不被使用的方法?】

A: 使用覆盖索引,查询字段必须是建立覆盖索引字段

不敢说好太多, index 总比 all 好吧 。

B: 如果不能使用覆盖索引则可能需要借助搜索引擎 ,Es等

字符串不加单引号索引失效

7、少用or或in

用它查询时,mysql不一定使用索引,mysql内部优化器会根据检索比例、表大小等多个因素整体评 估是否使用索引,详见范围查询优化

8、范围查询优化

增加索引

没走索引原因:mysql内部优化器会根据检索比例、表大小等多个因素整体评估是否使用索引。比如这个例子,可能是由于单次数据量查询过大导致优化器最终选择不走索引

优化方法: 可以将大的范围拆分成多个小范围

9、like KK%相当于=常量,%KK和%KK% 相当于范围

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值