我有一个应用程序从数据库中获取大量数据,填充程序的内部结构。问题是需要花费很多时间。由于在嵌套结构的迭代期间有多个选择查询,因此该过程非常长。现在我正在考虑用很多JOINS进行更大的查询。
在此示例中,我收集了EventDefinitions
。
EventDefinitions
表已加入
EventMonitorTags
- 一个Tag
到多个EventDefinitions
EventRecipients
- 一个Recipient
到多个EventDefinitions
EventReports
- 一个Report
到多个EventDefinitions
让我向您展示一些需要优化的代码:
public static List<DatabaseStructures.EventDefinition> GetEventDefinitionCollection(bool getReportsAndRecipients, int plcNumber, bool onlyActiveEvents)
{
List<DatabaseStructures.EventDefinition> eventDefinitionCollection = new List<DatabaseStructures.EventDefinition>();
string dbQuery = "SELECT * FROM EventDefinitions ed LEFT JOIN Tags t ON ed.triggerTagId = t.tagId";
using (MySqlConnection dbConnection = new MySqlConnection(Connection.DbConnectionString))
{
dbConnection.Open();
using (MySqlCommand dbCommand = new MySqlCommand(dbQuery, dbConnection))
{
using (MySqlDataReader dbReader = dbCommand.ExecuteReader())
{
while (dbReader.Read())
{
DatabaseStructures.EventDefinition eventDefinition = FillEventDefinition(dbReader);
if (eventDefinition.EventActive | !onlyActiveEvents)
{
GetEventMonitorTags(ref eventDefinition);
if (getReportsAndRecipients)
{
GetEventReports(ref eventDefinition);
GetEventRecipients(ref eventDefinition);
}
eventDefinitionCollection.Add(eventDefinition);
}
}
}
}
}
return eventDefinitionCollection;
}
然后有一个代码来为每个EventDefinition结构获取嵌套结构:
private static void GetEventMonitorTags(ref DatabaseStructures.EventDefinition eventDefinition)
{
MySqlConnection dbConnection = new MySqlConnection(Connection.DbConnectionString);
using (dbConnection)
{
dbConnection.Open();
string dbQuery = "SELECT tagId FROM EventMonitorTags WHERE eventDefinitionId = " + eventDefinition.Id;
eventDefinition.MonitorTags = new ArrayList();
using (MySqlCommand dbCommand = new MySqlCommand(dbQuery, dbConnection))
{
using (MySqlDataReader dbReader = dbCommand.ExecuteReader())
{
while (dbReader.Read())
{
int monitorTagId = (int)dbReader["tagId"];
DatabaseStructures.Tag tag = Tags.Get(monitorTagId);
eventDefinition.MonitorTags.Add(tag);
}
}
}
}
}
private static void GetEventReports(ref DatabaseStructures.EventDefinition eventDefinition)
{
MySqlConnection dbConnection = new MySqlConnection(Connection.DbConnectionString);
using (dbConnection)
{
dbConnection.Open();
string dbQuery = "SELECT * FROM EventReports WHERE eventDefinitionId = " + eventDefinition.Id;
eventDefinition.ReportDefinitions = new ArrayList();
using (MySqlCommand dbCommand = new MySqlCommand(dbQuery, dbConnection))
{
using (MySqlDataReader dbReader = dbCommand.ExecuteReader())
{
while (dbReader.Read())
{
int reportDefinitionId = (int)dbReader["reportDefinitionId"];
DatabaseStructures.ReportDefinition reportDefinition = ReportsDefinition.Get(reportDefinitionId);
eventDefinition.ReportDefinitions.Add(reportDefinition);
}
}
}
}
}
private static void GetEventRecipients(ref DatabaseStructures.EventDefinition eventDefinition)
{
MySqlConnection dbConnection = new MySqlConnection(Connection.DbConnectionString);
using (dbConnection)
{
dbConnection.Open();
string dbQuery = "SELECT * FROM EventRecipients WHERE eventDefinitionId = " + eventDefinition.Id;
eventDefinition.Recipients = new ArrayList();
using (MySqlCommand dbCommand = new MySqlCommand(dbQuery, dbConnection))
{
using (MySqlDataReader dbReader = dbCommand.ExecuteReader())
{
while (dbReader.Read())
{
int recipientId = (int)dbReader["recipientId"];
DatabaseStructures.Recipient recipient = Recipients.Get(recipientId);
eventDefinition.Recipients.Add(recipient);
}
}
}
}
}
在每种方法中,都会调用另一个方法来获取正确的对象。在这种情况下,我只展示一个:
public static DatabaseStructures.Tag Get(int tagId)
{
DatabaseStructures.Tag tag = new DatabaseStructures.Tag();
string dbQuery = "SELECT * FROM Tags WHERE tagId = " + tagId;
MySqlConnection dbConnection = new MySqlConnection(Connection.DbConnectionString);
using (dbConnection)
{
dbConnection.Open();
using (MySqlCommand dbCommand = new MySqlCommand(dbQuery, dbConnection))
{
using (MySqlDataReader dbReader = dbCommand.ExecuteReader())
{
while (dbReader.Read())
{
tag = FillTag(dbReader);
}
}
}
}
return tag;
}
在这种情况下,最佳做法是什么?