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 复制代码
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>
相关推荐
醇氧10 分钟前
【MySQL】MySQL 9.0悄悄的来了
数据库·mysql
毛瞌羊12 分钟前
浅析MySQL-索引篇01
数据库·mysql
丁总学Java1 小时前
MySQL高级-MVCC-原理分析(RR级别)
数据库·mysql·mvcc·rr级别·可重复读
创作小达人1 小时前
InnoDB中的表级锁、页级锁、行级锁详解
java·数据库·mysql
周杰伦的稻香2 小时前
“LNMP环境搭建实战指南:从零开始配置CentOS 7下的Nginx、MySQL与PHP“
mysql·nginx·centos
苏生Susheng3 小时前
【Oracle】Oracle常用语句大全
java·数据库·sql·mysql·oracle·sql语句·数据库语法
夜行容忍4 小时前
索引失效的几种场景
数据库·mysql
969库库库5 小时前
MySQL-作业1
数据库·mysql
你喝不喝热水啊6 小时前
深入浅出mysql分库分表
数据库·mysql
霖烟易辞6 小时前
MySQL的安装
数据库·mysql