SQL1:
select sLY,sum(fZJE) from (
select sLY,sum(fZJE) as fZJE from DB1..FYMXB where dSJ>'2009-10-1' and dSJ<'2010-4-30' group by sLY
union all
select sLY,sum(fZJE) as fZJE from DB2..FYMXB where dSJ>'2009-10-1' and dSJ<'2010-4-30' group by sLY)a
group by sLYSQL2:
select sLY,sum(fZJE) from (
select * from DB1..FYMXB where dSJ>'2009-10-1' and dSJ<'2010-4-30'
union all
select * from DB2..FYMXB where dSJ>'2009-10-1' and dSJ<'2010-4-30')a
group by sLY请问,哪个SQL效率最高?

解决方案 »

  1.   

    SQL1效率更高些,这个在表中存在多个Image字段的时候,用数据集打开,效果最明显,差距很大。
      

  2.   

    我在300w数据表中测试,感觉两个表的速度差不多呀。是不是SQLserver本身对SQL语句进行了优化?
      

  3.   

    1.个人觉得在这个查询之前,先查看一下这两个数据库的FYMXB表有没有针对dsj字段的索引,如果没有的话,先建这个索引。 create index index_dsj on FYMXB(dSJ)
    2.再用下面的查询:
    select sLY,sum(fZJE) from (
    select SlY,fZJE from DB1..FYMXB where dSJ>'2009-10-1' and dSJ<'2010-4-30'
    union all
    select SlY,fZJE from DB2..FYMXB where dSJ>'2009-10-1' and dSJ<'2010-4-30')a
    group by sLY