为了管理C#数据库,我使用了一个mySQL应用程序。
我想做的是:
为了计算n个“东西”,我必须已经计算了(n-1)-th“东西”。
我就是这样做的:
声明:
static MySqlCommand cmd;
static MySqlDataReader dr;我的主循环如下:
for (...)
{
dr.Close();
cmd.CommandText = "insert into....";
dr = cmd.ExecuteReader();
}这要花很长时间。要插入的行总数约为2.5M。
当我在普通服务器中使用mySql数据库时,大约需要100到150个小时.当我使用本地主机数据库时,大约需要50h。
我想应该有更快的办法。我的想法:
CommandText。这个是可能的吗?发布于 2014-01-02 17:20:10
发布于 2014-01-02 18:04:39
您可能正在使用InnoDB作为访问方法。在这种情况下,您应该尝试在事务中每百行左右包装一次INSERT操作。当我不得不处理这种应用程序时,它就产生了巨大的变化。要做到这一点,请按如下方式构造代码:
MySqlCommand commit;
start.CommandText = "START TRANSACTION";
MySqlCommand commit;
commit.CommandText = "COMMIT";
int bunchSize = 100;
int bunch = 0;
start.ExecuteNonQuery(); /* start the first bunch transaction */
bunch = bunchsize;
for(/*whatever loop conditions you need*/) {
/* whatever you need to do */
/* your insert statement */
if (--bunchsize <= 0) {
commit.ExecuteNonQuery(); /* end one bunch transaction */
start.ExecuteNonQuery(); /* and begin the next */
bunchsize = bunch;
}
}
commit.ExecuteNonQuery(); /* end the last bunch transaction */您要向其中插入megarows的表也可能包含许多索引。在本例中,您可以通过开始使用以下系列INSERT来加快速度
SET unique_checks=0;
SET foreign_key_checks=0;
ALTER TABLE tbl_name DISABLE KEYS;然后用这个序列结束。
ALTER TABLE tbl_name ENABLE KEYS;
SET unique_checks=1;
SET foreign_key_checks=1;在您的软件中,您必须非常小心,以避免插入在使用此技术时会被拒绝为副本的行,因为在这种情况下,启用键操作将无法工作。
有关更多信息,请阅读以下内容:http://dev.mysql.com/doc/refman/5.5/en/optimizing-innodb-bulk-data-loading.html
https://stackoverflow.com/questions/20888020
复制相似问题