索引失效之Or连接条件

文章展示了在DB2和MySQL数据库中,当OR条件查询涉及到的列中有一个没有索引时,查询优化器如何选择执行计划。在DB2中,如果其中一个条件无索引,整个查询将不会使用任何索引,而MySQL可能会使用索引合并策略。创建或删除索引会显著影响查询性能和成本。
摘要由CSDN通过智能技术生成

用or分割开的条件, 如果or前的条件和后面的列中有一个没有索引,那么涉及的索引都不会被用到。

DB2:

db2expln -d test -statement "select * from employee where id=987 or  name='lisy'" -terminal -graph -opids
Optimizer Plan:

                        Rows   
                      Operato
                        (ID)   
                        Cost   
                              
                      1.99999 
                      RETURN  
                       ( 1)   
                      40.6023 
                        |     
                      1.99999 
                       FETCH  
                       (--)   
                      40.6023 
                     /       \
             1.99999           200003  
             RIDSCN           Table:   
              ( 3)            DB2INST1 
             27.0743          EMPLOYEE 
            /       \
        1               1    
      SORT            SORT   
      ( 4)            ( 6)   
     13.5372         13.5372 
       |               |    
       1               1    
     IXSCAN          IXSCAN 
      ( 5)            ( 7)  
     13.537          13.537 
      |               |         
    200003             4        
 Index:         Index:          
 DB2INST1       DB2INST1        
 EMPLOYEE_IDX1  EMPLOYEE_INDEX2 
[db2inst1@t3-dtpoc-dtpoc-web04 ~]$ db2 "drop index employee_index2"
DB20000I  The SQL command completed successfully.
[db2inst1@t3-dtpoc-dtpoc-web04 ~]$ db2expln -d test -statement "select * from employee where id=987 or  name='lisy'" -terminal -graph -opids
Optimizer Plan:

    Rows   
  Operator 
    (ID)   
    Cost   
          
  1.99999 
  RETURN  
   ( 1)   
  1247.08 
    |     
  1.99999 
  TBSCAN  
   ( 2)   
  1247.08 
    |     
  200003  
 Table:   
 DB2INST1 
 EMPLOYEE
 
 [db2inst1@t3-dtpoc-dtpoc-web04 ~]$ db2 "create index employee_idx2 on employee(name)"
DB20000I  The SQL command completed successfully.
[db2inst1@t3-dtpoc-dtpoc-web04 ~]$ db2 "drop index employee_idx1"
DB20000I  The SQL command completed successfully.
[db2inst1@t3-dtpoc-dtpoc-web04 ~]$ db2expln -d test -statement "select * from employee where id=987 or  name='lisy'" -terminal -graph -opids

End of section


Optimizer Plan:

    Rows   
  Operator 
    (ID)   
    Cost   
          
  1.99999 
  RETURN  
   ( 1)   
  1247.08 
    |     
  1.99999 
  TBSCAN  
   ( 2)   
  1247.08 
    |     
  200003  
 Table:   
 DB2INST1 
 EMPLOYEE
 
 MYSQL;
 mysql>  explain select * from employee where id=987 or  name='lisy';
+----+-------------+----------+------------+-------------+-------------------------------+-------------------------------+---------+------+------+----------+---------------------------------------------------------+
| id | select_type | table    | partitions | type        | possible_keys                 | key                           | key_len | ref  | rows | filtered | Extra                                                   |
+----+-------------+----------+------------+-------------+-------------------------------+-------------------------------+---------+------+------+----------+---------------------------------------------------------+
|  1 | SIMPLE      | employee | NULL       | index_merge | employee_idx1,employee_index2 | employee_idx1,employee_index2 | 5,43    | NULL |    2 |   100.00 | Using union(employee_idx1,employee_index2); Using where |
+----+-------------+----------+------------+-------------+-------------------------------+-------------------------------+---------+------+------+----------+---------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)

mysql> drop index employee_index2 on employee;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql>  explain select * from employee where id=987 or  name='lisy';
+----+-------------+----------+------------+------+---------------+------+---------+------+--------+----------+-------------+
| id | select_type | table    | partitions | type | possible_keys | key  | key_len | ref  | rows   | filtered | Extra       |
+----+-------------+----------+------------+------+---------------+------+---------+------+--------+----------+-------------+
|  1 | SIMPLE      | employee | NULL       | ALL  | employee_idx1 | NULL | NULL    | NULL | 199799 |    10.00 | Using where |
+----+-------------+----------+------------+------+---------------+------+---------+------+--------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

mysql> create index employee_idx2 on employee(name);
Query OK, 0 rows affected (0.64 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> drop index employee_idx1  on employee;               
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql>  explain select * from employee where id=987 or  name='lisy';
+----+-------------+----------+------------+------+---------------+------+---------+------+--------+----------+-------------+
| id | select_type | table    | partitions | type | possible_keys | key  | key_len | ref  | rows   | filtered | Extra       |
+----+-------------+----------+------------+------+---------------+------+---------+------+--------+----------+-------------+
|  1 | SIMPLE      | employee | NULL       | ALL  | employee_idx2 | NULL | NULL    | NULL | 199799 |    19.00 | Using where |
+----+-------------+----------+------------+------+---------------+------+---------+------+--------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值