我使用Connector/Python将许多行插入mysql中的临时表中。这些行都在一个列表中.我执行这样的插入:
cursor = connection.cursor();
batch = [[1, 'foo', 'bar'],[2, 'xyz', 'baz']]
cursor.executemany('INSERT INTO temp VALUES(?, ?, ?)', batch)
connection.commit()我注意到(当然还有更多行)性能非常差。使用SHOW PROCESSLIST,我注意到每个insert分别执行。但文档https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-executemany.html说,这应该优化为1插入。到底怎么回事?
发布于 2016-12-16 01:15:47
回答,这样其他人就不会经历调试我不得不!
我在代码中使用准备好的语句并使用'?‘对其他查询进行了建模。指示参数。但是对于executemany(),您不能这样做!它必须使用'%s‘。改为:
cursor.executemany('INSERT INTO temp VALUES(%s,%s,%s)', batch)...led以100倍的速度提高和优化的单一查询可以看到使用显示PROCESSLIST。小心标准“?”语法!
发布于 2020-11-11 03:59:26
如果您像使用IGNORE一样使用cursor.executemany('INSERT IGNORE INTO temp VALUES(%s,%s,%s)', batch),那么executemany()的作用就像多个执行!
发布于 2020-06-13 01:37:22
尝试打开以下命令: cursor.fast_executemany = True
否则,executemany的行为就像多次执行一样。
https://stackoverflow.com/questions/41175862
复制相似问题