mysql engine csv_MySQL table engine

本文详细介绍了MySQL5.7中默认的InnoDB存储引擎,对比MyISAM引擎,阐述了InnoDB的特点如事务安全性、一致性读取、外键约束等,并说明了如何选择合适的存储引擎。

5.7 默认使用的是 InnoDB engine

常见的 engine 类型, MyISAM 与 InnoDB 的区别

大部分情况下,采用默认的 InnoDB 即可

InnoDB: The default storage engine in MySQL 5.7. InnoDB is a transaction-safe (ACID compliant) storage engine for MySQL that has commit, rollback, and crash-recovery capabilities to protect user data. InnoDB row-level locking (without escalation to coarser granularity locks) and Oracle-style consistent nonlocking reads increase multi-user concurrency and performance. InnoDB stores user data in clustered indexes to reduce I/O for common queries based on primary keys. To maintain data integrity, InnoDB also supports FOREIGN KEY referential-integrity constraints.

除非数据量大到需要放弃事务的时候,再考虑 MyISAM 吧

MyISAM is slightly faster than InnoDB, and implements the FULLTEXT index which is quite useful for integrating search capabilities. MyISAM is not transacted and doesn't implement foreign key constraints, which is a major drawback.

除了 MySQL table 可以指定 engine 之外,database 是否也可以指定 engine

database 没有 engine 的概念,只有 table 有。

所以,同一个 database, 不同的表可以采用不同的 engine。

You are not restricted to using the same storage engine for an entire server or schema. You can specify the storage engine for any table. For example, an application might use mostly InnoDB tables, with one CSV table for exporting data to a spreadsheet and a few MEMORY tables for temporary workspaces.

如何查看表的 engine 类型

SHOW TABLE STATUS\G;

例如,wordpress 的各数据表 engine 类型都是 InnoDB

90aed70b9e9774741b226dc183fe30b0.png

参考

import pymysql import pandas as pd import os import csv from sqlalchemy import create_engine, text from sqlalchemy.exc import SQLAlchemyError def import_csv_to_mysql(config): """ 将CSV文件数据导入MySQL数据库 参数: config (dict): 包含数据库连接和文件信息的配置字典 返回: bool: 导入成功返回True,失败返回False """ try: # 检查CSV文件是否存在 if not os.path.exists(config['csv_path']): raise FileNotFoundError(f"CSV文件不存在: {config['csv_path']}") # 创建数据库连接 connection_str = f"mysql+pymysql://{config['user']}:{config['password']}@{config['host']}/{config['database']}" engine = create_engine(connection_str, echo=False) # 获取目标表结构 with engine.connect() as conn: result = conn.execute(text(f"DESCRIBE `{config['table']}`")) table_columns = [row[0] for row in result] print(f"目标表 '{config['table']}' 的列: {table_columns}") # 读取CSV文件 with open(config['csv_path'], 'r', encoding=config.get('encoding', 'utf-8')) as f: csv_reader = csv.reader(f, delimiter=config.get('delimiter', ',')) headers = next(csv_reader) # 列映射检查 missing_columns = set(table_columns) - set(headers) if missing_columns: print(f"⚠️ 警告: CSV中缺少表列: {missing_columns}") column_mapping = {header: header for header in headers if header in table_columns} print(f"列映射: {column_mapping}") data_rows = [] for row in csv_reader: # 跳过空行 if not any(row): continue # 创建映射行 mapped_row = {} for csv_col, value in zip(headers, row): if csv_col in column_mapping: db_col = column_mapping[csv_col] mapped_row[db_col] = value data_rows.append(mapped_row) # 检查是否有有效数据 if not data_rows: print("❌ CSV文件中未找到有效数据") return False # 分批导入参数 chunk_size = 1000 total_rows = len(data_rows) success_count = 0 # 创建临时表 temp_table = f"temp_{config['table'].lower()}_{os.getpid()}" with engine.begin() as conn: # 创建临时表 conn.execute(text(f"CREATE TEMPORARY TABLE `{temp_table}` LIKE `{config['table']}`")) # 分批处理数据 for i in range(0, total_rows, chunk_size): # 获取当前批次数据 chunk = data_rows[i:i + chunk_size] # 转换为DataFrame并处理空值 df_chunk = pd.DataFrame(chunk) df_chunk = df_chunk.where(pd.notnull(df_chunk), None) try: # 导入到临时表 df_chunk.to_sql( name=temp_table, con=conn, if_exists='append', index=False ) success_count += len(chunk) print(f"✅ 已插入 {success_count}/{total_rows} 条记录") except Exception as e: print(f"❌ 批量插入失败: {e}") print(f"问题数据示例: {chunk[0] if chunk else '无数据'}") return False # 从临时表导入目标表 try: cols = ', '.join([f'`{col}`' for col in column_mapping.values()]) insert_sql = f""" INSERT INTO `{config['table']}` ({cols}) SELECT {cols} FROM `{temp_table}` """ conn.execute(text(insert_sql)) print(f"🎉 成功导入 {success_count} 条记录到表 '{config['table']}'") # 清理临时表 conn.execute(text(f"DROP TEMPORARY TABLE IF EXISTS `{temp_table}`")) return True except SQLAlchemyError as e: print(f"❌ 最终导入失败: {e}") return False except Exception as e: print(f"❌ 导入过程出错: {e}") return False # 使用示例 if __name__ == "__main__": # 配置参数 IMPORT_CONFIG = { 'host': 'localhost', # 数据库主机 'user': 'root', # 数据库用户名 'password': '123456', # 数据库密码 'database': 'bdpd_rmv', # 数据库名称 'table': 'RMV_KDSQ_MODEL_MODEL_INFO', # 目标表名 'csv_path': r'D:\历史项目留存2\诺褀2025\python加工浦发模型模拟\底层表模拟\RMV_KDSQ_MODEL_MODEL_INFO.csv', # CSV文件路径 'delimiter': ',', # CSV分隔符 'encoding': 'gbk' # 文件编码 } print("=== CSV数据导入MySQL ===") print(f"源文件: {IMPORT_CONFIG['csv_path']}") print(f"目标表: {IMPORT_CONFIG['table']}") # 执行导入 result = import_csv_to_mysql(IMPORT_CONFIG) # 输出结果 if result: print("✅ 数据导入完成") else: print("❌ 数据导入失败") 逐步解析这边的代码我是小白,第一次接触
最新发布
07-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值