I have a database table like below:
create table temperature
(id int unsigned not null auto_increment primary key,
temperature double
);
And in my program I got about 20 million temperature to insert into the table.
I worke in .Net environment, use Connector/Net connecting to MySql. The code was like below:
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();
How can i insert so many lines data as fast as possible?
(It can only insert 2000 records every minute in my computer.)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…