【发布时间】:2021-08-23 16:27:16
【问题描述】:
我是 MySQL 的新手。我正在使用 Python Connect 将超过 350,000 行插入到在 MySQL 8 数据库上运行的表中。我的 Python 代码如下所示。
cursor = cnx.cursor(buffered=True)
stmt = "INSERT INTO ......"
cursor.executemany(stmt, data)
cnx.commit()
cursor.close()
返回以下错误:
[错误] [MY-010934] [服务器] 内存不足;检查mysqld或一些 其他进程使用所有可用内存;如果没有,您可能必须使用 'ulimit' 允许 mysqld 使用更多内存,或者您可以添加更多交换 空间
如果我减少插入的行,例如只插入 200,000 行,错误就会消失。我认为 MySQL 设置必须有一些大小限制,但我不知道要更改哪一个。正如许多答案所说,我尝试手动将innodb_buffer_pool_size 增加到 500MB,但错误仍在继续。我该怎么办?我打印了关于大小的系统变量,它在下面列出。
| binlog_cache_size | 32768 |
| binlog_row_event_max_size | 8192 |
| binlog_stmt_cache_size | 32768 |
| binlog_transaction_dependency_history_size | 25000 |
| bulk_insert_buffer_size | 8388608 |
| delayed_queue_size | 1000 |
| histogram_generation_max_mem_size | 20000000 |
| host_cache_size | 279 |
| innodb_buffer_pool_chunk_size | 134217728 |
| innodb_buffer_pool_size | 134217728 |
| innodb_change_buffer_max_size | 25 |
| innodb_doublewrite_batch_size | 0 |
| innodb_ft_cache_size | 8000000 |
| innodb_ft_max_token_size | 84 |
| innodb_ft_min_token_size | 3 |
| innodb_ft_total_cache_size | 640000000 |
| innodb_log_buffer_size | 16777216 |
| innodb_log_file_size | 50331648 |
| innodb_log_write_ahead_size | 8192 |
| innodb_max_undo_log_size | 1073741824 |
| innodb_online_alter_log_max_size | 134217728 |
| innodb_page_size | 16384 |
| innodb_purge_batch_size | 300 |
| innodb_sort_buffer_size | 1048576 |
| innodb_sync_array_size | 1 |
| join_buffer_size | 262144 |
| key_buffer_size | 8388608 |
| key_cache_block_size | 1024 |
| large_page_size | 0 |
| max_binlog_cache_size | 18446744073709547520 |
| max_binlog_size | 1073741824 |
| max_binlog_stmt_cache_size | 18446744073709547520 |
| max_heap_table_size | 16777216 |
| max_join_size | 18446744073709551615 |
| max_relay_log_size | 0 |
| myisam_data_pointer_size | 6 |
| myisam_max_sort_file_size | 9223372036853727232 |
| myisam_mmap_size | 18446744073709551615 |
| myisam_sort_buffer_size | 8388608 |
| ngram_token_size | 2 |
| optimizer_trace_max_mem_size | 1048576 |
| parser_max_mem_size | 18446744073709551615 |
| performance_schema_accounts_size | -1 |
| performance_schema_digests_size | 10000 |
| performance_schema_error_size | 4890 |
| performance_schema_events_stages_history_long_size | 10000 |
| performance_schema_events_stages_history_size | 10 |
| performance_schema_events_statements_history_long_size | 10000 |
| performance_schema_events_statements_history_size | 10 |
| performance_schema_events_transactions_history_long_size | 10000 |
| performance_schema_events_transactions_history_size | 10 |
| performance_schema_events_waits_history_long_size | 10000 |
| performance_schema_events_waits_history_size | 10 |
| performance_schema_hosts_size | -1 |
| performance_schema_session_connect_attrs_size | 512 |
| performance_schema_setup_actors_size | -1 |
| performance_schema_setup_objects_size | -1 |
| performance_schema_users_size | -1 |
| preload_buffer_size | 32768 |
| profiling_history_size | 15 |
| query_alloc_block_size | 8192 |
| query_prealloc_size | 8192 |
| range_alloc_block_size | 4096 |
| range_optimizer_max_mem_size | 8388608 |
| read_buffer_size | 131072 |
| read_rnd_buffer_size | 262144 |
| rpl_read_size | 8192 |
| select_into_buffer_size | 131072 |
| slave_pending_jobs_size_max | 134217728 |
| sort_buffer_size | 262144 |
| thread_cache_size | 9 |
| tmp_table_size | 16777216 |
| transaction_alloc_block_size | 8192 |
| transaction_prealloc_size | 4096 |
【问题讨论】:
-
您不能将数据拆分为 5,000 行的块并进行 7 次插入而不是 1 次插入,是否有充分的理由?
-
我想确保一个组中的所有数据(超过 300,000 行)都已插入,或者它们都不应该插入。另一方面,我认为我可以通过解决问题而不是解决问题来学习一些东西。
-
@Yuan 这就是交易的用途。启动一个事务,只有在一切成功时才提交,否则回滚。
-
@MarkRotteveel 非常感谢。我检查了交易示例。可以完美解决我的问题。只是为了学习,我仍然希望任何 MySQL 资深人士能给我一些关于调整 MySQL 大小限制变量的提示,以便我可以一次插入大行(超过 350,000 行)。我尝试增加一些大小变量,但它不起作用。如果以后没有人可以回答,我将关闭这个问题。再次感谢。