码迷,mamicode.com
首页 > 数据库 > 详细

oracle 优化or 更换in、exists、union all几个字眼,测试没有问题!

时间:2015-06-26 14:47:57      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

oracle 优化or 更换in、exists、union几个字眼。测试没有问题!

根据实际情况选择相应的语句是。假设指数,or全表扫描,in 和not in 应慎用。否则会导致全表扫描。

 select *
   from T_Pro_Product
  where bar_code = 'nnnmmm'
     or name = 'nnnmmm'
     or no = 'nnnmmm';

select *
   from T_Pro_Product
  where 'nnnmmm' in (bar_code, name, no)
 
 --忧化
   select *
           from T_Pro_Product t1
          where exists
          (select 1
                   from T_Pro_Product tt1
                  where t1.bar_code = 'nnnmmm'
                 union all
                 select 1
                   from T_Pro_Product tt2
                  where t1.no = 'nnnmmm'
                 union all
                 select 1 from T_Pro_Product tt3 where t1.name like 'n%')
         
         --忧化
           select *
                   from T_Pro_Product t1
                  where t1.id in (select id
                                    from T_Pro_Product tt1
                                   where t1.bar_code = 'nnnmmm'
                                  union all
                                  select id
                                    from T_Pro_Product tt2
                                   where t1.no = 'nnnmmm'
                                  union all
                                  select id
                                    from T_Pro_Product tt3
                                   where t1.name = 'nnnmmm')


oracle 优化or 更换in、exists、union all几个字眼,测试没有问题!

标签:

原文地址:http://www.cnblogs.com/yxwkf/p/4602143.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!