gpt4 book ai didi

c# - 远程mysql在循环中插入表非常慢

转载 作者:行者123 更新时间:2023-11-29 08:08:50 26 4
gpt4 key购买 nike

我正在尝试循环插入文本文件中的大量项目。该循环显然是一个 while 循环,为文本文件的每一行发送一个 sendtodb 命令。使用本地数据库,速度非常快(190 万个插入需要大约 4 小时),但是当在远程 mysql 服务器上托管每秒 1 个插入时,这将需要几天或几周的时间!有什么想法吗?

    private void sendtodb(string image, string title, string content, int cat)
{
string cs = @"the connection string";
MySqlConnection conn = null;
conn = new MySqlConnection(cs);
conn.Open();
try
{

MySqlCommand cmd = new MySqlCommand("", conn);

cmd.CommandText = "INSERT INTO wp_posts (post_author, post_date, post_date_gmt, post_content, post_title, post_excerpt,post_status,comment_status,ping_status,post_password,post_name,to_ping,pinged,post_modified,post_modified_gmt,post_content_filtered,post_parent, guid,menu_order,post_type,post_mime_type,comment_count) "
+ "VALUES (@post_author, @post_date, @post_date_gmt, @post_content, @post_title, @post_excerpt,@post_status,@comment_status,@ping_status,@post_password,@post_name,@to_ping,@pinged,@post_modified,@post_modified_gmt,@post_content_filtered,@post_parent, @guid, @menu_order, @post_type, @post_mime_type, @comment_count)";

// cmd.Parameters.Add("?ID", MySqlDbType.VarChar).Value = "1";
cmd.Parameters.Add("@post_author", MySqlDbType.Int32).Value = 1;
cmd.Parameters.Add("@post_date", MySqlDbType.DateTime).Value = DateTime.Now;
cmd.Parameters.Add("@post_date_gmt", MySqlDbType.DateTime).Value = DateTime.Now;
cmd.Parameters.Add("@post_content", MySqlDbType.Text).Value = content;
cmd.Parameters.Add("@post_title", MySqlDbType.Text).Value = title;
cmd.Parameters.Add("@post_excerpt", MySqlDbType.VarChar).Value = "";
cmd.Parameters.Add("@post_status", MySqlDbType.VarChar).Value = "publish";
cmd.Parameters.Add("@comment_status", MySqlDbType.VarChar).Value = "open";
cmd.Parameters.Add("@ping_status", MySqlDbType.VarChar).Value = "open";
cmd.Parameters.Add("@post_password", MySqlDbType.VarChar).Value = "";
cmd.Parameters.Add("@post_name", MySqlDbType.VarChar).Value = "";

cmd.Parameters.Add("@to_ping", MySqlDbType.Text).Value = "";
cmd.Parameters.Add("@pinged", MySqlDbType.Text).Value = "";

cmd.Parameters.Add("@post_modified", MySqlDbType.DateTime).Value = DateTime.Now;
cmd.Parameters.Add("@post_modified_gmt", MySqlDbType.DateTime).Value = DateTime.Now;
cmd.Parameters.Add("@post_content_filtered", MySqlDbType.LongText).Value = "";
cmd.Parameters.Add("@post_parent", MySqlDbType.Int32).Value = 0;
cmd.Parameters.Add("@guid", MySqlDbType.VarChar).Value = "";

cmd.Parameters.Add("@menu_order", MySqlDbType.Int32).Value = 0;
cmd.Parameters.Add("@post_type", MySqlDbType.VarChar).Value = "post";
cmd.Parameters.Add("@post_mime_type", MySqlDbType.VarChar).Value = "";
cmd.Parameters.Add("@comment_count", MySqlDbType.Int32).Value = 0;
cmd.ExecuteNonQuery();

cmd.CommandText = "select * from wp_posts order by ID desc LIMIT 1";
MySqlDataReader dataReader = cmd.ExecuteReader();
int idcolumn = 0;
while (dataReader.Read())
{
idcolumn = Convert.ToInt32(dataReader["ID"]);
}
dataReader.Close();

cmd.CommandText = "INSERT INTO wp_postmeta (post_id,meta_key,meta_value)" + "VALUES (@post_id,@meta_key,@meta_value)";

cmd.Parameters.Add("@post_id", MySqlDbType.Int32).Value = idcolumn;
cmd.Parameters.Add("@meta_key", MySqlDbType.VarChar).Value = "Image";
cmd.Parameters.Add("@meta_value", MySqlDbType.LongText).Value = image;
cmd.ExecuteNonQuery();

cmd.CommandText = "INSERT INTO wp_term_relationships (object_id,term_taxonomy_id,term_order)" + "VALUES (@object_id,@term_taxonomy_id,@term_order)";

cmd.Parameters.Add("@object_id", MySqlDbType.Int32).Value = idcolumn;
cmd.Parameters.Add("@term_taxonomy_id", MySqlDbType.Int32).Value = cat;
cmd.Parameters.Add("@term_order", MySqlDbType.Int32).Value = 0;
cmd.ExecuteNonQuery();


textBox4.Text = idcolumn.ToString();

textBox2.Text = "MySQL version : " + conn.ServerVersion;

}
catch (MySqlException ex)
{
textBox2.Text = ex.ToString();

}
finally
{
if (conn != null)
{
conn.Close();
}
}
}

这是一个庞大的 WordPress 数据库

最佳答案

您应该在代码之外连接到数据库。现在您正在执行每个插入一个连接,这是极其低效的。

步骤应该是:

connection to database
while(have data) {
use connection to insert data
}
disconnect from database

关于c# - 远程mysql在循环中插入表非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22102482/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com