从MySQL数据库获取数百万行并写入csv的性能降低

时间:2018-12-28 17:45:51

标签: c# mysql wpf mysqldatareader

编辑:遍历Reader需要时间,Execute Reader会在几秒钟后返回。有没有比迭代更快速的方法了?

我正在尝试基于过滤器从MySQL获取数据。以前我使用EF的速度非常慢,所以后来我决定切换到使用MySQLDataReader读取数据,当结果包含的记录很少时,它可以正常工作,但是对于大结果(比如说百万行),它的运行速度非常慢。 我正在使用以下函数来制定查询。

public string GetCorrespondingQuery()
    {
        string zips = "\"" + string.Join("\",\"", _zips) + "\"";
        string counties = "\"" + string.Join("\",\"", _counties) + "\"";
        string states = "\"" + string.Join("\",\"", _states) + "\"";
        string cities = "\"" + string.Join("\",\"", _cities) + "\"";

        if ((_zips.Count == 0) && (_states.Count == 0) && (_counties.Count == 0) && (_cities.Count == 0))
            throw new Exception("Alert! No Filters Selected.");
        string query = "select * from dbcustomer JOIN dbzipcode On dbcustomer.ZIPCODE = dbzipcode.ZIP";
        if (_zips.Count > 0)
            query += " where dbzipcode.Zip in (" + zips + ")";
        if (_states.Count > 0)
            query += " and dbzipcode.STATE in (" + states + ")";
        if (_cities.Count > 0)
            query += " and dbzipcode.City in (" + cities + ")";
        if (_counties.Count > 0)
            query += " and dbzipcode.County in (" + counties + ")";

        return query + ";";
    }

在MySQL Workbench中执行上述查询需要花费几秒钟,而使用C#则需要花费几分钟。

以下是从MySQL数据库获取数据的代码。

public List<CustomerDTO> FetchCustomersUsingQuery(CustomersFilter filter)
    {
        string query = filter.GetCorrespondingQuery();
        List<CustomerDTO> customers = new List<CustomerDTO>();
        using (MySqlConnection con = new MySqlConnection(_connectionString))
        {
            MySqlCommand cmd = new MySqlCommand(query, con);
            cmd.CommandTimeout = 0;
            cmd.CommandType = CommandType.Text;

            con.Open();
            using (MySqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    var customer = new CustomerDTO()
                    {
                        PHONE = reader.GetString(0),
                        FIRSTNAME = reader.GetString(1),
                        LASTNAME = reader.GetString(2),
                        ADDRESS = reader.GetString(3),
                        CStatus = reader.GetString(4),
                        Campaign = reader.GetString(5),
                        ListName = reader.GetString(6),
                        Email = reader.GetString(7),
                        Recording = reader.GetBoolean(8),
                        ZIP = reader.GetString(9),
                        CITY = reader.GetString(11),
                        COUNTY = reader.GetString(12),
                        STATE = reader.GetString(13),
                    };
                    customers.Add(customer);
                }
                reader.Close();
            }
            con.Close();
        }
        //s.Stop();
        //throw new Exception("Time to Fetch " + customers.Count + " records = " + s.Elapsed);

        return customers;
    }

我已经尽力了,有人可以引导我加快速度吗?过去几周,我一直在努力改善这一点

0 个答案:

没有答案