Bug #62738 | Index is not used if I use IS TRUE instead of = TRUE when filtering | ||
---|---|---|---|
Submitted: | 14 Oct 2011 14:32 | Modified: | 16 Oct 2012 5:15 |
Reporter: | Cyril SCETBON | Email Updates: | |
Status: | Won't fix | Impact on me: | |
Category: | MySQL Server: Optimizer | Severity: | S4 (Feature request) |
Version: | 5.1.60, 5.5.16 | OS: | Any |
Assigned to: | CPU Architecture: | Any | |
Tags: | boolean, explain, INDEX |
[14 Oct 2011 14:32]
Cyril SCETBON
[14 Oct 2011 14:46]
Valeriy Kravchuk
Thank you for the bug report. Verified just as described: macbook-pro:5.1 openxs$ bin/mysql -uroot test Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.1.60 Source distribution Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> create table t(id int, b boolean default FALSE); Query OK, 0 rows affected (0.13 sec) mysql> insert into t(id) select 1; Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> insert into t(id) select id from t; Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> insert into t(id) select id from t; Query OK, 2 rows affected (0.02 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> insert into t(id) select id from t; Query OK, 4 rows affected (0.00 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> insert into t(id) select id from t; Query OK, 8 rows affected (0.00 sec) Records: 8 Duplicates: 0 Warnings: 0 mysql> insert into t(id) select id from t; Query OK, 16 rows affected (0.00 sec) Records: 16 Duplicates: 0 Warnings: 0 mysql> alter table t add index(b); Query OK, 32 rows affected (0.18 sec) Records: 32 Duplicates: 0 Warnings: 0 mysql> insert into t values(10,TRUE); Query OK, 1 row affected (0.00 sec) mysql> select * from t; +------+------+ | id | b | +------+------+ | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 1 | 0 | | 10 | 1 | +------+------+ 33 rows in set (0.00 sec) mysql> explain select count(*) from t where b is true\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: t type: index possible_keys: NULL key: b key_len: 2 ref: NULL rows: 33 Extra: Using where; Using index 1 row in set (0.03 sec) mysql> explain select count(*) from t where b = true\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: t type: ref possible_keys: b key: b key_len: 2 ref: const rows: 1 Extra: Using where; Using index 1 row in set (0.00 sec) Index is used in both cases, but it is "ref" access for one row vs "index" (entire index scan).
[16 Oct 2012 5:15]
Erlend Dahl
Analysis from dev team: This would have been a valid and useful feature request if MySQL supported the data type boolean. However, when creating a table and specifying a boolean data type, the server interprets this as synonym for tinyint(1). According to the manual, "Nonzero values are considered true" for such data types, so it will not be possible for us to transform the expression "b IS TRUE" into "b = 1", because it will miss all values that are not zero and not 1.