select a.OrderCode,a.AllianceName,a.userID,o.OrderID,o.AddTime,c.ProID,c.ProType,c.SigoPrice,c.BuyCount,o.PayType from Sigo_AlliancesOrders a join Sigo_ProOrder o on a.OrderCode=o.OrderCode right join Sigo_ProCart c on o.OrderID=c.OrderID where a.AllianceName=@AllianceName and (c.AddTime >= @fromTime and c.AddTime<=@toTime) order by a.OrderCodeselect a.OrderCode,a.AllianceName,a.userID,o.OrderID,o.AddTime,c.ProID,c.ProType,c.SigoPrice,c.BuyCount,o.PayType from Sigo_AlliancesOrders a join Sigo_ProOrder o on a.OrderCode=o.OrderCode right join Sigo_ProCart c on o.OrderID=c.OrderID where a.AllianceName=@AllianceName and (o.AddTime >= @fromTime and o.AddTime<=@toTime) order by a.OrderCode
select a.OrderCode,a.AllianceName,a.userID,o.OrderID,o.AddTime,c.ProID,c.ProType,c.SigoPrice,c.BuyCount,o.PayType from Sigo_AlliancesOrders a join Sigo_ProOrder o on a.OrderCode=o.OrderCode right join Sigo_ProCart c on o.OrderID=c.OrderID where a.AllianceName=@AllianceName and (o.AddTime >= @fromTime and o.AddTime<=@toTime) order by a.OrderCode

解决方案 »

  1.   

    这两条sql语句在执行时的性能差别很大
      

  2.   

    两个结果是不一样的,但如果
    Sigo_ProCart
    Sigo_ProOrder
    两表完全匹配则结果一样。
      

  3.   

    表Sigo_ProOrder的addtime列是否没建索引?而Sigo_ProCart表的addtime字段建了?
      

  4.   

    给你个例子,就明白了
    create table #a(id int,date varchar(10))insert into #a 
    select 1,'2010-08-01' union all
    select 2,'2010-08-02'create table #b(id int,date varchar(10)) 
    insert into #b
    select 1,'2010-08-01' union all
    select 3,'2010-08-04'
    select *from #a a right join #b b on a.id=b.id 
    where a.date between '2010-08-01' and '2010-08-30'
    /*
    id          date       id          date
    ----------- ---------- ----------- ----------
    1           2010-08-01 1           2010-08-01(1 行受影响)*/
    select *from #a a right join #b b on a.id=b.id 
    where b.date between '2010-08-01' and '2010-08-30'
    /*
    id          date       id          date
    ----------- ---------- ----------- ----------
    1           2010-08-01 1           2010-08-01
    NULL        NULL       3           2010-08-04(2 行受影响)*/
      

  5.   

    谢谢楼上的回答  ,两个表的数据是统一的 ,我想说的是这两条sql语句执行所需要的时间差别很大
      

  6.   

    不知道是不是我数据库的原因还是查询语句的原因  我用这两个sql语句在程序中执行的时候 一条会超时 一条执行正常
      

  7.   

    我问的不是结果集出来的是什么格式的,是想问这两条sql语句执行的速度问题
      

  8.   

    性能你只有察看執行計劃,,
    或者用with顯示寫清楚你要使用的索引
    MSSQL的會使用它覺得可能最有的方式來優化-- ,天知道它在搞什麽
    我有次就踫到,,A查詢  5分鐘  B查詢 1分鐘 ,但重點是B查詢居然比A查詢還多平行連接一個30多W筆數據的表
    或者踫到更改一個where條件就導致速度減慢10倍的情況