比如: select id, name from table1 where name = 'x' union all select id, name from table2 where name = 'x'      

select * from (select id, name from table1 union all select id, name from table2) where name = 'x'.哪一种方式性能更好一些呢? 希望高手能详细说明下, 并且考虑到有索引和无索引的情况

解决方案 »

  1.   

    个人觉得先WHERE过滤数据后UNION ALL的方式会好一些。如果表数据量大的话,这种方式效率会更高一些。
      

  2.   

    正常都是先where 过滤掉数据比较好
      

  3.   

    先WHERE过滤数据后UNION ALL的快
      

  4.   

    看数据量吧。set timing on 根据你现实的数据量,用时间来证实。
      

  5.   

    建议使用先where再union的方式,通过过滤得到小的数据量,然后再去和其他数据做join 等处理,这应该是数据处理的一个原则。当然了,将过多的数据抓过来,经过了层层处理,最后再通过条件去过滤,这也是效率低下语句的通病。不过,对于你的两条语句,有可能他们的执行计划都是一样的,不管有没有索引的情况。
      

  6.   

    select id, name from table1 where name = 'x' union all select id, name from table2 where name = 'x' 
    更加块
      

  7.   

    先用where过滤,再用union快......可以用执行计划查看COST...
    也可以通过实际速度来查看...
      

  8.   

    先 where 再 union 快
      

  9.   

    答案这么统一?呵呵
    我也认为where 快