2013-07-15 178 views
0

我有一個表像下面:SQL查詢性能

CREATE TABLE MetalTemprature(
    idMetalTemprature int 
    rawTime bigint NOT NULL, 
    metal nchar(7) NOT NULL, 
    color nchar(5) NOT NULL, 
    Temp float NOT NULL) 

和打擊指數:

PRIMARY KEY CLUSTERED 
(
    idMetalTemprature ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON PRIMARY 
) ON PRIMARY 

CREATE NONCLUSTERED INDEX NonClusteredIndex1112 ON MetalTemprature 
(
    rawTime DESC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON PRIMARY 

當我運行此查詢取0秒要做到這一點:

SELECT count(*) 
    FROM MetalTemprature 
    where rawTime < 4449449575 and rawTime > (4449449575 -10000000) and metal = 'iron'; 

但當我把這個查詢下其他選擇像下面

SELECT 
    SELECT count(*) 
      FROM MetalTemprature 
      where rawTime < other.rawTime and rawTime > (other.rawTime -10000000) and metal = 'iron'; 
from other_table_only_one_row as other; 

這需要約60秒(當other.rawTime只有4449449575和兩個查詢的結果是相同的)爲什麼?

回答

0
SELECT * 
from other_table_only_one_row as other, (SELECT count(*) 
      FROM MetalTemprature 
      where rawTime < other.rawTime and rawTime > (other.rawTime -10000000) and metal = 'iron') as cnt 

將其放入FROM部件,執行只有一次

+0

Thnaks但是當我嘗試,但我得到的錯誤消息4104,級別16,狀態1,行10 多部分組成的標識符「other.rawTime 「不能被束縛。 –