更快地获取大量数据库链接对象

时间:2017-12-22 11:12:02

标签: c# mysql

我有一个应用程序从数据库中获取大量数据,填充程序的内部结构。问题是需要花费很多时间。由于在嵌套结构的迭代期间有多个选择查询,因此该过程非常长。现在我正在考虑用很多JOINS进行更大的查询。

在此示例中,我收集了EventDefinitions

的集合

EventDefinitions表已加入

  1. EventMonitorTags - 一个Tag到多个EventDefinitions
  2. EventRecipients - 一个Recipient到多个EventDefinitions
  3. EventReports - 一个Report到多个EventDefinitions
  4. 让我向您展示一些需要优化的代码:

    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;
        }
    

    在这种情况下,最佳做法是什么?

0 个答案:

没有答案