0

编辑:迭代 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;
    }

我已经尽力了,谁能指导我加快速度?从过去几周开始,我一直在努力改善这一点

4

0 回答 0