奇怪的SQLserver執行

來源:互聯網
上載者:User

標籤:

 今天工作遇到,下面這段執行超慢,幾乎不出來,在舊伺服器上運行是可以的,一樣代碼,很快,在新虛擬伺服器上,超慢。

     select * from         (            select RowNum=row_number() over(order by AutoId desc),StartTime,cast(testtime/1000.00 as decimal(10,2)) as testtime,AutoId            from dbo.FTMaster M             WITH(NOLock) Where StartTime >=‘2014-01-01 00:00:00.000‘ and StartTime <‘2014-12-31 23:00:00.000‘ and ModelIc=‘XXX‘ and Result=0 and TestType=3                        and exists            (                    select AutoId from                 (                    select Min(BB.autoid) AutoID 
            from (Select Max(StartTime) as starttime,SN From dbo.FTMaster WITH(NOLock)
             Where StartTime >=‘2014-01-01 00:00:00.000‘ and StartTime <‘2014-12-31 23:00:00.000‘ and ModelIc=‘XXX‘ and TestType=3 group by Sn) as AA inner join
            (Select AutoID,SN,StartTime From dbo.FTMaster WITH(NOLock)   
             Where StartTime >=‘2014-01-01 00:00:00.000‘ and StartTime <‘2014-12-31 23:00:00.000‘ and ModelIc=‘XXX‘ and TestType=3 ) As BB
             on AA.SN=BB.SN And AA.StartTime=BB.StartTime group by BB.SN ) t where t.AutoId=M.AutoId ) ) as MasterTable WHERE RowNum between (1 - 1) * 12430 + 1 and 1 * 12430

執行裡面的又很快, 為什麼呢。

       select RowNum=row_number() over(order by AutoId desc),StartTime,cast(testtime/1000.00 as decimal(10,2)) as testtime,AutoId            from dbo.FTMaster M             WITH(NOLock) Where StartTime >=‘2014-01-01 00:00:00.000‘ and StartTime <‘2014-12-31 23:00:00.000‘ and ModelIc=‘XXX‘ and Result=0 and TestType=3                        and exists            (                    select AutoId from                 (                    select Min(BB.autoid) AutoID             from                      (Select Max(StartTime) as starttime,SN From dbo.FTMaster WITH(NOLock)              Where StartTime >=‘2014-01-01 00:00:00.000‘ and StartTime <‘2014-12-31 23:00:00.000‘ and ModelIc=‘XXX‘ and TestType=3 group by Sn) as AA                      inner join             (Select AutoID,SN,StartTime From dbo.FTMaster WITH(NOLock)                Where StartTime >=‘2014-01-01 00:00:00.000‘ and StartTime <‘2014-12-31 23:00:00.000‘ and ModelIc=‘XXX‘ and TestType=3 ) As BB              on AA.SN=BB.SN And AA.StartTime=BB.StartTime                     group by BB.SN                ) t where t.AutoId=M.AutoId             )    

 

奇怪的SQLserver執行

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.