CS-Base icon indicating copy to clipboard operation
CS-Base copied to clipboard

"在 WHERE 子句中,如果在 OR 前的条件列是索引列,而在 OR 后的条件列不是索引列,那么索引会失效"。 这个结论是不是有问题

Open yueludanfeng opened this issue 3 years ago • 4 comments

mysql> drop view if exists tmp;
create view tmp as 
Query OK, 0 rows affected (0.00 sec)

mysql> create view tmp as 
    -> select (h*1000+ t*100+u*10+v+1) x from
    -> (select 0 h union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) A,
    -> (select 0 t union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) B,
    -> (select 0 u union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) C,
    -> (select 0 v union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) D
    -> order by x;
Query OK, 0 rows affected (0.01 sec)

mysql> 
mysql> 
mysql> drop table if exists test2;
 CREATE TABLE `test2` (
  `id` int(11) NOT NULL,
Query OK, 0 rows affected (0.03 sec)

mysql>  CREATE TABLE `test2` (
    ->   `id` int(11) NOT NULL,
    ->   `name` varchar(11) DEFAULT NULL,
    ->   `age` int(11) DEFAULT NULL,
    ->   PRIMARY KEY (`id`),
    ->   KEY `name` (`name`)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ;

insert into test2 select x , trim(both '' from  concat(x,'name')) , x from tmp;

Query OK, 0 rows affected (0.05 sec)

mysql> 
mysql> insert into test2 select x , trim(both '' from  concat(x,'name')) , x from tmp;

Query OK, 10000 rows affected (0.45 sec)
Records: 10000  Duplicates: 0  Warnings: 0

mysql> 
mysql>     explain select * from test2 where id=1000 or age=100;
+----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows  | filtered | Extra       |
+----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+-------------+
|  1 | SIMPLE      | test2 | NULL       | ALL  | PRIMARY       | NULL | NULL    | NULL | 10000 |    10.01 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

mysql>     explain select * from test2 where age=100 or id=1000 ;
+----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows  | filtered | Extra       |
+----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+-------------+
|  1 | SIMPLE      | test2 | NULL       | ALL  | PRIMARY       | NULL | NULL    | NULL | 10000 |    10.01 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

mysql> 
mysql> select version();
+------------+
| version()  |
+------------+
| 5.7.41-log |
+------------+
1 row in set (0.00 sec)

yueludanfeng avatar Jul 15 '23 10:07 yueludanfeng

type=all

kuun993 avatar Aug 08 '23 02:08 kuun993

早上好!您的邮件我已经收到了,谢谢您的支持!

mrxiaojie avatar Aug 08 '23 02:08 mrxiaojie

谢谢, 我自己弄错了

yueludanfeng avatar Aug 10 '23 07:08 yueludanfeng

下午好!您的邮件我已经收到了,谢谢您的支持!

mrxiaojie avatar Aug 10 '23 07:08 mrxiaojie