## D.3 Nacos统一配置中心 - 添加spring-cloud-starter-alibaba-nacos-config依赖 - 创建bootstrap.yaml配置文件 - DynamicConfig: @RefreshScope动态配置刷新示例 ## F.1 ShardingSphere读写分离 - 添加shardingsphere-jdbc-core依赖 - sharding-config.yaml: 读写分离配置示例 - 支持主从切换、负载均衡策略 ## F.3 数据库索引优化 - db-index-optimization.sql: 全库索引优化脚本 - 用户/角色/菜单/部门表索引 - 用款/支出/收款表索引 - 复合索引优化常用查询场景
138 lines
5.6 KiB
SQL
138 lines
5.6 KiB
SQL
-- =====================================================
|
|
-- 资金平台数据库索引优化SQL
|
|
-- 执行说明: 根据实际查询场景选择执行
|
|
-- =====================================================
|
|
|
|
-- 使用fund_sys数据库
|
|
USE fund_sys;
|
|
|
|
-- =====================================================
|
|
-- 用户表索引
|
|
-- =====================================================
|
|
|
|
-- 用户名唯一索引(已存在可跳过)
|
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_sys_user_username ON sys_user(username);
|
|
|
|
-- 租户ID索引(多租户查询)
|
|
CREATE INDEX IF NOT EXISTS idx_sys_user_tenant_id ON sys_user(tenant_id);
|
|
|
|
-- 部门ID索引(按部门查询用户)
|
|
CREATE INDEX IF NOT EXISTS idx_sys_user_dept_id ON sys_user(dept_id);
|
|
|
|
-- 状态索引(查询启用/禁用用户)
|
|
CREATE INDEX IF NOT EXISTS idx_sys_user_status ON sys_user(status);
|
|
|
|
-- 创建时间索引(按时间范围查询)
|
|
CREATE INDEX IF NOT EXISTS idx_sys_user_created_time ON sys_user(created_time);
|
|
|
|
-- 复合索引: 租户+状态(常用查询组合)
|
|
CREATE INDEX IF NOT EXISTS idx_sys_user_tenant_status ON sys_user(tenant_id, status);
|
|
|
|
-- =====================================================
|
|
-- 角色表索引
|
|
-- =====================================================
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_sys_role_tenant_id ON sys_role(tenant_id);
|
|
CREATE INDEX IF NOT EXISTS idx_sys_role_status ON sys_role(status);
|
|
|
|
-- =====================================================
|
|
-- 菜单表索引
|
|
-- =====================================================
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_sys_menu_parent_id ON sys_menu(parent_id);
|
|
CREATE INDEX IF NOT EXISTS idx_sys_menu_type ON sys_menu(type);
|
|
CREATE INDEX IF NOT EXISTS idx_sys_menu_sort ON sys_menu(sort);
|
|
|
|
-- =====================================================
|
|
-- 部门表索引
|
|
-- =====================================================
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_sys_dept_parent_id ON sys_dept(parent_id);
|
|
CREATE INDEX IF NOT EXISTS idx_sys_dept_tenant_id ON sys_dept(tenant_id);
|
|
|
|
-- =====================================================
|
|
-- 用户角色关联表索引
|
|
-- =====================================================
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_sys_user_role_user_id ON sys_user_role(user_id);
|
|
CREATE INDEX IF NOT EXISTS idx_sys_user_role_role_id ON sys_user_role(role_id);
|
|
|
|
-- =====================================================
|
|
-- 角色菜单关联表索引
|
|
-- =====================================================
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_sys_role_menu_role_id ON sys_role_menu(role_id);
|
|
CREATE INDEX IF NOT EXISTS idx_sys_role_menu_menu_id ON sys_role_menu(menu_id);
|
|
|
|
-- =====================================================
|
|
-- 操作日志表索引
|
|
-- =====================================================
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_sys_log_user_id ON sys_log(user_id);
|
|
CREATE INDEX IF NOT EXISTS idx_sys_log_tenant_id ON sys_log(tenant_id);
|
|
CREATE INDEX IF NOT EXISTS idx_sys_log_operation ON sys_log(operation);
|
|
CREATE INDEX IF NOT EXISTS idx_sys_log_created_time ON sys_log(created_time);
|
|
-- 复合索引: 租户+时间(常用日志查询)
|
|
CREATE INDEX IF NOT EXISTS idx_sys_log_tenant_time ON sys_log(tenant_id, created_time);
|
|
|
|
|
|
-- =====================================================
|
|
-- 使用fund_req数据库(用款申请)
|
|
-- =====================================================
|
|
|
|
USE fund_req;
|
|
|
|
-- 用款申请表索引
|
|
CREATE INDEX IF NOT EXISTS idx_fund_request_tenant_id ON fund_request(tenant_id);
|
|
CREATE INDEX IF NOT EXISTS idx_fund_request_user_id ON fund_request(user_id);
|
|
CREATE INDEX IF NOT EXISTS idx_fund_request_status ON fund_request(status);
|
|
CREATE INDEX IF NOT EXISTS idx_fund_request_created_time ON fund_request(created_time);
|
|
CREATE INDEX IF NOT EXISTS idx_fund_request_project_id ON fund_request(project_id);
|
|
-- 复合索引: 租户+状态+时间(审批列表查询)
|
|
CREATE INDEX IF NOT EXISTS idx_fund_request_tenant_status_time ON fund_request(tenant_id, status, created_time);
|
|
|
|
|
|
-- =====================================================
|
|
-- 使用fund_exp数据库(支出管理)
|
|
-- =====================================================
|
|
|
|
USE fund_exp;
|
|
|
|
-- 支出表索引
|
|
CREATE INDEX IF NOT EXISTS idx_fund_expense_tenant_id ON fund_expense(tenant_id);
|
|
CREATE INDEX IF NOT EXISTS idx_fund_expense_request_id ON fund_expense(request_id);
|
|
CREATE INDEX IF NOT EXISTS idx_fund_expense_status ON fund_expense(status);
|
|
CREATE INDEX IF NOT EXISTS idx_fund_expense_created_time ON fund_expense(created_time);
|
|
-- 复合索引: 租户+支付状态
|
|
CREATE INDEX IF NOT EXISTS idx_fund_expense_tenant_status ON fund_expense(tenant_id, status);
|
|
|
|
|
|
-- =====================================================
|
|
-- 使用fund_receipt数据库(收款管理)
|
|
-- =====================================================
|
|
|
|
USE fund_receipt;
|
|
|
|
-- 收款表索引
|
|
CREATE INDEX IF NOT EXISTS idx_fund_receipt_tenant_id ON fund_receipt(tenant_id);
|
|
CREATE INDEX IF NOT EXISTS idx_fund_receipt_project_id ON fund_receipt(project_id);
|
|
CREATE INDEX IF NOT EXISTS idx_fund_receipt_status ON fund_receipt(status);
|
|
CREATE INDEX IF NOT EXISTS idx_fund_receipt_created_time ON fund_receipt(created_time);
|
|
-- 复合索引: 租户+确认状态
|
|
CREATE INDEX IF NOT EXISTS idx_fund_receipt_tenant_status ON fund_receipt(tenant_id, status);
|
|
|
|
|
|
-- =====================================================
|
|
-- 分析慢查询(可选)
|
|
-- =====================================================
|
|
|
|
-- 查看表的索引情况
|
|
-- SHOW INDEX FROM sys_user;
|
|
|
|
-- 分析查询执行计划
|
|
-- EXPLAIN SELECT * FROM sys_user WHERE tenant_id = 1 AND status = 1;
|
|
|
|
-- 查看慢查询日志(需要开启慢查询日志)
|
|
-- SHOW VARIABLES LIKE 'slow_query_log';
|
|
-- SHOW VARIABLES LIKE 'long_query_time';
|