like效率 regexp_mysql中REGEXP 和 LIKE 的效率问题?

看到一个帖子,是讨论 REGEXP 和 LIKE 的效率问题,下面简单做个测试:

1,我们先来看表person的索引信息:

mysql> show index from person;

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

| Table  | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |

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

| person |          0 | PRIMARY  |            1 | id          | A         |        2009 |     NULL | NULL   |      | BTREE      |         |

| person |          1 | descs    |            1 | descs       | A         |        2009 |     NULL | NULL   | YES  | BTREE      |         |

| person |          1 | name     |            1 | name        | A         |        1004 |     NULL | NULL   | YES  | BTREE      |         |

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

2,下面具体看看这两条SQL语句的执行过程。

mysql> explain SELECT * FROM person WHERE descs REGEXP '^.{5}$';

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

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

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

|  1 | SIMPLE      | person | ALL  | NULL          | NULL | NULL    | NULL | 2009 | Using where |

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

1 row in set (0.00 sec)

mysql> EXPLAIN SELECT * FROM person WHERE descs LIKE '_____';

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

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

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

|  1 | SIMPLE      | person | ALL  | NULL          | NULL | NULL    | NULL | 2009 | Using where |

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

1 row in set (0.00 sec)

显然,两个SQL语句的执行过程是一样的,当然其性能也是如此。

会不会有其他情况,看下面:

mysql> EXPLAIN SELECT * FROM person WHERE descs LIKE '^_____%';

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

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

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

|  1 | SIMPLE      | person | range | descs         | descs | 63      | NULL |    1 | Using where |

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

1 row in set (0.09 sec)

mysql>

mysql>

mysql> EXPLAIN SELECT * FROM person WHERE descs LIKE '_____%';

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

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

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

|  1 | SIMPLE      | person | ALL  | NULL          | NULL | NULL    | NULL | 2009 | Using where |

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

1 row in set (0.00 sec)

mysql> EXPLAIN SELECT * FROM person WHERE descs LIKE '^_____';

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

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

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

|  1 | SIMPLE      | person | range | descs         | descs | 63      | NULL |    1 | Using where |

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

1 row in set (0.00 sec)

mysql> EXPLAIN SELECT * FROM person WHERE descs LIKE '%_____%';

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

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

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

|  1 | SIMPLE      | person | ALL  | NULL          | NULL | NULL    | NULL | 2009 | Using where |

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

1 row in set (0.00 sec)

测试结果很明显,如果 LIKE 左端不固定,那么查询就不会用上索引,效率就很不好,如果你有更好的方法,我们可以邮件讨论。

阅读(5905) | 评论(1) | 转发(0) |

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值