继续优化数据库

This commit is contained in:
2026-07-30 10:59:44 +08:00
parent 4e20d7edac
commit 08dec03371
7 changed files with 721 additions and 108 deletions

View File

@@ -275,3 +275,133 @@ def init_database_tables():
except Exception as e:
log_error(f"[数据库] 初始化失败: {e}", 'db_init', exc_info=True)
return False
def optimize_sum_data_indexes():
"""优化求和数据表的索引,提升查询性能"""
try:
log_info("[数据库] 开始优化 t_daily_report_sum_data 表索引", 'db_init')
# 检查倒排索引是否存在
def _index_exists(index_name):
try:
result = execute_query("SHOW INDEX FROM t_daily_report_sum_data")
for row in (result or []):
key_name = row.get('Key_name', '') or row.get('Index_name', '')
if key_name == index_name:
return True
except Exception:
pass
return False
# 添加倒排索引以加速查询
# report_date 索引 - 日期范围查询
try:
if not _index_exists('idx_report_date'):
execute_update("""
ALTER TABLE t_daily_report_sum_data
ADD INDEX idx_report_date (report_date)
USING INVERTED PROPERTIES("parser" = "none")
COMMENT '报表日期索引'
""")
log_info("[数据库] 添加 report_date 倒排索引", 'db_init')
else:
log_info("[数据库] report_date 倒排索引已存在", 'db_init')
except Exception as e:
log_warning(f"[数据库] 添加 report_date 索引失败: {e}", 'db_init')
# config_id 索引 - 配置查询
try:
if not _index_exists('idx_config_id'):
execute_update("""
ALTER TABLE t_daily_report_sum_data
ADD INDEX idx_config_id (config_id)
USING INVERTED PROPERTIES("parser" = "none")
COMMENT '配置ID索引'
""")
log_info("[数据库] 添加 config_id 倒排索引", 'db_init')
else:
log_info("[数据库] config_id 倒排索引已存在", 'db_init')
except Exception as e:
log_warning(f"[数据库] 添加 config_id 索引失败: {e}", 'db_init')
# data_source 索引 - 数据来源查询
try:
if not _index_exists('idx_data_source'):
execute_update("""
ALTER TABLE t_daily_report_sum_data
ADD INDEX idx_data_source (data_source)
USING INVERTED PROPERTIES("parser" = "none")
COMMENT '数据来源索引'
""")
log_info("[数据库] 添加 data_source 倒排索引", 'db_init')
else:
log_info("[数据库] data_source 倒排索引已存在", 'db_init')
except Exception as e:
log_warning(f"[数据库] 添加 data_source 索引失败: {e}", 'db_init')
# sum_field_key 索引 - 字段查询
try:
if not _index_exists('idx_sum_field_key'):
execute_update("""
ALTER TABLE t_daily_report_sum_data
ADD INDEX idx_sum_field_key (sum_field_key)
USING INVERTED PROPERTIES("parser" = "none")
COMMENT '求和字段索引'
""")
log_info("[数据库] 添加 sum_field_key 倒排索引", 'db_init')
else:
log_info("[数据库] sum_field_key 倒排索引已存在", 'db_init')
except Exception as e:
log_warning(f"[数据库] 添加 sum_field_key 索引失败: {e}", 'db_init')
# config_name 索引 - 配置名称模糊搜索
try:
if not _index_exists('idx_config_name'):
execute_update("""
ALTER TABLE t_daily_report_sum_data
ADD INDEX idx_config_name (config_name)
USING INVERTED PROPERTIES("parser" = "none")
COMMENT '配置名称索引'
""")
log_info("[数据库] 添加 config_name 倒排索引", 'db_init')
else:
log_info("[数据库] config_name 倒排索引已存在", 'db_init')
except Exception as e:
log_warning(f"[数据库] 添加 config_name 索引失败: {e}", 'db_init')
# split_type 索引 - 拆分类型查询
try:
if not _index_exists('idx_split_type'):
execute_update("""
ALTER TABLE t_daily_report_sum_data
ADD INDEX idx_split_type (split_type)
USING INVERTED PROPERTIES("parser" = "none")
COMMENT '拆分类型索引'
""")
log_info("[数据库] 添加 split_type 倒排索引", 'db_init')
else:
log_info("[数据库] split_type 倒排索引已存在", 'db_init')
except Exception as e:
log_warning(f"[数据库] 添加 split_type 索引失败: {e}", 'db_init')
# split_value 索引 - 拆分值查询
try:
if not _index_exists('idx_split_value'):
execute_update("""
ALTER TABLE t_daily_report_sum_data
ADD INDEX idx_split_value (split_value)
USING INVERTED PROPERTIES("parser" = "none")
COMMENT '拆分值索引'
""")
log_info("[数据库] 添加 split_value 倒排索引", 'db_init')
else:
log_info("[数据库] split_value 倒排索引已存在", 'db_init')
except Exception as e:
log_warning(f"[数据库] 添加 split_value 索引失败: {e}", 'db_init')
log_info("[数据库] 求和数据表索引优化完成", 'db_init')
return True
except Exception as e:
log_error(f"[数据库] 优化索引失败: {e}", 'db_init', exc_info=True)
return False