• Mysql索引 like篇


    Mysql索引 like篇

    Mysql在查询中使用like的时候,对应的字段上面的索引是否会生效呢?

    • like ‘张’ 用到了索引
    • like ‘张%’ 前缀匹配 用到了索引
    • like ‘%张%’ 中间匹配 没有用到了索引
    • like ‘%张’ 后缀匹配 没有用到了索引
    mysql> CREATE TABLE `tea` (
        ->   `id` bigint NOT NULL AUTO_INCREMENT,
        ->   `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
        ->   `number` bigint DEFAULT NULL COMMENT '编号',
        ->   `no_index_number` bigint DEFAULT NULL,
        ->   PRIMARY KEY (`id`),
        ->   KEY `index1` (`name`)
        -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
    Query OK, 0 rows affected, 3 warnings (0.08 sec)
    
    mysql>
    mysql> select * from tea;
    +----+------+--------+-----------------+
    | id | name | number | no_index_number |
    +----+------+--------+-----------------+
    |  1 | 张三 |  10001 |               3 |
    |  2 | 李四 |  10002 |               2 |
    |  3 | 王五 |  10003 |               1 |
    +----+------+--------+-----------------+
    3 rows in set (0.00 sec)
    
    mysql>
    mysql>
    mysql>
    mysql> explain select * from tea where name like '张';
    +----+-------------+-------+------------+-------+---------------+--------+---------+------+------+----------+-----------------------+
    | id | select_type | table | partitions | type  | possible_keys | key    | key_len | ref  | rows | filtered | Extra                 |
    +----+-------------+-------+------------+-------+---------------+--------+---------+------+------+----------+-----------------------+
    |  1 | SIMPLE      | tea   | NULL       | range | index1        | index1 | 768     | NULL |    1 |   100.00 | Using index condition |
    +----+-------------+-------+------------+-------+---------------+--------+---------+------+------+----------+-----------------------+
    1 row in set, 1 warning (0.00 sec)
    
    mysql> explain select * from tea where name like '张%';
    +----+-------------+-------+------------+-------+---------------+--------+---------+------+------+----------+-----------------------+
    | id | select_type | table | partitions | type  | possible_keys | key    | key_len | ref  | rows | filtered | Extra                 |
    +----+-------------+-------+------------+-------+---------------+--------+---------+------+------+----------+-----------------------+
    |  1 | SIMPLE      | tea   | NULL       | range | index1        | index1 | 768     | NULL |    1 |   100.00 | Using index condition |
    +----+-------------+-------+------------+-------+---------------+--------+---------+------+------+----------+-----------------------+
    1 row in set, 1 warning (0.01 sec)
    
    mysql>
    mysql> explain select * from tea where name like '%张%';
    +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
    | id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
    +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
    |  1 | SIMPLE      | tea   | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    3 |    33.33 | Using where |
    +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
    1 row in set, 1 warning (0.00 sec)
    
    mysql> explain select * from tea where name like '%张';
    +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
    | id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
    +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
    |  1 | SIMPLE      | tea   | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    3 |    33.33 | Using where |
    +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
    1 row in set, 1 warning (0.00 sec)
    
    mysql>
    
    
    • 正则表达式匹配
    mysql> explain select id,number from tea where name REGEXP '张';
    +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
    | id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
    +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
    |  1 | SIMPLE      | tea   | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    3 |   100.00 | Using where |
    +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
    1 row in set, 1 warning (0.00 sec)
    
    mysql>
    
  • 相关阅读:
    深度学习机器学习面试题——自然语言处理NLP,transformer,BERT,RNN,LSTM
    低代码开发平台应该如何分类?
    Acwing-宠物小精灵之收服-(多维01背包+正序倒序+两种形式dp求答案)
    Ant Design 国内镜像
    网络安全实战攻防演练应急处置预案
    git原理浅析
    数据结构从入门到精通——栈
    食品饮料行业数智化采购管理系统规范供应商管理,完善企业协同体系
    【javaEE】网络原理(数据链路层+小结)
    算法总结--ST表
  • 原文地址:https://blog.csdn.net/jc0803kevin/article/details/139888645