温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MySQL 5.5 模式匹配LIKE

发布时间:2020-08-07 20:49:27 来源:ITPUB博客 阅读:137 作者:feelpurple 栏目:MySQL数据库
mysql> select * from t_test;
+--------+-------------+---------+
| deptno | dname       | loc     |
+--------+-------------+---------+
|     10 | Research    | Beijing |
|     20 | Maintenance | Huludao |
|     30 | Market      | Tianjin |
|     40 | Leader      | Qingdao |
+--------+-------------+---------+
4 rows in set (0.00 sec)

mysql> create index idx_test_dname on t_test(dname);
Query OK, 0 rows affected (0.31 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show keys from t_test\G
*************************** 1. row ***************************
        Table: t_test
   Non_unique: 1
     Key_name: idx_test_dname
 Seq_in_index: 1
  Column_name: dname
    Collation: A
  Cardinality: 4
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: BTREE
      Comment: 
Index_comment: 
1 row in set (0.00 sec)

%Lea%没有使用索引扫描

mysql> explain select * from t_test where dname like '%Lea%';
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | t_test | ALL  | NULL          | NULL | NULL    | NULL |    4 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

Lea%使用索引扫描

mysql> explain select * from t_test where dname like 'Lea%';
+----+-------------+--------+-------+----------------+----------------+---------+------+------+-------------+
| id | select_type | table  | type  | possible_keys  | key            | key_len | ref  | rows | Extra       |
+----+-------------+--------+-------+----------------+----------------+---------+------+------+-------------+
|  1 | SIMPLE      | t_test | range | idx_test_dname | idx_test_dname | 47      | NULL |    1 | Using where |
+----+-------------+--------+-------+----------------+----------------+---------+------+------+-------------+
1 row in set (0.12 sec)

mysql> explain select dname from t_test where dname like 'Lea%';
+----+-------------+--------+-------+----------------+----------------+---------+------+------+--------------------------+
| id | select_type | table  | type  | possible_keys  | key            | key_len | ref  | rows | Extra                    |
+----+-------------+--------+-------+----------------+----------------+---------+------+------+--------------------------+
|  1 | SIMPLE      | t_test | index | idx_test_dname | idx_test_dname | 47      | NULL |    4 | Using where; Using index |
+----+-------------+--------+-------+----------------+----------------+---------+------+------+--------------------------+
1 row in set (0.00 sec)
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI