需要帮助才能让这个更快

时间:2016-07-29 20:09:05

标签: sql sql-server

我需要帮助重写以下查询以使其更快。最终,我想要的是每个服务器(标题)的单行和相应的指标。如果我删除Max和Avgs,我每台服务器获得1600行并在3秒内运行。如果我添加它们,计算单个服务器的数据大约需要3分钟。服务器总数超过11K。

SELECT        
   Nodes.Caption, COUNT(DISTINCT CPUMultiLoad_Detail.CPUIndex) AS CPUCount,
   Max(CPULoad.MaxLoad) as MaxCPU, Avg(CPULoad.AvgLoad) as AvgCPU,
   Round((CPULoad.TotalMemory / 1073741824),0) as MemCount,
   Round(Max(CPULoad.MaxMemoryUsed / CPULoad.TotalMemory * 100),2) as MaxMem,
   Round(Avg(CPULoad.AvgPercentMemoryUsed),2) as AvgMem
FROM 
   Nodes 
INNER JOIN
   CPUMultiLoad_Detail ON Nodes.NodeID = CPUMultiLoad_Detail.NodeID 
INNER JOIN
   CPULoad ON Nodes.NodeID = CPULoad.NodeID
WHERE CPULoad.Datetime >= DATEADD(month,datediff(month,0,getdate())-1,0)
        and CPULoad.Datetime < DATEADD(day,datediff(day,0,getdate()),1) 
GROUP BY Nodes.Caption, CPULoad.TotalMemory

1 个答案:

答案 0 :(得分:0)

不确定这是否有用,没有数据可供测试,但您可以尝试将聚合功能推送到他们需要的位置,然后格式化结果:

SELECT      
    n.nodeid,
    Caption, 
    count_cpuIndex AS CPUCount,
    max_maxLoad AS MaxCPU, 
    avg_avgLoad AS AvgCPU,
    ROUND((TotalMemory / 1073741824),0) AS MemCount,
    ROUND(max_maxMemoryUsedPct * 100, 2) AS MaxMem,
    ROUND(avg_avgPercentMemoryUsed, 2) AS AvgMem
FROM 
   (SELECT
        caption, 
        nodeid
    FROM
        Nodes) n INNER JOIN
   (SELECT
        nodeid,
        COUNT(DISTINCT CPUIndex) AS count_cpuIndex
   FROM
        CPUMultiLoad_Detail 
  GROUP BY 
    nodeid) d ON 
   n.NodeID = d.NodeID INNER JOIN
   (SELECT
       nodeid,
       TotalMemory,
       MAX(MaxLoad) AS max_maxLoad, 
       AVG(AvgLoad) AS avg_avgLoad,
       MAX(MaxMemoryUsed / TotalMemory ) AS max_maxMemoryUsedPct,
       AVG(AvgPercentMemoryUsed) AS avg_avgPercentMemoryUsed
   FROM
        CPULoad
   WHERE
        Datetime >= DATEADD(MONTH,datediff(MONTH,0,getdate())-1,0)        AND 
        Datetime < DATEADD(DAY,datediff(DAY,0,getdate()),1) 
   GROUP BY 
    nodeid,
    TotalMemory) l ON 
   n.NodeID = l.NodeID

此方法的另一个作用是帮助您确定问题所在,因为您可以分别运行带有和不带连接的每个子查询。

HTH