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>
相关推荐
秋意钟28 分钟前
MySQL日期类型选择建议
数据库·mysql
ac-er88881 小时前
MySQL如何实现PHP输入安全
mysql·安全·php
桀桀桀桀桀桀2 小时前
数据库中的用户管理和权限管理
数据库·mysql
瓜牛_gn7 小时前
mysql特性
数据库·mysql
Yaml412 小时前
Spring Boot 与 Vue 共筑二手书籍交易卓越平台
java·spring boot·后端·mysql·spring·vue·二手书籍
追风林12 小时前
mac 本地docker-mysql主从复制部署
mysql·macos·docker
Hsu_kk14 小时前
MySQL 批量删除海量数据的几种方法
数据库·mysql
编程学无止境14 小时前
第02章 MySQL环境搭建
数据库·mysql
knight-n14 小时前
MYSQL库的操作
数据库·mysql
eternal__day16 小时前
MySQL_聚合函数&分组查询
数据库·mysql