我有一个存储过程,它能够从DB(通常是2500+)中检索行,并且执行和完成相当快。使用DBReader
收集和阅读此信息,如下所示:
var command = new SqlCommand("GetTestResults", connection) { CommandType = CommandType.StoredProcedure };
command.Parameters.Add(new SqlParameter("@id", _testrunID));
command.CommandTimeout = 0;
connection.Open();
DBReader rdr = new DBReader(command.ExecuteReader());
AlertItem newAlertItem;
while (rdr.read())
{
//Populate new alert item
newAlertItem = new AlertItem(rdr.getInt("TestRunMetricID"), rdr.getString("Type"), rdr.getString("Name"),
rdr.getDouble("Value"), rdr.getString("Machine_Name"), rdr.getString("ExpectedProfile"),
rdr.getInt("MetricID"), rdr.getString("Alias"), rdr.getString("GroupName"),
rdr.getString("AlertTypeName"), rdr.getDouble("Green"), rdr.getDouble("red"),
rdr.getString("CounterName"), rdr.getString("PlatformName"), rdr.getInt("PlatformEnv"), rdr.getInt("InstanceID"));
//Get all alerts associated to test run metric
newAlertItem.associatedAlerts = GraphingService.GetAssociatedAlertTypes(newAlertItem.alertTypeName, db.TestRunMetricRepo.getByID(newAlertItem.testrunmetric).Alerts.ToList());
allAlerts.Add(newAlertItem);
}
这也运行得相当快,瓶颈位于GraphingService.GetAssociatedAlertTypes()
;这接受一个列表(allAlerts
)并返回一个新的列表,省略selectedAlert
的任何内容:
public static List<AssociatedAlertData> GetAssociatedAlertTypes(string selectedAlert, List<Alert> allAlerts)
{
List<AssociatedAlertData> additionalAlerts = new List<AssociatedAlertData>();
if (allAlerts != null)
{
foreach (Alert alert in allAlerts)
{
if (!alert.AlertRanking.AlertType.Name.Trim().Equals(selectedAlert))
additionalAlerts.Add(new AssociatedAlertData(alert.AlertRanking.AlertType.Name.Trim(), ReturnAlertCategory(alert).ToString(), Math.Round(alert.Value, 2)));
}
}
return additionalAlerts;
}
GraphingService.GetAssociatedAlertTypes()
大大增加了方法执行时间,但是我们正在努力加速这种方法。我们怎么能重构这个方法呢?