# 🚀 腾讯云数据库优化历史功能部署说明 ## 📋 概述 本文档提供了在腾讯云数据库上部署优化历史功能的完整指南,包括数据库表创建、配置验证和功能测试。 ## 🛠️ 部署脚本说明 ### 1. 脚本文件列表 | 文件名 | 功能描述 | 使用场景 | |--------|----------|----------| | `check_tencent_db.py` | 数据库连接检查 | 部署前验证 | | `deploy_tencent_simple.py` | 简化部署脚本 | 快速部署 | | `deploy_history_to_tencent.py` | 完整部署脚本 | 详细部署 | | `quick_deploy_history.sh` | 一键部署脚本 | 自动化部署 | ### 2. 部署流程 #### 2.1 预检查(推荐) ```bash # 检查数据库连接和权限 python3 check_tencent_db.py ``` #### 2.2 快速部署 ```bash # 方式1: 直接运行Python脚本 python3 deploy_tencent_simple.py # 方式2: 使用一键部署脚本 ./quick_deploy_history.sh ``` #### 2.3 完整部署 ```bash # 使用完整部署脚本(包含触发器、存储过程等) python3 deploy_history_to_tencent.py ``` ## 🔧 配置说明 ### 1. 数据库配置 所有脚本使用以下腾讯云数据库配置: ```python DB_CONFIG = { 'host': 'gz-cynosdbmysql-grp-d26pzce5.sql.tencentcdb.com', 'port': 24936, 'user': 'root', 'password': '!Rjb12191', 'database': 'pro_db', 'charset': 'utf8mb4' } ``` ### 2. 创建的表结构 #### 2.1 历史记录表 (prompt_history) ```sql CREATE TABLE prompt_history ( id INT PRIMARY KEY AUTO_INCREMENT, user_id INT NOT NULL, wx_user_id INT, original_input TEXT NOT NULL, generated_prompt TEXT NOT NULL, template_id INT, template_name VARCHAR(100), generation_time INT, satisfaction_rating TINYINT, tags JSON, is_favorite BOOLEAN DEFAULT FALSE, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ); ``` #### 2.2 标签表 (history_tags) ```sql CREATE TABLE history_tags ( id INT PRIMARY KEY AUTO_INCREMENT, history_id INT NOT NULL, tag_name VARCHAR(50) NOT NULL, tag_type VARCHAR(20) DEFAULT 'custom', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); ``` #### 2.3 统计表 (user_statistics) ```sql CREATE TABLE user_statistics ( id INT PRIMARY KEY AUTO_INCREMENT, user_id INT UNIQUE NOT NULL, total_generations INT DEFAULT 0, favorite_count INT DEFAULT 0, avg_rating DECIMAL(3,2) DEFAULT 0.00, last_generation_at TIMESTAMP NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ); ``` ## 🚀 快速开始 ### 1. 环境准备 ```bash # 确保Python3已安装 python3 --version # 安装依赖 pip install pymysql # 给脚本添加执行权限 chmod +x *.py *.sh ``` ### 2. 执行部署 ```bash # 步骤1: 检查数据库连接 python3 check_tencent_db.py # 步骤2: 执行部署 python3 deploy_tencent_simple.py # 步骤3: 重启应用 pkill -f gunicorn gunicorn -c gunicorn.conf.py src.flask_prompt_master:app # 步骤4: 测试功能 python3 test_history_feature.py ``` ### 3. 验证部署 ```bash # 访问历史页面 curl http://localhost:5002/history # 测试API接口 curl http://localhost:5002/api/history ``` ## 📊 部署结果验证 ### 1. 数据库验证 ```sql -- 检查表是否创建 SHOW TABLES LIKE '%history%'; -- 查看表结构 DESCRIBE prompt_history; DESCRIBE history_tags; DESCRIBE user_statistics; -- 检查示例数据 SELECT COUNT(*) FROM prompt_history; SELECT * FROM prompt_history LIMIT 5; ``` ### 2. 应用验证 ```bash # 检查应用日志 tail -f logs/app.log # 测试API响应 curl -X GET "http://localhost:5002/api/history" -H "Content-Type: application/json" ``` ### 3. 功能验证 ```bash # 运行完整测试 python3 test_history_feature.py # 检查特定功能 curl -X GET "http://localhost:5002/api/history/statistics" ``` ## 🔧 故障排除 ### 1. 常见问题 #### 1.1 连接失败 ``` ❌ 数据库连接失败: (2003, "Can't connect to MySQL server") ``` **解决方案:** - 检查网络连接 - 验证数据库地址和端口 - 确认防火墙设置 #### 1.2 权限不足 ``` ❌ 创建表失败: (1142, "CREATE command denied to user") ``` **解决方案:** - 检查用户权限 - 联系数据库管理员授权 #### 1.3 表已存在 ``` ⚠️ 表 prompt_history 已存在 ``` **解决方案:** - 脚本会自动跳过已存在的表 - 如需重新创建,先删除现有表 ### 2. 日志查看 ```bash # 查看部署日志 python3 deploy_tencent_simple.py 2>&1 | tee deploy.log # 查看应用日志 tail -f logs/app.log | grep -i history ``` ### 3. 回滚操作 ```sql -- 删除历史相关表(谨慎操作) DROP TABLE IF EXISTS history_tags; DROP TABLE IF EXISTS user_statistics; DROP TABLE IF EXISTS prompt_history; ``` ## 📈 性能优化 ### 1. 数据库优化 ```sql -- 添加复合索引 CREATE INDEX idx_user_created_at ON prompt_history(user_id, created_at); CREATE INDEX idx_favorite_rating ON prompt_history(is_favorite, satisfaction_rating); -- 分析表使用情况 ANALYZE TABLE prompt_history; ANALYZE TABLE history_tags; ANALYZE TABLE user_statistics; ``` ### 2. 应用优化 ```python # 在配置中添加连接池设置 SQLALCHEMY_ENGINE_OPTIONS = { 'pool_pre_ping': True, 'pool_recycle': 300, 'pool_size': 10, 'max_overflow': 20 } ``` ## 🔄 维护操作 ### 1. 数据清理 ```sql -- 清理30天前的数据 DELETE FROM prompt_history WHERE created_at < DATE_SUB(NOW(), INTERVAL 30 DAY); -- 更新统计信息 UPDATE user_statistics SET total_generations = (SELECT COUNT(*) FROM prompt_history WHERE user_id = user_statistics.user_id), favorite_count = (SELECT COUNT(*) FROM prompt_history WHERE user_id = user_statistics.user_id AND is_favorite = TRUE); ``` ### 2. 数据备份 ```bash # 备份历史数据 mysqldump -h gz-cynosdbmysql-grp-d26pzce5.sql.tencentcdb.com -P 24936 -u root -p pro_db prompt_history history_tags user_statistics > history_backup.sql ``` ### 3. 监控脚本 ```bash # 创建监控脚本 cat > monitor_history.sh << 'EOF' #!/bin/bash echo "历史记录统计:" mysql -h gz-cynosdbmysql-grp-d26pzce5.sql.tencentcdb.com -P 24936 -u root -p pro_db -e " SELECT COUNT(*) as total_records, COUNT(DISTINCT user_id) as unique_users, AVG(satisfaction_rating) as avg_rating FROM prompt_history; " EOF chmod +x monitor_history.sh ``` ## 🎉 部署完成 部署成功后,您将拥有以下功能: ### ✅ 核心功能 - 📝 历史记录管理 - 🔍 智能搜索筛选 - ⭐ 收藏评分系统 - 📊 统计分析 - 📤 数据导出 ### ✅ 访问地址 - **历史页面**: http://localhost:5002/history - **API接口**: http://localhost:5002/api/history - **统计接口**: http://localhost:5002/api/history/statistics ### ✅ 测试验证 - 运行测试脚本验证功能 - 检查数据库表结构 - 验证API接口响应 --- *部署完成时间:2025年1月* *功能版本:v1.0* *维护人员:系统管理员*