【发布时间】: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 行)并批量/批量插入它们。