1

你好,我的观点有问题。子查询导致视图运行非常缓慢。

SELECT dbo.Calls.CallID
      ,dbo.Calls.StartTime
      ,dbo.Calls.EndTime
      ,dbo.Connections.Connectionname
      ,dbo.Repositorys.RepositoryName
      ,REPLACE(dbo.Calls.Querytime ,',' ,'.') AS Querytijd
      ,dbo.Calls.Uur
      ,dbo.Calls.DayOfMonth
      ,REPLACE(
           (
               SELECT MAX(Querytime)  AS MaxQueryTime
               FROM   dbo.Calls       AS C
               WHERE  (
                          DATEPART(yyyy ,StartTime)=DATEPART(yyyy ,dbo.Calls.StartTime)
                      )
                      AND (DATEPART(M ,StartTime)=DATEPART(M,dbo.Calls.StartTime))               
                      AND (DayOfMonth=dbo.Calls.DayOfMonth)
                      AND (Uur=dbo.Calls.Uur)
                      AND (
                              DATEPART(MINUTE ,dbo.Calls.StartTime)=DATEPART(Minute ,StartTime)
                          )
           )
          ,','
          ,'.'
       ) AS MaxQueryTime
FROM   dbo.Calls
       INNER JOIN dbo.Connections
            ON  dbo.Calls.ConnectionID = dbo.Connections.ConnectionID
       LEFT OUTER JOIN dbo.Repositorys
            ON  dbo.Connections.RepositoryID = dbo.Repositorys.RepositoryID

我基本上想要 StartTime 的年/月/日/小时/分钟相同的最大 QueryTime。

4

4 回答 4

2

在 SQLServer2005+ 中,您可以使用OVER()子句

SELECT dbo.Calls.CallID, 
       dbo.Calls.StartTime, 
       dbo.Calls.EndTime,
       dbo.Connections.Connectionname,
       dbo.Repositorys.RepositoryName, 
       REPLACE(dbo.Calls.Querytime, ',', '.') AS Querytijd, 
       dbo.Calls.Uur, 
       dbo.Calls.DayOfMonth, 
       REPLACE(MAX(Querytime) OVER(PARTITION BY DATEPART(yyyy ,StartTime), 
                                                DATEPART(Minute ,StartTime), 
                                                DayOfMonth, Uur), ',', '.'
               ) AS MaxQueryTime
FROM dbo.Calls INNER JOIN dbo.Connections 
                 ON dbo.Calls.ConnectionID = dbo.Connections.ConnectionID 
               LEFT OUTER JOIN dbo.Repositorys 
                 ON dbo.Connections.RepositoryID = dbo.Repositorys.RepositoryID
于 2013-04-12T10:02:58.373 回答
0
SELECT dbo.Calls.CallID
  ,dbo.Calls.StartTime
  ,dbo.Calls.EndTime
  ,dbo.Connections.Connectionname
  ,dbo.Repositorys.RepositoryName
  ,REPLACE(dbo.Calls.Querytime ,',' ,'.') AS Querytijd
  ,dbo.Calls.Uur
  ,dbo.Calls.DayOfMonth
  ,REPLACE( QUERYTIME,',','.') AS MaxQueryTime
FROM   dbo.Calls
   INNER JOIN dbo.Connections
        ON  dbo.Calls.ConnectionID = dbo.Connections.ConnectionID
   LEFT OUTER JOIN dbo.Repositorys
        ON  dbo.Connections.RepositoryID = dbo.Repositorys.RepositoryID
LEFT JOIN 
(
SELECT StartTime, Uur, DayOfMonth, StartTime, MAX(QUERYTIME) AS QUERYTIME
FROM dbo.Calls       
GROUP BY StartTime, Uur, DayOfMonth, StartTime
) AS C
ON DATEPART(yyyy ,C.StartTime)=DATEPART(yyyy ,dbo.Calls.StartTime)
AND C.DayOfMonth=dbo.Calls.DayOfMonth
AND C.Uur=dbo.Calls.Uur
AND DATEPART(MINUTE ,dbo.Calls.StartTime)=DATEPART(Minute ,C.StartTime)
于 2013-04-12T09:53:26.890 回答
0

当我查看您的查询时..您正在为每一行计算“MaxQueryTime”。要优化此查询,您可以计算一次“MaxQueryTime”,然后使用 union 关键字组合到您的结果集。据我说,“MaxQueryTime”对所有人都是一样的。

(SELECT REPLACE(MAX(Querytime),',','.')  AS MaxQueryTime
               FROM   dbo.Calls       AS C
               WHERE  (
                          DATEPART(yyyy ,StartTime)=DATEPART(yyyy ,dbo.Calls.StartTime)
                      )
                      AND (DayOfMonth=dbo.Calls.DayOfMonth)
                      AND (Uur=dbo.Calls.Uur)
                      AND (
                              DATEPART(MINUTE ,dbo.Calls.StartTime)=DATEPART(Minute ,StartTime)
                          )) union all

                ( SELECT dbo.Calls.CallID
      ,dbo.Calls.StartTime
      ,dbo.Calls.EndTime
      ,dbo.Connections.Connectionname
      ,dbo.Repositorys.RepositoryName
      ,REPLACE(dbo.Calls.Querytime ,',' ,'.') AS Querytijd
      ,dbo.Calls.Uur
      ,dbo.Calls.DayOfMonth

FROM   dbo.Calls
       INNER JOIN dbo.Connections
            ON  dbo.Calls.ConnectionID = dbo.Connections.ConnectionID
       LEFT OUTER JOIN dbo.Repositorys
            ON  dbo.Connections.RepositoryID = dbo.Repositorys.RepositoryID         )
于 2013-04-12T10:00:15.643 回答
0

它查询工作吗?

当我查看您的查询时,“发件人”关键字在单个查询中使用了两次。只有两个表的连接。如果您想快速获得结果,请先过滤两个表,然后进行连接,这样连接中的行数就会减少,从而优化您的结果。

SELECT dbo.Calls.CallID, dbo.Calls.StartTime, dbo.Calls.EndTime, 
dbo.Connections.Connectionname, dbo.Repositorys.RepositoryName, 
REPLACE(dbo.Calls.Querytime, ',', '.') AS Querytijd, dbo.Calls.Uur, 
dbo.Calls.DayOfMonth, REPLACE((SELECT MAX(Querytime) AS MaxQueryTime 
FROM dbo.Calls AS C 
WHERE (DATEPART(yyyy, StartTime) = DATEPART(yyyy, dbo.Calls.StartTime))
 AND (DayOfMonth = dbo.Calls.DayOfMonth) AND (Uur = dbo.Calls.Uur) AND 
 (DATEPART(MINUTE, dbo.Calls.StartTime) = DATEPART(Minute, StartTime))), ',', '.') AS MaxQueryTime 

 FROM dbo.Calls INNER JOIN dbo.Connections ON dbo.Calls.ConnectionID = dbo.Connections.ConnectionID 
 LEFT OUTER JOIN dbo.Repositorys ON dbo.Connections.RepositoryID = dbo.Repositorys.RepositoryID
于 2013-04-12T09:40:54.140 回答