【问题标题】:Increase speed of inserting data in MySQL database with C# [duplicate]使用 C# 提高在 MySQL 数据库中插入数据的速度 [重复]
【发布时间】:2016-01-26 20:08:53
【问题描述】:

所以我有一个包含 3000 万行的 .txt 文件,我用 MD5 和 SHA1 对每个文件进行哈希处理并将其插入到我的 MySQL 数据库中。它工作得很好,但每个哈希需要 1 秒。那是 3000 万秒...

问题:速度不够快,1秒1个太慢了。

我想要什么:某种可以加快速度的线程,因此它可以在 1 秒内完成 20 个! (可能太不现实了)

这是我的代码,随意操作:(我使用 MySql.Data 作为参考)

        bool success;
        int done = 0;
        string command;
        WebClient putHash = new WebClient();

        string server = "my_server";
        string database = "my_db";
        string uid = "my_dbuser";
        string password = "my_pass";
        string connectionstring = "SERVER=" + server + ";DATABASE=" + database + ";UID=" + uid + ";PASSWORD=" + password + ";";

        MySqlConnection cnn = new MySqlConnection(connectionstring);
        MySqlCommand cmd = new MySqlCommand();

        foreach (string line in File.ReadLines(@"directory with .txt file which has 30million lines"))
        {
                string linefixed = line.Replace(" ", "").Replace("'", "").Replace(";", "").Replace(")", "").Replace("\\", "").Replace("=", "");
                success = false;
                byte[] hashedv = new UTF8Encoding().GetBytes(linefixed);
                byte[] hash = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(hashedv);
                string encodedinput = BitConverter.ToString(hash).Replace("-", string.Empty).ToLower();

                byte[] bytes = Encoding.UTF8.GetBytes(linefixed);

                var sha1 = SHA1.Create();
                byte[] hashBytes = sha1.ComputeHash(bytes);

                byte[] hexed = hashBytes;

                var sb = new StringBuilder();
                foreach (byte b in hexed)
                {
                    var hex = b.ToString("x2");
                    sb.Append(hex);
                }
                string sha1done = sb.ToString();

                while (!success)
                {
                    try
                    {
                        command = "INSERT INTO passwdhashes (password,MD5,SHA1) VALUES('" + linefixed + "','" + encodedinput + "','" + sha1done + "')" ;
                        cmd.CommandText = command;
                        cmd.Connection = cnn;
                        cnn.Open();
                        cmd.ExecuteNonQuery();
                        cnn.Close();
                        success = true;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        cnn.Close();
                    }
                }
            done = done + 1;
            Console.WriteLine("\n" + done);
        }
        Console.ReadKey();
    }
}

}

【问题讨论】:

  • 一些改进:在循环外创建一个 MD5 和 SHA1 哈希器实例。在循环外打开/关闭数据库连接一次。收集多行(例如 1000 行)并批量/批量插入它们。

标签: c# mysql


【解决方案1】:

您可以一次插入多行:https://dev.mysql.com/doc/refman/5.5/en/insert.html

类似这样的东西:INSERT INTO passwordHashed (password,MD5,SHA1) VALUES(1,2,3),(4,5,6),(7,8,9);

这是示例/伪代码,可能无法编译:

List<ElementType> elementsToInsert;

foreach (string line in File.ReadLines(@"file")) {

    elementsToInsert.Add(element);
    if (elementsToInsert.size == 100) {
        // execute SQL insertion
        elementsToInsert.clear();
    }
}

ElementType 应该是一个包含用于插入的字段的类。

【讨论】:

  • 但是我必须做几次 foreaches 来替换 4,5,6 和 7,8,9 并确保我不会再次错误地散列同一行......或者是你知道我可以用 C# 实现它吗?
  • @RaghavJ。您创建一个包含要插入的值的数组,并在每 N 个元素处执行插入。注意:1,2,3,4,5,6,7,8,9 只是样本值。
  • 您不需要多个 foreaches,只需将要插入的值保留在列表中,并在列表达到 1000 行 f.e 时进行批量/批量插入。只是不要忘记在 foreach 完成后插入剩余部分 :)
  • 你能操纵我的代码并告诉我你的意思吗?我很难理解...
  • “重复”不起作用,我通过将整个东西放在 for 循环中进行测试。每个条目仍然需要 1 秒。
猜你喜欢
  • 1970-01-01
  • 2019-03-04
  • 2020-11-13
  • 2017-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-04
  • 1970-01-01
相关资源
最近更新 更多