optimize.sql 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. -- ============================================================
  2. -- project-load 云函数性能优化索引
  3. -- 执行前请先备份数据库!
  4. -- ============================================================
  5. -- 1. Project 表索引 - 核心查询优化
  6. -- 用于按公司+状态+时间范围查询
  7. CREATE INDEX IF NOT EXISTS "idx_project_company_status_deleted"
  8. ON "Project" ("company", "status", "isDeleted")
  9. WHERE "isDeleted" IS NOT TRUE AND "status" != '已完成';
  10. -- 用于时间范围查询(deadline 和 createdAt)
  11. CREATE INDEX IF NOT EXISTS "idx_project_company_deadline"
  12. ON "Project" ("company", "deadline")
  13. WHERE "isDeleted" IS NOT TRUE;
  14. CREATE INDEX IF NOT EXISTS "idx_project_company_created"
  15. ON "Project" ("company", "createdAt")
  16. WHERE "isDeleted" IS NOT TRUE;
  17. -- 复合索引:公司 + 更新时间(用于排序)
  18. CREATE INDEX IF NOT EXISTS "idx_project_company_updated"
  19. ON "Project" ("company", "updatedAt" DESC)
  20. WHERE "isDeleted" IS NOT TRUE;
  21. -- 2. Profile 表索引 - 工作负载查询优化
  22. CREATE INDEX IF NOT EXISTS "idx_profile_company_role"
  23. ON "Profile" ("company", "roleName")
  24. WHERE "isDeleted" IS NOT TRUE;
  25. -- 3. ProjectTeam 表索引 - 团队成员关联查询
  26. CREATE INDEX IF NOT EXISTS "idx_projectteam_project"
  27. ON "ProjectTeam" ("project")
  28. WHERE "isDeleted" IS NOT TRUE;
  29. CREATE INDEX IF NOT EXISTS "idx_projectteam_profile"
  30. ON "ProjectTeam" ("profile")
  31. WHERE "isDeleted" IS NOT TRUE;
  32. -- 4. Product 表索引 - 空间查询优化
  33. CREATE INDEX IF NOT EXISTS "idx_product_project"
  34. ON "Product" ("project")
  35. WHERE ("isDeleted" IS NULL OR "isDeleted" = false);
  36. -- 5. ProjectFile 表索引 - 交付物查询优化
  37. CREATE INDEX IF NOT EXISTS "idx_projectfile_project_type"
  38. ON "ProjectFile" ("project", "fileType")
  39. WHERE ("isDeleted" IS NULL OR "isDeleted" = false)
  40. AND ("fileType" LIKE 'delivery_%' OR "data"->>'uploadStage' = 'delivery');
  41. -- 用于 spaceId/productId 查找
  42. CREATE INDEX IF NOT EXISTS "idx_projectfile_spaceid"
  43. ON "ProjectFile" (("data"->>'spaceId'))
  44. WHERE ("isDeleted" IS NULL OR "isDeleted" = false);
  45. -- 6. ProjectIssue 表索引
  46. CREATE INDEX IF NOT EXISTS "idx_issue_project_status"
  47. ON "ProjectIssue" ("project", "status")
  48. WHERE ("isDeleted" IS NULL OR "isDeleted" = false)
  49. AND "status" IN ('待处理', '处理中');
  50. -- ============================================================
  51. -- 分析表统计信息(执行后让查询优化器更好地选择索引)
  52. -- ============================================================
  53. ANALYZE "Project";
  54. ANALYZE "Profile";
  55. ANALYZE "ProjectTeam";
  56. ANALYZE "Product";
  57. ANALYZE "ProjectFile";
  58. ANALYZE "ProjectIssue";
  59. -- ============================================================
  60. -- 查看索引使用情况(诊断用)
  61. -- ============================================================
  62. -- SELECT
  63. -- schemaname, tablename, indexname,
  64. -- idx_scan as index_scans,
  65. -- idx_tup_read as tuples_read,
  66. -- idx_tup_fetch as tuples_fetched
  67. -- FROM pg_stat_user_indexes
  68. -- WHERE tablename IN ('Project', 'Profile', 'ProjectTeam', 'Product', 'ProjectFile', 'ProjectIssue')
  69. -- ORDER BY tablename, idx_scan DESC;