2013-07-15 135 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 “不能被束缚。 –