我使用enum作为检查约束的替代方法,mysql仍然不支持这种约束。您能说明一下为什么优化器不考虑枚举的允许值吗?例如,
CREATE TABLE test_1(id int not null auto_increment PRIMARY KEY, val enum('A','B') not null);
insert into test_1(val) values ('A');
1. explain select * from test_1 where val is null;
2. explain select * from test_1 where val ='C';第一种解释以额外的形式显示impossible where,而第二种解释则不显示。val不可能包含'C‘(以及null),而且可以在不查看表的情况下从表定义中扣除它。
谢谢。
发布于 2012-01-12 18:39:02
我创建了您提供的示例如下:
drop database if exists a1ex07;
create database a1ex07;
use a1ex07
CREATE TABLE test_1(id int not null auto_increment PRIMARY KEY, val enum('A','B') not null);
insert into test_1(val) values ('A');
explain select * from test_1 where val is null;
explain select * from test_1 where val ='C'; 这是装好的:
mysql> drop database if exists a1ex07;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> create database a1ex07;
Query OK, 1 row affected (0.00 sec)
mysql> use a1ex07
Database changed
mysql> CREATE TABLE test_1(id int not null auto_increment PRIMARY KEY, val enum('A','B') not null);
Query OK, 0 rows affected (0.05 sec)
mysql> insert into test_1(val) values ('A');
Query OK, 1 row affected (0.06 sec)
mysql> explain select * from test_1 where val is null;
+----+-------------+-------+------+---------------+------+---------+------+------+------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+------------------+
| 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Impossible WHERE |
+----+-------------+-------+------+---------------+------+---------+------+------+------------------+
1 row in set (0.00 sec)
mysql> explain select * from test_1 where val ='C';
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | test_1 | ALL | NULL | NULL | NULL | NULL | 1 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
mysql>在第一个解释计划中,Impossible where将基于您给出的列的定义,即val enum('A','B') not null。这将帮助MySQL查询优化器快速排除不相关的计算,特别是当表有数百万行时。
在第二个解释计划中,Using where表示MySQL查询优化器必须计算一个潜在值(C),以查看它是否驻留在表中。我相信您要寻找的是MySQL查询优化器检查列的定义,它是enum('A','B')。你应该把它作为一个bug报告来提交,看看甲骨文,出于他们的好意,是否会解决这个问题。
我只是在mysql网站上查找有关解释和ENUM的任何bug。在这个时候,没有关于这种自然界的昆虫的报道。你应该尽快提交这份报告。
https://dba.stackexchange.com/questions/10595
复制相似问题