sql优化之模糊查询

文章讨论了在数据库操作中,使用模糊查询如LIKE关键字时,特别是在查询模式以通配符%开头的情况下,如何影响索引的效率。当%zha%这样的模式出现在查询条件的头部时,DB2和MySQL的索引会失效,导致全表扫描,从而降低了查询性能。
摘要由CSDN通过智能技术生成


索引中禁止头部出现模糊查询

# 如果头部出现%,索引失效

-- 索引有效
explain select * from employee where name like 'zha%';

-- 索引失效
explain select * from employee where name like '%zha';

-- 索引失效
explain select * from employee where name like '%zha%';

[db2inst1@ ~]$ db2expln -d test -statement "select * from employee where  name like 'zha%'" -terminal -graph -opids
Optimizer Plan:

             Rows   
           Operator 
             (ID)   
             Cost   
                     
         4.65661e-05 
           RETURN    
            ( 1)     
           13.5373   
             |       
         4.65661e-05 
            FETCH    
            ( 2)     
           13.5373   
        /           \
   4.65661e-05     200000  
     IXSCAN       Table:   
      ( 3)        DB2INST1 
     13.5369      EMPLOYEE 
       |         
        0        
 Index:          
 DB2INST1        
 EMPLOYEE_INDEX2 


[db2inst1@ ~]$ db2expln -d test -statement "select * from employee where  name like '%zha'" -terminal -graph -opids 

Optimizer Plan:

    Rows   
  Operator 
    (ID)   
    Cost   
          
   20000  
  RETURN  
   ( 1)   
  1234.25 
    |     
   20000  
  TBSCAN  
   ( 2)   
  1234.25 
    |     
  200000  
 Table:   
 DB2INST1 
 EMPLOYEE 

[db2inst1@ ~]$ db2expln -d test -statement "select * from employee where  name like '%zha%'" -terminal -graph -opids


Optimizer Plan:

    Rows   
  Operator 
    (ID)   
    Cost   
          
   20000  
  RETURN  
   ( 1)   
  1234.25 
    |     
   20000  
  TBSCAN  
   ( 2)   
  1234.25 
    |     
  200000  
 Table:   
 DB2INST1 
 EMPLOYEE 


mysql> explain select * from employee where name like 'zha%';
+----+-------------+----------+------------+-------+-----------------+-----------------+---------+------+------+----------+-----------------------+
| id | select_type | table    | partitions | type  | possible_keys   | key             | key_len | ref  | rows | filtered | Extra                 |
+----+-------------+----------+------------+-------+-----------------+-----------------+---------+------+------+----------+-----------------------+
|  1 | SIMPLE      | employee | NULL       | range | employee_index2 | employee_index2 | 43      | NULL |    1 |   100.00 | Using index condition |
+----+-------------+----------+------------+-------+-----------------+-----------------+---------+------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)

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

mysql> explain select * from employee where name like '%zha%';
+----+-------------+----------+------------+------+---------------+------+---------+------+--------+----------+-------------+
| id | select_type | table    | partitions | type | possible_keys | key  | key_len | ref  | rows   | filtered | Extra       |
+----+-------------+----------+------------+------+---------------+------+---------+------+--------+----------+-------------+
|  1 | SIMPLE      | employee | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 199799 |    11.11 | 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、付费专栏及课程。

余额充值