0

我有一个如下表:

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 并且两个查询的结果相同时)为什么?

4

1 回答 1

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部分以执行一次

于 2013-07-15T09:31:27.917 回答