This commit is contained in:
HuangHai
2026-02-06 08:21:47 +08:00
parent b943cb6b64
commit 1f4d29d5a1
4 changed files with 109 additions and 84 deletions

View File

@@ -59,12 +59,24 @@ def load_csv(jdbc_url, user, password, table, csv_dir, columns=None):
# Optimization settings
print("Setting session parameters for speed...")
cursor.execute("SET NAMES utf8mb4")
try:
cursor.execute("SET FOREIGN_KEY_CHECKS = 0")
cursor.execute("SET UNIQUE_CHECKS = 0")
cursor.execute("SET SQL_LOG_BIN = 0")
except Exception as e:
print(f"Warning: Could not set some optimization flags: {e}")
# 逐个尝试设置优化参数,避免因单个参数(如 SQL_LOG_BIN)权限不足导致整体失败
opts = [
("SET FOREIGN_KEY_CHECKS = 0", "Foreign Key Checks Disabled"),
("SET UNIQUE_CHECKS = 0", "Unique Checks Disabled"),
("SET SQL_LOG_BIN = 0", "Binary Logging Disabled (Requires SUPER privilege)")
]
for sql, desc in opts:
try:
cursor.execute(sql)
print(f" - {desc}: Success")
except Exception as e:
# 如果是权限问题 (1227),打印更友好的信息
if "1227" in str(e):
print(f" - {desc}: Skipped (Insufficient privileges, but that's okay)")
else:
print(f" - {desc}: Failed ({e})")
# Truncate table
print(f"Truncating table {table}...")
@@ -111,6 +123,20 @@ def load_csv(jdbc_url, user, password, table, csv_dir, columns=None):
total_rows += rows
print(f" -> Loaded {rows} rows")
# 显示 MySQL 警告(SHOW WARNINGS)的功能,用于排查导入差异
try:
cursor.execute("SHOW WARNINGS")
warnings = cursor.fetchall()
if warnings:
print(f" - MySQL Warnings ({len(warnings)}):")
# 最多显示前 5 条警告,避免日志过多
for i, warn in enumerate(warnings[:5]):
print(f" - {warn.get('Level', 'Warning')}: {warn.get('Message', 'Unknown error')}")
if len(warnings) > 5:
print(f" - ... and {len(warnings) - 5} more warnings")
except Exception as warn_e:
print(f" - Could not fetch warnings: {warn_e}")
conn.commit()
duration = time.time() - start_time