1
mysql> EXPLAIN SELECT col1, col2 FROM t WHERE col1 REGEXP '^foobar_[0-9]{4}$';
+----+-------------+---------------+------+---------------+------+---------+------+--------+-------------+
| id | select_type | table         | type | possible_keys | key  | key_len | ref  | rows   | Extra       |
+----+-------------+---------------+------+---------------+------+---------+------+--------+-------------+
|  1 | SIMPLE      |        t      | ALL  | NULL          | NULL | NULL    | NULL | 757000 | Using where | 
+----+-------------+---------------+------+---------------+------+---------+------+--------+-------------+
1 row in set (0.15 sec)

mysql> DESCRIBE t;
+------------------------+------------------------+------+-----+---------+-------+
| Field                  | Type                   | Null | Key | Default | Extra |
+------------------------+------------------------+------+-----+---------+-------+
| col2                   | varchar(255)           | NO   | MUL | {}      |       | 
| col1                   | varbinary(255)         | NO   | PRI |         |       | 
+------------------------+------------------------+------+-----+---------+-------+
2 rows in set (0.22 sec)
4

2 回答 2

2

如果你在 col1 上有一个索引,并添加LIKE这样的:

SELECT col1, col2
FROM t 
WHERE col1 LIKE 'foobar_%' AND col1 REGEXP '^foobar_[0-9]{4}$';

你应该得到更快的速度。

您还可以创建 (col1, col2) 的索引以使 mysql 在内存中存储该表的副本

于 2012-07-05T05:39:44.803 回答
0

您可以通过创建第三行来加速此查询,如果它始终相同,您已经预先计算了 RegEx。

MySQL 似乎不支持基于函数的索引

于 2012-07-05T05:36:29.913 回答