MySQL插入/更新记录非常慢

时间:2014-02-04 02:52:58

标签: c# mysql performance

我发现mysql更新记录非常慢。

我有一个逻辑,如果记录存在,那么它将更新,否则它将插入。 但是源数据库的总记录有14k行,并且它只在几秒内成功插入每一行,因此,它非常慢,我怎么能在1分钟内插入14k行? 顺便说一句,我正在使用c#

foreach (row in rows)
    checkrecords(row); // if exist update, else create.      

此致 MH

3 个答案:

答案 0 :(得分:0)

如果存在可能是问题的根源。 确保在检查时是否使用唯一索引记录是否存在。

答案 1 :(得分:0)

尝试设置值:

innodb_flush_log_at_trx_commit=2
innodb_flush_method=O_DIRECT (for non-windows machine)
innodb_buffer_pool_size=25GB (currently it is close to 21GB)
innodb_doublewrite=0
innodb_support_xa=0
innodb_thread_concurrency=0...1000 (try different values, beginning with 200)

参考文献:

MySQL docs用于描述不同的变量。

MySQL Server Setting Tuning

MySQL Performance Optimization basics

希望它有所帮助...

请参阅Why is MySQL InnoDB insert so slow?

Source

答案 2 :(得分:0)

重组查询并确保索引设置正确。

将要更新的行加载到临时表中。然后插入/更新为:

insert into t(. . .)
    select . . .
    from temptable tt
    on duplicate key update col1 = values(col1), . . .;

确保将检查是否存在记录的条件合并为唯一索引。因此,如果您要查找三列的组合,请确保在t(col1, col2, col3)上有唯一索引。

相关问题