结合两个SQL查询以实现网络利用率

时间:2015-12-03 23:10:54

标签: sql join network-utilization

寻求一些帮助,结合两个SQL查询(或提供可行性意见。)我们的卫生局,网络服务小组单独使用它们来规划WAN带宽分配。我们的网络监控系统使用MS SQL Server 2012作为数据库。

在过去30天内,当WAN链接的平均利用率超过90%时,第一个查询会提取指标。这不是由我创建的,但似乎是高度使用的网络的良好代理。不知道为什么在'其中'中有两个日期时间语句。子句:

DECLARE @StartDate DateTime
DECLARE @EndDate DateTime

SET @StartDate =CAST((ROUND(CAST(GetDate() - 30 AS FLOAT), 0, 1)) as datetime) 
SET @EndDate = GetDate()

SELECT     
    Nodes.HA, 
    Nodes.Site, 
    Nodes.Address,
    Nodes.City,
    Nodes.SiteType,
    Interfaces.WANFeed, 
    (Interfaces.InBandwidth / 1000000) As Subscribed_Mbps,
    Count(*) as SaturationEvents

FROM
    Interfaces 
    INNER JOIN InterfaceTraffic_Detail ON Interfaces.InterfaceID = InterfaceTraffic_Detail.InterfaceID 
    INNER JOIN Nodes ON Interfaces.NodeID = Nodes.NodeID
WHERE      
    (NOT (Interfaces.WANFeed IS NULL)) 
    AND (InterfaceTraffic_Detail.DateTime > DATEADD(day, -30, GETDATE())) 
    AND (InterfaceTraffic_Detail.In_Averagebps / Interfaces.InBandwidth > .9)
    AND (Nodes.SiteType = 'Acute')
    AND (DateTime between @StartDate and @EndDate)

GROUP BY 
    Nodes.HA,
    Nodes.SiteType,
    Nodes.Site,
    Nodes.Address,
    Nodes.City, 
    Interfaces.WANFeed,
    Interfaces.InBandwidth

Order By SaturationEvents Desc

Sample output (cleaned)

HA  SITE    Address City    SiteType    WANFeed Subscribed_Mbps Saturation Events
--- --- --- --- Acute   Primary 10  252
--- --- --- --- Acute   Primary 10  152
--- --- --- --- Acute   Primary 100 104
--- --- --- --- Acute   Primary 10  57

我根据NMS供应商提供的示例创建了第二个查询。它报告了第95百分位利用率:

DECLARE @StartDate DateTime
DECLARE @EndDate DateTime

SET @StartDate =CAST((ROUND(CAST(GetDate() - 30 AS FLOAT), 0, 1)) as datetime) 
SET @EndDate = GetDate()

SELECT  

Nodes.HA,
Nodes.Site,
Nodes.Address,
Nodes.City,
Interfaces.WANFeed,
Nodes.SiteType,
(Interfaces.InBandwidth / 1000000) As Subscribed_Mbps,
Interfaces.InterfaceId,
Nodes.NodeID,
Nodes.Caption AS NodeName,
Interfaces.Caption AS Interface_Caption,
Maxbps_In95,
Maxbps_Out95,
Maxbps_95

FROM Nodes
INNER JOIN Interfaces ON Nodes.NodeID = Interfaces.NodeID
INNER JOIN (
    SELECT  InterfaceID,
            dbo.GetInBps95th(InterfaceID,@StartDate ,@EndDate ) AS Maxbps_In95,
            dbo.GetOutBps95th(InterfaceID,@StartDate ,@EndDate ) AS Maxbps_Out95,
            dbo.GetMaxBps95th(InterfaceID,@StartDate ,@EndDate ) AS Maxbps_95
    FROM InterfaceTraffic
    WHERE InterfaceTraffic.DateTime >= @StartDate AND InterfaceTraffic.DateTime <= @EndDate 
    GROUP BY InterfaceID
) TrafficStat
ON Interfaces.InterfaceID = TrafficStat.InterfaceID

WHERE
(NOT (Interfaces.WANFeed IS NULL)) AND
(Nodes.SiteType = 'Acute') AND
(Interfaces.WANFeed <> 'Secondary') 

ORDER BY HA,NodeName, Interface_Caption

Sample Output (cleaned)

SITE    Address City    SiteType    WANFeed Subscribed_Mbps InterfaceID NodeID  Hostname    Interface_Caption   Maxbps_In95 Maxbps_Out95    Maxbps_95
--- --- --- Primary Acute   10000   25899   3516    --- --- 2.84E+08    3.92E+08    4.01E+08
--- --- --- Primary Acute   20000   23428   3508    --- --- 7.44E+08    3.50E+08    7.52E+08
--- --- --- Primary Acute   10000   23354   3508    --- --- 5.74E+08    4.61E+08    6.46E+08
--- --- --- Primary Acute   10000   23368   3508    --- --- 2.28E+08    4.59E+07    2.28E+08
--- --- --- Primary Acute   1000    11389   1232    --- --- 8.42E+07    8.55E+07    1.06E+08
--- --- --- Primary Acute   1000    29360   1234    --- --- 6.65E+07    3.95E+07    7.46E+07

我希望它们可以组合成一个报告(只有一些网站会饱和,但都会有95%。)

感谢您花时间看这个。

1 个答案:

答案 0 :(得分:0)

首先,我认为它会导致更多的混淆,而不是直接合并到一个查询中。但是,您可以声明两个临时表,从不同的查询中填充每个表,然后从表之间的连接中选择所需的结果集。

此外,似乎查询比必要的要复杂得多,并且可以使得运行速度更快,并且可以通过调整微调来更清晰。我建议如下:

  1. 不需要@EndDate参数,因为查询运行到当前时间。因此,请更改每个日期比较以查找DateTime >= @StartDate
  2. (NOT (Interfaces.WANFeed IS NULL))增加了不必要的复杂性。请改用(Interfaces.WANFeed IS NOT NULL)
  3. 删除或澄清第一个查询中AND (DateTime between @StartDate and @EndDate)子句末尾的WHERE。它看起来多余,并且没有完全限定其正确的别名。如果这是与第一个日期比较不同的字段,只需添加表别名以进行说明。
  4. 我希望所有这些都有所帮助。