我有一个如下数据库表:
create table temperature
(id int unsigned not null auto_increment primary key,
temperature double
);
Run Code Online (Sandbox Code Playgroud)
在我的程序中,我有大约2000万的温度插入表中.我在.Net环境中工作,使用Connector/Net连接到MySql.代码如下:
List<double> temps = new List<double>();
...
string connStr = "server=localhost;user=name;database=test;port=3306;password=*****;";
MySqlConnection conn = new MySqlConnection(connStr);
try
{
conn.Open();
//temps.Count is about 20 million
for (int i = 0; i < temps.Count; i++)
{
string sql1 = "INSERT INTO temperature VALUES (null, "+temps[i]+")";
MySqlCommand cmd1 = new MySqlCommand(sql1, conn);
cmd1.ExecuteNonQuery();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
conn.Close();
Run Code Online (Sandbox Code Playgroud)
如何尽可能快地插入如此多的行数据?(它只能在我的计算机上每分钟插入2000条记录.)