python - 光标插入返回内存不足错误
问题描述
我是 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 |
解决方案
减少 innodb_buffer_pool_size
而不是增加它可以解决内存过度使用。
我的机器硬件有 1.7 G 内存,MySQL 在启动时使用大约 23% 的内存。
设置innodb_buffer_pool_size = 100M
时,使用 插入40万行数据后Python MySQL Connector
,MySQL内存使用率攀升至30%,即使程序结束也不会下降。
但是,如果我设置innodb_buffer_pool_size = 50M
,当 Python 程序运行时,内存仍然会攀升到 30% 的水平,但如果程序结束,内存很快就会下降。
纯实验发现,或许有资深人士可以解释背后的推理。
推荐阅读
- sql - 如何在 Oracle 中将毫秒转换为时间(hh:mm:ss)
- javascript - 在选项卡中显示 javascript 对象
- javascript - 根据日期和时间标准显示网页
- regex - 匹配单词之间的内容
- rust - 如何将值移出对象安全的特征对象?
- google-apps-script - Google Script Drive.Files.remove(idToDELETE) 权限不足
- javascript - 通过 GET 请求获取 YouTube 播放列表的所有视频 ID
- java - 切换活动时冻结动画
- java - 如何在 Java / Kotlin 中创建返回复杂类型的 Spark UDF?
- kubernetes - 如何在挂载到多个 Kubernetes pod 时写入 gcePersistentDisk