universal-dev-standards 5.0.0-rc.8 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/uds.js +5 -3
- package/bundled/ai/standards/acceptance-criteria-traceability.ai.yaml +178 -0
- package/bundled/ai/standards/agent-dispatch.ai.yaml +69 -0
- package/bundled/ai/standards/ai-command-behavior.ai.yaml +45 -0
- package/bundled/ai/standards/api-design-standards.ai.yaml +192 -0
- package/bundled/ai/standards/branch-completion.ai.yaml +82 -0
- package/bundled/ai/standards/change-batching-standards.ai.yaml +195 -0
- package/bundled/ai/standards/database-standards.ai.yaml +220 -0
- package/bundled/ai/standards/error-codes.ai.yaml +37 -2
- package/bundled/ai/standards/git-worktree.ai.yaml +77 -0
- package/bundled/ai/standards/logging.ai.yaml +21 -2
- package/bundled/ai/standards/model-selection.ai.yaml +81 -0
- package/bundled/ai/standards/pipeline-integration-standards.ai.yaml +184 -0
- package/bundled/ai/standards/systematic-debugging.ai.yaml +73 -0
- package/bundled/ai/standards/verification-evidence.ai.yaml +84 -0
- package/bundled/ai/standards/workflow-enforcement.ai.yaml +253 -0
- package/bundled/core/acceptance-criteria-traceability.md +293 -0
- package/bundled/core/agent-dispatch.md +149 -0
- package/bundled/core/ai-command-behavior.md +239 -0
- package/bundled/core/api-design-standards.md +938 -0
- package/bundled/core/branch-completion.md +158 -0
- package/bundled/core/change-batching-standards.md +257 -0
- package/bundled/core/checkin-standards.md +43 -6
- package/bundled/core/database-standards.md +828 -0
- package/bundled/core/error-code-standards.md +228 -2
- package/bundled/core/forward-derivation-standards.md +54 -2
- package/bundled/core/git-worktree.md +131 -0
- package/bundled/core/model-selection.md +153 -0
- package/bundled/core/pipeline-integration-standards.md +230 -0
- package/bundled/core/systematic-debugging.md +156 -0
- package/bundled/core/testing-standards.md +49 -39
- package/bundled/core/verification-evidence.md +172 -0
- package/bundled/core/workflow-enforcement.md +132 -0
- package/bundled/locales/zh-CN/CLAUDE.md +2 -2
- package/bundled/locales/zh-CN/README.md +4 -4
- package/bundled/locales/zh-CN/SECURITY.md +92 -0
- package/bundled/locales/zh-CN/core/acceptance-criteria-traceability.md +301 -0
- package/bundled/locales/zh-CN/core/acceptance-test-driven-development.md +47 -0
- package/bundled/locales/zh-CN/core/accessibility-standards.md +124 -0
- package/bundled/locales/zh-CN/core/agent-dispatch.md +113 -0
- package/bundled/locales/zh-CN/core/ai-agreement-standards.md +103 -0
- package/bundled/locales/zh-CN/core/ai-command-behavior.md +247 -0
- package/bundled/locales/zh-CN/core/api-design-standards.md +946 -0
- package/bundled/locales/zh-CN/core/behavior-driven-development.md +48 -0
- package/bundled/locales/zh-CN/core/branch-completion.md +112 -0
- package/bundled/locales/zh-CN/core/change-batching-standards.md +265 -0
- package/bundled/locales/zh-CN/core/checkin-standards.md +43 -5
- package/bundled/locales/zh-CN/core/context-aware-loading.md +106 -0
- package/bundled/locales/zh-CN/core/database-standards.md +836 -0
- package/bundled/locales/zh-CN/core/deployment-standards.md +80 -0
- package/bundled/locales/zh-CN/core/error-code-standards.md +2 -2
- package/bundled/locales/zh-CN/core/forward-derivation-standards.md +493 -0
- package/bundled/locales/zh-CN/core/git-worktree.md +101 -0
- package/bundled/locales/zh-CN/core/model-selection.md +112 -0
- package/bundled/locales/zh-CN/core/performance-standards.md +104 -0
- package/bundled/locales/zh-CN/core/pipeline-integration-standards.md +238 -0
- package/bundled/locales/zh-CN/core/project-context-memory.md +124 -0
- package/bundled/locales/zh-CN/core/requirement-engineering.md +49 -0
- package/bundled/locales/zh-CN/core/security-standards.md +100 -0
- package/bundled/locales/zh-CN/core/systematic-debugging.md +106 -0
- package/bundled/locales/zh-CN/core/test-governance.md +116 -0
- package/bundled/locales/zh-CN/core/testing-standards.md +392 -156
- package/bundled/locales/zh-CN/core/verification-evidence.md +118 -0
- package/bundled/locales/zh-CN/core/virtual-organization-standards.md +104 -0
- package/bundled/locales/zh-CN/core/workflow-enforcement.md +132 -0
- package/bundled/locales/zh-CN/docs/CHEATSHEET.md +27 -1
- package/bundled/locales/zh-CN/docs/CLI-INIT-OPTIONS.md +9 -1
- package/bundled/locales/zh-CN/docs/FEATURE-REFERENCE.md +37 -9
- package/bundled/locales/zh-CN/docs/USER-MANUAL.md +652 -0
- package/bundled/locales/zh-CN/integrations/github-copilot/copilot-instructions.md +1 -1
- package/bundled/locales/zh-CN/integrations/openspec/AGENTS.md +29 -4
- package/bundled/locales/zh-CN/integrations/spec-kit/AGENTS.md +143 -72
- package/bundled/locales/zh-CN/skills/ac-coverage-assistant/SKILL.md +126 -0
- package/bundled/locales/zh-CN/skills/api-design-assistant/SKILL.md +100 -0
- package/bundled/locales/zh-CN/skills/brainstorm-assistant/SKILL.md +119 -0
- package/bundled/locales/zh-CN/skills/ci-cd-assistant/SKILL.md +82 -0
- package/bundled/locales/zh-CN/skills/commands/ac-coverage.md +97 -0
- package/bundled/locales/zh-CN/skills/commands/api-design.md +94 -0
- package/bundled/locales/zh-CN/skills/commands/atdd.md +163 -0
- package/bundled/locales/zh-CN/skills/commands/audit.md +77 -0
- package/bundled/locales/zh-CN/skills/commands/bdd.md +1 -1
- package/bundled/locales/zh-CN/skills/commands/brainstorm.md +87 -0
- package/bundled/locales/zh-CN/skills/commands/changelog.md +63 -0
- package/bundled/locales/zh-CN/skills/commands/check.md +219 -0
- package/bundled/locales/zh-CN/skills/commands/checkin.md +61 -0
- package/bundled/locales/zh-CN/skills/commands/ci-cd.md +77 -0
- package/bundled/locales/zh-CN/skills/commands/commit.md +81 -0
- package/bundled/locales/zh-CN/skills/commands/config.md +207 -0
- package/bundled/locales/zh-CN/skills/commands/coverage.md +74 -0
- package/bundled/locales/zh-CN/skills/commands/database.md +78 -0
- package/bundled/locales/zh-CN/skills/commands/derive-all.md +58 -0
- package/bundled/locales/zh-CN/skills/commands/derive-atdd.md +54 -0
- package/bundled/locales/zh-CN/skills/commands/derive-bdd.md +53 -0
- package/bundled/locales/zh-CN/skills/commands/derive-tdd.md +54 -0
- package/bundled/locales/zh-CN/skills/commands/derive.md +78 -0
- package/bundled/locales/zh-CN/skills/commands/dev-workflow.md +119 -0
- package/bundled/locales/zh-CN/skills/commands/discover.md +86 -0
- package/bundled/locales/zh-CN/skills/commands/docgen.md +67 -0
- package/bundled/locales/zh-CN/skills/commands/docs.md +65 -0
- package/bundled/locales/zh-CN/skills/commands/durable.md +87 -0
- package/bundled/locales/zh-CN/skills/commands/guide.md +52 -0
- package/bundled/locales/zh-CN/skills/commands/incident.md +92 -0
- package/bundled/locales/zh-CN/skills/commands/init.md +272 -0
- package/bundled/locales/zh-CN/skills/commands/methodology.md +1 -1
- package/bundled/locales/zh-CN/skills/commands/metrics.md +73 -0
- package/bundled/locales/zh-CN/skills/commands/migrate.md +92 -0
- package/bundled/locales/zh-CN/skills/commands/pr.md +80 -0
- package/bundled/locales/zh-CN/skills/commands/refactor.md +1 -1
- package/bundled/locales/zh-CN/skills/commands/release.md +62 -0
- package/bundled/locales/zh-CN/skills/commands/requirement.md +54 -0
- package/bundled/locales/zh-CN/skills/commands/reverse-bdd.md +47 -0
- package/bundled/locales/zh-CN/skills/commands/reverse-sdd.md +51 -0
- package/bundled/locales/zh-CN/skills/commands/reverse-tdd.md +51 -0
- package/bundled/locales/zh-CN/skills/commands/reverse.md +63 -0
- package/bundled/locales/zh-CN/skills/commands/review.md +50 -0
- package/bundled/locales/zh-CN/skills/commands/scan.md +76 -0
- package/bundled/locales/zh-CN/skills/commands/sdd-retro.md +40 -0
- package/bundled/locales/zh-CN/skills/commands/sdd.md +379 -0
- package/bundled/locales/zh-CN/skills/commands/security.md +75 -0
- package/bundled/locales/zh-CN/skills/commands/tdd.md +111 -0
- package/bundled/locales/zh-CN/skills/commands/update.md +337 -0
- package/bundled/locales/zh-CN/skills/commit-standards/SKILL.md +1 -2
- package/bundled/locales/zh-CN/skills/database-assistant/SKILL.md +97 -0
- package/bundled/locales/zh-CN/skills/dev-workflow-guide/SKILL.md +145 -0
- package/bundled/locales/zh-CN/skills/durable-execution-assistant/SKILL.md +84 -0
- package/bundled/locales/zh-CN/skills/incident-response-assistant/SKILL.md +107 -0
- package/bundled/locales/zh-CN/skills/metrics-dashboard-assistant/SKILL.md +67 -0
- package/bundled/locales/zh-CN/skills/migration-assistant/SKILL.md +77 -0
- package/bundled/locales/zh-CN/skills/pr-automation-assistant/SKILL.md +90 -0
- package/bundled/locales/zh-CN/skills/security-assistant/SKILL.md +79 -0
- package/bundled/locales/zh-CN/skills/security-scan-assistant/SKILL.md +72 -0
- package/bundled/locales/zh-CN/skills/spec-driven-dev/SKILL.md +73 -10
- package/bundled/locales/zh-CN/skills/spec-driven-dev/guide.md +267 -122
- package/bundled/locales/zh-TW/CLAUDE.md +2 -2
- package/bundled/locales/zh-TW/README.md +4 -4
- package/bundled/locales/zh-TW/SECURITY.md +92 -0
- package/bundled/locales/zh-TW/core/acceptance-criteria-traceability.md +301 -0
- package/bundled/locales/zh-TW/core/acceptance-test-driven-development.md +47 -0
- package/bundled/locales/zh-TW/core/accessibility-standards.md +84 -0
- package/bundled/locales/zh-TW/core/agent-dispatch.md +89 -0
- package/bundled/locales/zh-TW/core/ai-agreement-standards.md +87 -0
- package/bundled/locales/zh-TW/core/ai-command-behavior.md +247 -0
- package/bundled/locales/zh-TW/core/api-design-standards.md +946 -0
- package/bundled/locales/zh-TW/core/behavior-driven-development.md +63 -0
- package/bundled/locales/zh-TW/core/branch-completion.md +76 -0
- package/bundled/locales/zh-TW/core/change-batching-standards.md +265 -0
- package/bundled/locales/zh-TW/core/checkin-standards.md +39 -5
- package/bundled/locales/zh-TW/core/context-aware-loading.md +86 -0
- package/bundled/locales/zh-TW/core/database-standards.md +836 -0
- package/bundled/locales/zh-TW/core/deployment-standards.md +66 -0
- package/bundled/locales/zh-TW/core/error-code-standards.md +230 -4
- package/bundled/locales/zh-TW/core/forward-derivation-standards.md +62 -2
- package/bundled/locales/zh-TW/core/git-worktree.md +104 -0
- package/bundled/locales/zh-TW/core/model-selection.md +83 -0
- package/bundled/locales/zh-TW/core/performance-standards.md +84 -0
- package/bundled/locales/zh-TW/core/pipeline-integration-standards.md +238 -0
- package/bundled/locales/zh-TW/core/project-context-memory.md +79 -0
- package/bundled/locales/zh-TW/core/requirement-engineering.md +79 -0
- package/bundled/locales/zh-TW/core/security-standards.md +74 -0
- package/bundled/locales/zh-TW/core/systematic-debugging.md +95 -0
- package/bundled/locales/zh-TW/core/test-governance.md +88 -0
- package/bundled/locales/zh-TW/core/testing-standards.md +309 -86
- package/bundled/locales/zh-TW/core/verification-evidence.md +94 -0
- package/bundled/locales/zh-TW/core/virtual-organization-standards.md +88 -0
- package/bundled/locales/zh-TW/core/workflow-enforcement.md +132 -0
- package/bundled/locales/zh-TW/docs/CHEATSHEET.md +27 -1
- package/bundled/locales/zh-TW/docs/CLI-INIT-OPTIONS.md +9 -1
- package/bundled/locales/zh-TW/docs/FEATURE-REFERENCE.md +37 -9
- package/bundled/locales/zh-TW/docs/USER-MANUAL.md +652 -0
- package/bundled/locales/zh-TW/integrations/github-copilot/copilot-instructions.md +1 -1
- package/bundled/locales/zh-TW/integrations/openspec/AGENTS.md +29 -4
- package/bundled/locales/zh-TW/integrations/spec-kit/AGENTS.md +142 -71
- package/bundled/locales/zh-TW/skills/ac-coverage-assistant/SKILL.md +127 -0
- package/bundled/locales/zh-TW/skills/ai-friendly-architecture/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/ai-instruction-standards/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/api-design-assistant/SKILL.md +97 -0
- package/bundled/locales/zh-TW/skills/atdd-assistant/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/audit-assistant/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/bdd-assistant/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/brainstorm-assistant/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/changelog-guide/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/checkin-assistant/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/ci-cd-assistant/SKILL.md +79 -0
- package/bundled/locales/zh-TW/skills/code-review-assistant/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/commands/ac-coverage.md +95 -0
- package/bundled/locales/zh-TW/skills/commands/api-design.md +92 -0
- package/bundled/locales/zh-TW/skills/commands/atdd.md +167 -0
- package/bundled/locales/zh-TW/skills/commands/audit.md +75 -0
- package/bundled/locales/zh-TW/skills/commands/bdd.md +1 -1
- package/bundled/locales/zh-TW/skills/commands/brainstorm.md +91 -0
- package/bundled/locales/zh-TW/skills/commands/changelog.md +67 -0
- package/bundled/locales/zh-TW/skills/commands/check.md +223 -0
- package/bundled/locales/zh-TW/skills/commands/checkin.md +65 -0
- package/bundled/locales/zh-TW/skills/commands/ci-cd.md +75 -0
- package/bundled/locales/zh-TW/skills/commands/commit.md +85 -0
- package/bundled/locales/zh-TW/skills/commands/config.md +211 -0
- package/bundled/locales/zh-TW/skills/commands/coverage.md +78 -0
- package/bundled/locales/zh-TW/skills/commands/database.md +76 -0
- package/bundled/locales/zh-TW/skills/commands/derive-all.md +62 -0
- package/bundled/locales/zh-TW/skills/commands/derive-atdd.md +58 -0
- package/bundled/locales/zh-TW/skills/commands/derive-bdd.md +54 -0
- package/bundled/locales/zh-TW/skills/commands/derive-tdd.md +55 -0
- package/bundled/locales/zh-TW/skills/commands/derive.md +79 -0
- package/bundled/locales/zh-TW/skills/commands/dev-workflow.md +120 -0
- package/bundled/locales/zh-TW/skills/commands/discover.md +87 -0
- package/bundled/locales/zh-TW/skills/commands/docgen.md +68 -0
- package/bundled/locales/zh-TW/skills/commands/docs.md +66 -0
- package/bundled/locales/zh-TW/skills/commands/durable.md +85 -0
- package/bundled/locales/zh-TW/skills/commands/guide.md +51 -0
- package/bundled/locales/zh-TW/skills/commands/incident.md +90 -0
- package/bundled/locales/zh-TW/skills/commands/init.md +273 -0
- package/bundled/locales/zh-TW/skills/commands/methodology.md +1 -1
- package/bundled/locales/zh-TW/skills/commands/metrics.md +71 -0
- package/bundled/locales/zh-TW/skills/commands/migrate.md +90 -0
- package/bundled/locales/zh-TW/skills/commands/pr.md +78 -0
- package/bundled/locales/zh-TW/skills/commands/refactor.md +1 -1
- package/bundled/locales/zh-TW/skills/commands/release.md +63 -0
- package/bundled/locales/zh-TW/skills/commands/requirement.md +61 -0
- package/bundled/locales/zh-TW/skills/commands/reverse-bdd.md +54 -0
- package/bundled/locales/zh-TW/skills/commands/reverse-sdd.md +58 -0
- package/bundled/locales/zh-TW/skills/commands/reverse-tdd.md +58 -0
- package/bundled/locales/zh-TW/skills/commands/reverse.md +70 -0
- package/bundled/locales/zh-TW/skills/commands/review.md +57 -0
- package/bundled/locales/zh-TW/skills/commands/scan.md +74 -0
- package/bundled/locales/zh-TW/skills/commands/sdd-retro.md +47 -0
- package/bundled/locales/zh-TW/skills/commands/sdd.md +362 -0
- package/bundled/locales/zh-TW/skills/commands/security.md +73 -0
- package/bundled/locales/zh-TW/skills/commands/tdd.md +115 -0
- package/bundled/locales/zh-TW/skills/commands/update.md +326 -0
- package/bundled/locales/zh-TW/skills/commit-standards/SKILL.md +2 -6
- package/bundled/locales/zh-TW/skills/database-assistant/SKILL.md +94 -0
- package/bundled/locales/zh-TW/skills/dev-workflow-guide/SKILL.md +140 -0
- package/bundled/locales/zh-TW/skills/docs-generator/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/documentation-guide/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/durable-execution-assistant/SKILL.md +81 -0
- package/bundled/locales/zh-TW/skills/error-code-guide/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/forward-derivation/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/git-workflow-guide/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/incident-response-assistant/SKILL.md +104 -0
- package/bundled/locales/zh-TW/skills/logging-guide/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/methodology-system/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/metrics-dashboard-assistant/SKILL.md +64 -0
- package/bundled/locales/zh-TW/skills/migration-assistant/SKILL.md +74 -0
- package/bundled/locales/zh-TW/skills/pr-automation-assistant/SKILL.md +87 -0
- package/bundled/locales/zh-TW/skills/project-discovery/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/refactoring-assistant/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/release-standards/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/requirement-assistant/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/reverse-engineer/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/security-assistant/SKILL.md +76 -0
- package/bundled/locales/zh-TW/skills/security-scan-assistant/SKILL.md +69 -0
- package/bundled/locales/zh-TW/skills/spec-driven-dev/SKILL.md +74 -14
- package/bundled/locales/zh-TW/skills/spec-driven-dev/guide.md +243 -98
- package/bundled/locales/zh-TW/skills/tdd-assistant/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/test-coverage-assistant/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/testing-guide/SKILL.md +1 -5
- package/bundled/skills/README.md +11 -0
- package/bundled/skills/ac-coverage-assistant/SKILL.md +127 -0
- package/bundled/skills/ai-collaboration-standards/SKILL.md +10 -0
- package/bundled/skills/ai-friendly-architecture/SKILL.md +10 -0
- package/bundled/skills/ai-instruction-standards/SKILL.md +10 -0
- package/bundled/skills/api-design-assistant/SKILL.md +119 -0
- package/bundled/skills/atdd-assistant/SKILL.md +7 -0
- package/bundled/skills/bdd-assistant/SKILL.md +7 -0
- package/bundled/skills/brainstorm-assistant/SKILL.md +7 -0
- package/bundled/skills/checkin-assistant/SKILL.md +7 -0
- package/bundled/skills/ci-cd-assistant/SKILL.md +97 -0
- package/bundled/skills/code-review-assistant/SKILL.md +7 -0
- package/bundled/skills/commands/README.md +70 -0
- package/bundled/skills/commands/ac-coverage.md +135 -0
- package/bundled/skills/commands/api-design.md +86 -0
- package/bundled/skills/commands/atdd.md +69 -0
- package/bundled/skills/commands/audit.md +69 -0
- package/bundled/skills/commands/bdd.md +101 -0
- package/bundled/skills/commands/brainstorm.md +45 -0
- package/bundled/skills/commands/changelog.md +34 -0
- package/bundled/skills/commands/check.md +52 -1
- package/bundled/skills/commands/checkin.md +46 -0
- package/bundled/skills/commands/ci-cd.md +69 -0
- package/bundled/skills/commands/commit.md +79 -0
- package/bundled/skills/commands/config.md +48 -0
- package/bundled/skills/commands/coverage.md +53 -0
- package/bundled/skills/commands/database.md +70 -0
- package/bundled/skills/commands/derive-all.md +40 -0
- package/bundled/skills/commands/derive-atdd.md +33 -0
- package/bundled/skills/commands/derive-bdd.md +39 -0
- package/bundled/skills/commands/derive-tdd.md +40 -0
- package/bundled/skills/commands/derive.md +47 -0
- package/bundled/skills/commands/dev-workflow.md +104 -7
- package/bundled/skills/commands/discover.md +39 -0
- package/bundled/skills/commands/docgen.md +35 -0
- package/bundled/skills/commands/docs.md +40 -0
- package/bundled/skills/commands/durable.md +79 -0
- package/bundled/skills/commands/incident.md +84 -0
- package/bundled/skills/commands/init.md +55 -0
- package/bundled/skills/commands/methodology.md +72 -0
- package/bundled/skills/commands/metrics.md +65 -0
- package/bundled/skills/commands/migrate.md +84 -0
- package/bundled/skills/commands/pr.md +72 -0
- package/bundled/skills/commands/refactor.md +51 -0
- package/bundled/skills/commands/release.md +60 -0
- package/bundled/skills/commands/requirement.md +38 -0
- package/bundled/skills/commands/reverse-bdd.md +34 -0
- package/bundled/skills/commands/reverse-sdd.md +42 -0
- package/bundled/skills/commands/reverse-tdd.md +40 -0
- package/bundled/skills/commands/reverse.md +41 -0
- package/bundled/skills/commands/review.md +39 -0
- package/bundled/skills/commands/scan.md +68 -0
- package/bundled/skills/commands/sdd-retro.md +48 -0
- package/bundled/skills/commands/sdd.md +220 -0
- package/bundled/skills/commands/security.md +67 -0
- package/bundled/skills/commands/tdd.md +101 -0
- package/bundled/skills/commands/update.md +61 -0
- package/bundled/skills/commit-standards/SKILL.md +8 -2
- package/bundled/skills/database-assistant/SKILL.md +118 -0
- package/bundled/skills/dev-workflow-guide/SKILL.md +53 -7
- package/bundled/skills/dev-workflow-guide/workflow-phases.md +24 -0
- package/bundled/skills/durable-execution-assistant/SKILL.md +116 -0
- package/bundled/skills/forward-derivation/SKILL.md +7 -0
- package/bundled/skills/incident-response-assistant/SKILL.md +132 -0
- package/bundled/skills/methodology-system/SKILL.md +24 -2
- package/bundled/skills/metrics-dashboard-assistant/SKILL.md +109 -0
- package/bundled/skills/migration-assistant/SKILL.md +119 -0
- package/bundled/skills/pr-automation-assistant/SKILL.md +114 -0
- package/bundled/skills/project-discovery/SKILL.md +7 -0
- package/bundled/skills/refactoring-assistant/SKILL.md +7 -0
- package/bundled/skills/release-standards/SKILL.md +7 -0
- package/bundled/skills/requirement-assistant/SKILL.md +7 -0
- package/bundled/skills/reverse-engineer/SKILL.md +7 -0
- package/bundled/skills/security-assistant/SKILL.md +105 -0
- package/bundled/skills/security-scan-assistant/SKILL.md +96 -0
- package/bundled/skills/spec-driven-dev/SKILL.md +84 -4
- package/bundled/skills/spec-driven-dev/guide.md +156 -22
- package/bundled/skills/tdd-assistant/SKILL.md +7 -0
- package/bundled/skills/test-coverage-assistant/SKILL.md +11 -0
- package/bundled/skills/testing-guide/SKILL.md +23 -0
- package/package.json +1 -1
- package/src/commands/check.js +154 -8
- package/src/commands/config.js +231 -104
- package/src/commands/init.js +23 -3
- package/src/commands/update.js +250 -25
- package/src/config/ai-agent-paths.js +16 -0
- package/src/config/workflow-definitions.js +184 -0
- package/src/core/constants.js +6 -0
- package/src/flows/init-flow.js +23 -6
- package/src/i18n/messages.js +51 -3
- package/src/installers/integration-installer.js +67 -2
- package/src/installers/manifest-installer.js +1 -0
- package/src/installers/standards-installer.js +3 -2
- package/src/prompts/init.js +50 -3
- package/src/reconciler/manifest-migrator.js +2 -2
- package/src/reconciler/plan-executor.js +11 -0
- package/src/utils/integration-generator.js +327 -14
- package/src/utils/workflow-gate.js +292 -0
- package/standards-registry.json +160 -5
|
@@ -0,0 +1,836 @@
|
|
|
1
|
+
---
|
|
2
|
+
source: ../../../core/database-standards.md
|
|
3
|
+
source_version: 1.0.0
|
|
4
|
+
translation_version: 1.0.0
|
|
5
|
+
last_synced: 2026-03-23
|
|
6
|
+
status: current
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
# 数据库标准
|
|
10
|
+
|
|
11
|
+
> **语言**: [English](../../../core/database-standards.md) | 简体中文
|
|
12
|
+
|
|
13
|
+
> 版本: 1.0.0 | 最后更新: 2026-03-18
|
|
14
|
+
|
|
15
|
+
**适用性**: 所有软件项目
|
|
16
|
+
**范围**: universal
|
|
17
|
+
**产业标准**: ISO/IEC 9075 (SQL)、ACID 特性、BASE 定理
|
|
18
|
+
**参考资源**: [use-the-index-luke.com](https://use-the-index-luke.com/)、[sqlstyle.guide](https://www.sqlstyle.guide/)
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## 目的
|
|
23
|
+
|
|
24
|
+
本标准定义数据库设计、查询、迁移与运维的指导方针,涵盖关系型与非关系型数据库。包含 Schema 设计原则、索引策略、迁移工作流程、查询最优化、事务管理与数据完整性 — 确保数据库具备高性能、可维护且安全的特性。
|
|
25
|
+
|
|
26
|
+
**参考标准**:
|
|
27
|
+
- [ISO/IEC 9075 — SQL 标准](https://www.iso.org/standard/76583.html)
|
|
28
|
+
- [Use The Index, Luke — SQL 索引与调校](https://use-the-index-luke.com/)
|
|
29
|
+
- [Martin Fowler — 演进式数据库设计](https://martinfowler.com/articles/evodb.html)
|
|
30
|
+
- [Designing Data-Intensive Applications (Martin Kleppmann)](https://dataintensive.net/)
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
## 核心原则
|
|
35
|
+
|
|
36
|
+
| 原则 | 说明 |
|
|
37
|
+
|------|------|
|
|
38
|
+
| **数据完整性优先** | 约束、验证与引用完整性在数据库层级强制执行,而非仅在应用代码 |
|
|
39
|
+
| **Schema 即代码** | 数据库 Schema 通过迁移脚本进行版本控制且可重现 |
|
|
40
|
+
| **最小权限** | 数据库账号使用其功能所需的最低权限 |
|
|
41
|
+
| **先量测再调校** | 使用 EXPLAIN 计划与指标后再进行最优化;避免过早最优化 |
|
|
42
|
+
| **纵深防御** | 在多个层级使用加密、遮蔽与访问控制来保护敏感数据 |
|
|
43
|
+
| **向后兼容** | Schema 变更必须在部署窗口期间维持向后兼容 |
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## Schema 设计原则
|
|
48
|
+
|
|
49
|
+
### 正规化
|
|
50
|
+
|
|
51
|
+
套用正规化以消除冗余并确保数据完整性。以第三正规形式(3NF)作为事务系统的基准。
|
|
52
|
+
|
|
53
|
+
| 正规形式 | 规则 | 违反范例 |
|
|
54
|
+
|----------|------|----------|
|
|
55
|
+
| **1NF** | 仅原子值;无重复群组 | 单一字段中 `tags = "java,python,go"` |
|
|
56
|
+
| **2NF** | 1NF + 复合键无部分依赖 | 非键字段仅依赖复合主键的一部分 |
|
|
57
|
+
| **3NF** | 2NF + 无传递依赖 | `order.customer_name` 通过 `customer_id` 从 `customer.name` 衍生 |
|
|
58
|
+
|
|
59
|
+
### 反正规化决策矩阵
|
|
60
|
+
|
|
61
|
+
反正规化以完整性换取读取性能。需刻意为之并记录权衡考量。
|
|
62
|
+
|
|
63
|
+
| 判断条件 | 正规化 | 反正规化 |
|
|
64
|
+
|----------|--------|----------|
|
|
65
|
+
| 读写比例 | 写入密集或平衡 | 读取密集(>90% 读取) |
|
|
66
|
+
| 数据一致性 | 关键(财务、医疗) | 可接受最终一致性 |
|
|
67
|
+
| 查询复杂度 | 可接受的 JOIN 性能 | JOIN 导致不可接受的延迟 |
|
|
68
|
+
| 数据变动频率 | 经常更新 | 创建后很少变更 |
|
|
69
|
+
| 存储成本 | 最小化重复 | 存储便宜;速度是优先考量 |
|
|
70
|
+
|
|
71
|
+
**进行反正规化时:**
|
|
72
|
+
- 记录数据来源(source of truth)与同步机制
|
|
73
|
+
- 加入注释说明选择反正规化的原因
|
|
74
|
+
- 实现一致性检查或校正排程
|
|
75
|
+
|
|
76
|
+
### 命名惯例
|
|
77
|
+
|
|
78
|
+
| 元素 | 惯例 | 范例 |
|
|
79
|
+
|------|------|------|
|
|
80
|
+
| 数据表 | `snake_case`,单数 | `user_account`、`order_item` |
|
|
81
|
+
| 字段 | `snake_case` | `first_name`、`created_at` |
|
|
82
|
+
| 主键 | `id` | `user_account.id` |
|
|
83
|
+
| 外键 | `<被引用数据表>_id` | `order.user_account_id` |
|
|
84
|
+
| 布尔字段 | `is_` 或 `has_` 前缀 | `is_active`、`has_verified_email` |
|
|
85
|
+
| 时间戳记 | `_at` 后缀 | `created_at`、`updated_at`、`deleted_at` |
|
|
86
|
+
| 索引 | `idx_<数据表>_<字段>` | `idx_user_account_email` |
|
|
87
|
+
| 唯一约束 | `uq_<数据表>_<字段>` | `uq_user_account_email` |
|
|
88
|
+
| 检查约束 | `ck_<数据表>_<描述>` | `ck_order_positive_amount` |
|
|
89
|
+
|
|
90
|
+
### 保留字
|
|
91
|
+
|
|
92
|
+
- 避免使用 SQL 保留字作为标识符(`order`、`user`、`group`、`select`)
|
|
93
|
+
- 若无法避免,加上实体类型后缀:`user_account`、`order_record`、`user_group`
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
## 数据类型
|
|
98
|
+
|
|
99
|
+
### 选择适当类型
|
|
100
|
+
|
|
101
|
+
| 场景 | 建议 | 避免 |
|
|
102
|
+
|------|------|------|
|
|
103
|
+
| 金额值 | `DECIMAL(19,4)` 或 `NUMERIC` | `FLOAT`、`DOUBLE`(精度损失) |
|
|
104
|
+
| 日期时间 | `TIMESTAMPTZ`(含时区) | `VARCHAR` 存储日期 |
|
|
105
|
+
| 布尔标志 | `BOOLEAN` | `INT` (0/1)、`CHAR(1)` ('Y'/'N') |
|
|
106
|
+
| 短文本 (< 255) | `VARCHAR(n)` 含适当长度 | `TEXT` 用于已知长度字段 |
|
|
107
|
+
| 长文本 | `TEXT` | `VARCHAR(MAX)` 或过大的 `VARCHAR` |
|
|
108
|
+
| IP 地址 | 原生 IP 类型或 `INET` | `VARCHAR(45)` |
|
|
109
|
+
| JSON 数据 | `JSONB` (PostgreSQL) 或原生 JSON | `TEXT` 存储 JSON 字符串 |
|
|
110
|
+
| 枚举值 | `ENUM` 类型或查询表 | 字符串化的值 |
|
|
111
|
+
|
|
112
|
+
### UUID vs 自动递增
|
|
113
|
+
|
|
114
|
+
| 因素 | 自动递增 | UUID |
|
|
115
|
+
|------|----------|------|
|
|
116
|
+
| 存储大小 | 4-8 bytes | 16 bytes |
|
|
117
|
+
| 索引性能 | 较佳(循序写入) | 较差(随机插入造成 B-tree 碎片) |
|
|
118
|
+
| 分布式生成 | 需要协调 | 无需协调 |
|
|
119
|
+
| 安全性 | 可预测(可枚举) | 不可猜测 |
|
|
120
|
+
| URL 暴露 | 揭露记录数量 | 可安全用于公开 URL |
|
|
121
|
+
| 合并/复写 | 容易冲突 | 无冲突 |
|
|
122
|
+
|
|
123
|
+
**建议:**
|
|
124
|
+
- 单一数据库系统的内部 ID 使用**自动递增**
|
|
125
|
+
- 分布式系统或公开暴露的 ID 使用 **UUIDv7**(时间排序)
|
|
126
|
+
- 新项目考虑以 **UUIDv7** 为默认 — 兼具可排序性与唯一性
|
|
127
|
+
|
|
128
|
+
```sql
|
|
129
|
+
-- PostgreSQL:UUIDv7 作为主键
|
|
130
|
+
CREATE TABLE user_account (
|
|
131
|
+
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
132
|
+
email VARCHAR(255) NOT NULL,
|
|
133
|
+
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
-- 自动递增替代方案
|
|
137
|
+
CREATE TABLE user_account (
|
|
138
|
+
id BIGSERIAL PRIMARY KEY,
|
|
139
|
+
public_id UUID NOT NULL DEFAULT gen_random_uuid(),
|
|
140
|
+
email VARCHAR(255) NOT NULL,
|
|
141
|
+
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
|
142
|
+
);
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
---
|
|
146
|
+
|
|
147
|
+
## 索引策略
|
|
148
|
+
|
|
149
|
+
### 何时创建索引
|
|
150
|
+
|
|
151
|
+
| 创建索引的时机 | 避免创建索引的时机 |
|
|
152
|
+
|---------------|-------------------|
|
|
153
|
+
| 字段经常出现在 `WHERE` 子句中 | 数据表少于 1,000 行 |
|
|
154
|
+
| 字段用于 `JOIN` 条件 | 字段基数极低(如布尔值) |
|
|
155
|
+
| 字段用于 `ORDER BY` | 数据表为写入密集且很少读取 |
|
|
156
|
+
| 字段用于 `GROUP BY` | 字段经常更新 |
|
|
157
|
+
| 需要唯一约束 | 每张数据表已超过 8 个索引 |
|
|
158
|
+
|
|
159
|
+
### 复合索引字段顺序
|
|
160
|
+
|
|
161
|
+
复合索引的字段顺序影响重大。遵循**等值优先、范围最后**规则:
|
|
162
|
+
|
|
163
|
+
```sql
|
|
164
|
+
-- 查询模式:
|
|
165
|
+
-- WHERE status = 'active' AND created_at > '2026-01-01' ORDER BY created_at
|
|
166
|
+
|
|
167
|
+
-- 正确:等值字段在前,范围/排序字段在后
|
|
168
|
+
CREATE INDEX idx_order_status_created ON order_record (status, created_at);
|
|
169
|
+
|
|
170
|
+
-- 错误:范围字段在前,削弱等值筛选效果
|
|
171
|
+
CREATE INDEX idx_order_created_status ON order_record (created_at, status);
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
**字段顺序规则:**
|
|
175
|
+
1. 等值条件(`=`)优先
|
|
176
|
+
2. `IN` 条件其次
|
|
177
|
+
3. 范围条件(`>`、`<`、`BETWEEN`)最后
|
|
178
|
+
4. `ORDER BY` 字段放末端(若符合排序方向)
|
|
179
|
+
|
|
180
|
+
### 覆盖索引
|
|
181
|
+
|
|
182
|
+
覆盖索引包含查询所需的所有字段,实现仅索引扫描:
|
|
183
|
+
|
|
184
|
+
```sql
|
|
185
|
+
-- 查询:SELECT email, first_name FROM user_account WHERE status = 'active';
|
|
186
|
+
|
|
187
|
+
-- 覆盖索引 — 无需回表查询
|
|
188
|
+
CREATE INDEX idx_user_account_status_covering
|
|
189
|
+
ON user_account (status) INCLUDE (email, first_name);
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### 部分索引
|
|
193
|
+
|
|
194
|
+
对数据行的子集创建索引,以减少索引大小并改善写入性能:
|
|
195
|
+
|
|
196
|
+
```sql
|
|
197
|
+
-- 仅索引活跃记录(90% 的查询筛选 active)
|
|
198
|
+
CREATE INDEX idx_order_active
|
|
199
|
+
ON order_record (created_at)
|
|
200
|
+
WHERE status = 'active';
|
|
201
|
+
|
|
202
|
+
-- 仅索引非 NULL 值
|
|
203
|
+
CREATE INDEX idx_user_account_phone
|
|
204
|
+
ON user_account (phone_number)
|
|
205
|
+
WHERE phone_number IS NOT NULL;
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### 索引反模式
|
|
209
|
+
|
|
210
|
+
| 反模式 | 问题 | 解决方案 |
|
|
211
|
+
|--------|------|----------|
|
|
212
|
+
| **过度索引** | 拖慢写入、浪费存储空间 | 每季审查索引;移除未使用的 |
|
|
213
|
+
| **未使用的索引** | 有维护成本但无读取收益 | 监控 `pg_stat_user_indexes` 或同等工具 |
|
|
214
|
+
| **重复索引** | 多余的开销 | 索引 `(a, b)` 已涵盖 `(a)` 的查询 |
|
|
215
|
+
| **低基数字段索引** | 全表扫描通常更快 | 改用部分索引或 Bitmap 索引 |
|
|
216
|
+
| **FK 缺少索引** | CASCADE 删除与 JOIN 变慢 | 务必为外键字段创建索引 |
|
|
217
|
+
| **索引字段上使用函数** | 索引被绕过 | 创建函数/表达式索引 |
|
|
218
|
+
|
|
219
|
+
```sql
|
|
220
|
+
-- 反模式:函数阻止索引使用
|
|
221
|
+
SELECT * FROM user_account WHERE LOWER(email) = 'test@example.com';
|
|
222
|
+
|
|
223
|
+
-- 解决方案:表达式索引
|
|
224
|
+
CREATE INDEX idx_user_account_email_lower ON user_account (LOWER(email));
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
---
|
|
228
|
+
|
|
229
|
+
## 迁移策略
|
|
230
|
+
|
|
231
|
+
### 原则
|
|
232
|
+
|
|
233
|
+
| 原则 | 说明 |
|
|
234
|
+
|------|------|
|
|
235
|
+
| **版本控制** | 所有迁移与应用代码一同存储在版本控制系统 |
|
|
236
|
+
| **循序执行** | 迁移以确定性顺序执行 |
|
|
237
|
+
| **幂等性** | 执行两次迁移产生相同结果 |
|
|
238
|
+
| **已测试** | 迁移在部署前以类生产数据量进行测试 |
|
|
239
|
+
| **有文档** | 每次迁移包含变更内容与原因的说明 |
|
|
240
|
+
|
|
241
|
+
### 命名惯例
|
|
242
|
+
|
|
243
|
+
```
|
|
244
|
+
YYYYMMDDHHMMSS_description.sql
|
|
245
|
+
|
|
246
|
+
范例:
|
|
247
|
+
20260318120000_create_user_account_table.sql
|
|
248
|
+
20260318120100_add_email_index_to_user_account.sql
|
|
249
|
+
20260318120200_add_phone_column_to_user_account.sql
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
### 正向迁移 vs 可逆迁移
|
|
253
|
+
|
|
254
|
+
| 方式 | 优点 | 缺点 | 适用时机 |
|
|
255
|
+
|------|------|------|----------|
|
|
256
|
+
| **可逆** (up/down) | 容易回滚、较安全 | 需维护更多代码、部分变更不可逆 | 开发环境、非破坏性变更 |
|
|
257
|
+
| **正向** | 较简单、符合现实 | 需另外的回滚迁移 | 生产环境、破坏性变更 |
|
|
258
|
+
|
|
259
|
+
**建议:** 默认使用可逆迁移。对于破坏性操作(删除字段、删除数据表),使用正向迁移并搭配独立的回滚迁移文件。
|
|
260
|
+
|
|
261
|
+
### 零停机迁移模式(Expand-Contract)
|
|
262
|
+
|
|
263
|
+
适用于无法容忍停机的系统 Schema 变更:
|
|
264
|
+
|
|
265
|
+
**第一阶段 — Expand(向后兼容)**
|
|
266
|
+
```sql
|
|
267
|
+
-- 新增字段(可为 NULL,尚无约束)
|
|
268
|
+
ALTER TABLE user_account ADD COLUMN phone VARCHAR(20);
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
**第二阶段 — Migrate(双写)**
|
|
272
|
+
```sql
|
|
273
|
+
-- 回填现有数据
|
|
274
|
+
UPDATE user_account SET phone = legacy_phone WHERE phone IS NULL;
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
**第三阶段 — Contract(所有消费者更新后)**
|
|
278
|
+
```sql
|
|
279
|
+
-- 所有数据已填入后加入约束
|
|
280
|
+
ALTER TABLE user_account ALTER COLUMN phone SET NOT NULL;
|
|
281
|
+
-- 移除旧字段(仅在确认无消费者使用后)
|
|
282
|
+
ALTER TABLE user_account DROP COLUMN legacy_phone;
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
### 回滚策略
|
|
286
|
+
|
|
287
|
+
| 场景 | 回滚方式 |
|
|
288
|
+
|------|----------|
|
|
289
|
+
| 新增字段 | 移除该字段 |
|
|
290
|
+
| 新增索引 | 移除该索引 |
|
|
291
|
+
| 新增数据表 | 移除该数据表 |
|
|
292
|
+
| 移除字段 | 无法复原 — 需从备份还原或重新新增 |
|
|
293
|
+
| 数据转换 | 执行反向转换(若有设计) |
|
|
294
|
+
| 重新命名字段 | 重新命名回来 |
|
|
295
|
+
|
|
296
|
+
**关键规则:** 绝不在停止写入的同一次部署中移除字段或数据表。使用 Expand-Contract 模式。
|
|
297
|
+
|
|
298
|
+
---
|
|
299
|
+
|
|
300
|
+
## 查询最佳实践
|
|
301
|
+
|
|
302
|
+
### N+1 查询预防
|
|
303
|
+
|
|
304
|
+
N+1 问题发生在代码执行一个查询获取清单,再对每个项目额外执行 N 个查询。
|
|
305
|
+
|
|
306
|
+
```sql
|
|
307
|
+
-- N+1 问题(应用程序发出 N 个查询)
|
|
308
|
+
-- 查询 1:SELECT * FROM order_record WHERE user_id = 42;
|
|
309
|
+
-- 查询 2..N:SELECT * FROM order_item WHERE order_id = ?; (对每笔订单)
|
|
310
|
+
|
|
311
|
+
-- 解决方案:JOIN 或子查询
|
|
312
|
+
SELECT o.*, oi.*
|
|
313
|
+
FROM order_record o
|
|
314
|
+
JOIN order_item oi ON oi.order_id = o.id
|
|
315
|
+
WHERE o.user_id = 42;
|
|
316
|
+
|
|
317
|
+
-- 或批次加载
|
|
318
|
+
SELECT * FROM order_item
|
|
319
|
+
WHERE order_id IN (SELECT id FROM order_record WHERE user_id = 42);
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
### EXPLAIN 计划使用
|
|
323
|
+
|
|
324
|
+
对以下查询务必分析执行计划:
|
|
325
|
+
- 每分钟执行超过 100 次的查询
|
|
326
|
+
- 执行时间 > 100ms 的查询
|
|
327
|
+
- 涉及超过 10,000 行的查询
|
|
328
|
+
- 部署前的任何新查询
|
|
329
|
+
|
|
330
|
+
```sql
|
|
331
|
+
-- PostgreSQL
|
|
332
|
+
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
|
|
333
|
+
SELECT * FROM order_record WHERE status = 'pending' AND created_at > '2026-01-01';
|
|
334
|
+
|
|
335
|
+
-- 需关注的指标:
|
|
336
|
+
-- 大型数据表的 Seq Scan → 缺少索引
|
|
337
|
+
-- 高行数的 Nested Loop → 考虑 Hash Join
|
|
338
|
+
-- 高成本的 Sort → 加入符合 ORDER BY 的索引
|
|
339
|
+
-- 行数(估计 vs 实际) → 统计数据过时,执行 ANALYZE
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
### 分页
|
|
343
|
+
|
|
344
|
+
| 方法 | 优点 | 缺点 | 适用时机 |
|
|
345
|
+
|------|------|------|----------|
|
|
346
|
+
| **Offset 分页** | 简单、支持随机页面访问 | 大 Offset 时性能差、并发写入时结果不一致 | 小型数据集、管理后台 |
|
|
347
|
+
| **Keyset(游标)分页** | 性能一致、结果稳定 | 无法随机跳页、多字段排序时较复杂 | API、无限滚动、大型数据集 |
|
|
348
|
+
|
|
349
|
+
```sql
|
|
350
|
+
-- Offset 分页(大型数据表应避免)
|
|
351
|
+
SELECT * FROM order_record ORDER BY id LIMIT 20 OFFSET 10000;
|
|
352
|
+
-- 性能劣化:数据库须扫描并丢弃 10,000 行
|
|
353
|
+
|
|
354
|
+
-- Keyset 分页(建议使用)
|
|
355
|
+
SELECT * FROM order_record
|
|
356
|
+
WHERE id > :last_seen_id
|
|
357
|
+
ORDER BY id
|
|
358
|
+
LIMIT 20;
|
|
359
|
+
-- 无论页面深度,性能一致
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
### 查询反模式
|
|
363
|
+
|
|
364
|
+
| 反模式 | 问题 | 解决方案 |
|
|
365
|
+
|--------|------|----------|
|
|
366
|
+
| `SELECT *` | 获取不必要的数据、Schema 变更时可能中断 | 明确列出所需字段 |
|
|
367
|
+
| 查询中的字符串拼接 | SQL 注入漏洞 | 使用参数化查询 / Prepared Statement |
|
|
368
|
+
| 不同字段的 `OR` | 阻止索引使用 | 使用 `UNION ALL` 或重构查询 |
|
|
369
|
+
| `NOT IN` 含 NULL | 非预期的空结果 | 改用 `NOT EXISTS` |
|
|
370
|
+
| 隐含类型转换 | 绕过索引、结果错误 | 明确转型 |
|
|
371
|
+
| `LIKE '%prefix'` | 前导通配符阻止索引使用 | 使用全文搜索或反向索引 |
|
|
372
|
+
|
|
373
|
+
```sql
|
|
374
|
+
-- 反模式:SQL 注入风险
|
|
375
|
+
query = "SELECT * FROM user_account WHERE email = '" + email + "'";
|
|
376
|
+
|
|
377
|
+
-- 正确:参数化查询
|
|
378
|
+
query = "SELECT * FROM user_account WHERE email = $1";
|
|
379
|
+
params = [email];
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
---
|
|
383
|
+
|
|
384
|
+
## 事务管理
|
|
385
|
+
|
|
386
|
+
### ACID 特性
|
|
387
|
+
|
|
388
|
+
| 特性 | 说明 | 实施方式 |
|
|
389
|
+
|------|------|----------|
|
|
390
|
+
| **原子性 (Atomicity)** | 所有操作成功或全部失败 | 使用事务;避免部分提交 |
|
|
391
|
+
| **一致性 (Consistency)** | 数据库从一个有效状态转移到另一个 | 在数据库层级强制约束 |
|
|
392
|
+
| **隔离性 (Isolation)** | 并发事务不互相干扰 | 选择适当的隔离等级 |
|
|
393
|
+
| **持久性 (Durability)** | 已提交的数据在系统故障后存活 | 使用 WAL(预写日志);确认同步写入 |
|
|
394
|
+
|
|
395
|
+
### 隔离等级
|
|
396
|
+
|
|
397
|
+
| 等级 | 脏读 | 不可重复读 | 幻读 | 性能 | 使用场景 |
|
|
398
|
+
|------|------|-----------|------|------|----------|
|
|
399
|
+
| **Read Uncommitted** | 可能 | 可能 | 可能 | 最快 | 不建议使用 |
|
|
400
|
+
| **Read Committed** | 防止 | 可能 | 可能 | 快 | 大多数 RDBMS 的默认;一般用途查询 |
|
|
401
|
+
| **Repeatable Read** | 防止 | 防止 | 可能 | 中等 | 财务报表、库存检查 |
|
|
402
|
+
| **Serializable** | 防止 | 防止 | 防止 | 最慢 | 金钱转账、订位系统 |
|
|
403
|
+
|
|
404
|
+
**建议:** 使用 **Read Committed** 作为默认。仅在需要严格一致性的操作(如账户余额更新、座位预订)时提升至 **Repeatable Read** 或 **Serializable**。
|
|
405
|
+
|
|
406
|
+
### 死锁预防
|
|
407
|
+
|
|
408
|
+
| 策略 | 实施方式 |
|
|
409
|
+
|------|----------|
|
|
410
|
+
| **一致的锁定顺序** | 在所有事务中以相同顺序获取数据表/行的锁定 |
|
|
411
|
+
| **短事务** | 尽可能保持事务简短;将非数据库工作移出事务外 |
|
|
412
|
+
| **锁定超时** | 设定 `lock_timeout` 以快速失败而非无限等待 |
|
|
413
|
+
| **重试逻辑** | 对死锁错误(SQLSTATE 40P01)实现指数退避重试 |
|
|
414
|
+
| **避免用户交互** | 绝不在等待用户输入时保持事务开启 |
|
|
415
|
+
|
|
416
|
+
```sql
|
|
417
|
+
-- 设定锁定超时以防止无限等待
|
|
418
|
+
SET lock_timeout = '5s';
|
|
419
|
+
|
|
420
|
+
-- 保持事务简短
|
|
421
|
+
BEGIN;
|
|
422
|
+
UPDATE account SET balance = balance - 100 WHERE id = 1;
|
|
423
|
+
UPDATE account SET balance = balance + 100 WHERE id = 2;
|
|
424
|
+
COMMIT;
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
### 乐观锁定 vs 悲观锁定
|
|
428
|
+
|
|
429
|
+
| 因素 | 乐观锁定 | 悲观锁定 |
|
|
430
|
+
|------|----------|----------|
|
|
431
|
+
| 机制 | 写入时检查版本字段/时间戳记 | `SELECT ... FOR UPDATE` 获取行锁定 |
|
|
432
|
+
| 冲突率 | 低冲突环境 | 高冲突环境 |
|
|
433
|
+
| 性能 | 冲突罕见时较佳 | 冲突频繁时较佳 |
|
|
434
|
+
| 用户体验影响 | 用户可能看到「其他人已修改」错误 | 用户可能等待锁定释放 |
|
|
435
|
+
| 死锁风险 | 无 | 可能 |
|
|
436
|
+
|
|
437
|
+
```sql
|
|
438
|
+
-- 乐观锁定
|
|
439
|
+
UPDATE order_record
|
|
440
|
+
SET status = 'shipped', version = version + 1
|
|
441
|
+
WHERE id = 42 AND version = 3;
|
|
442
|
+
-- 若受影响行数 = 0,表示另一个事务已修改 → 重试或错误
|
|
443
|
+
|
|
444
|
+
-- 悲观锁定
|
|
445
|
+
BEGIN;
|
|
446
|
+
SELECT * FROM order_record WHERE id = 42 FOR UPDATE;
|
|
447
|
+
-- 行已锁定;其他事务等待
|
|
448
|
+
UPDATE order_record SET status = 'shipped' WHERE id = 42;
|
|
449
|
+
COMMIT;
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
---
|
|
453
|
+
|
|
454
|
+
## SQL vs NoSQL 决策矩阵
|
|
455
|
+
|
|
456
|
+
| 判断条件 | 关系型 (SQL) | 文档型 (NoSQL) | 键值型 | 图形型 |
|
|
457
|
+
|----------|-------------|---------------|--------|--------|
|
|
458
|
+
| **数据结构** | 结构化、明确定义的 Schema | 半结构化、弹性 Schema | 简单的 key→value 配对 | 高度连结的实体 |
|
|
459
|
+
| **一致性** | 强一致(ACID) | 最终一致(BASE),部分支持 ACID | 最终一致 | 依实现而异 |
|
|
460
|
+
| **查询复杂度** | 复杂 JOIN、聚合 | 简单查询、嵌套文档 | 单键查询 | 关系遍历 |
|
|
461
|
+
| **扩展模式** | 垂直扩展(scale-up) | 水平扩展(scale-out) | 水平扩展 | 依实现而异 |
|
|
462
|
+
| **Schema 变更** | 需要迁移 | 无 Schema / 弹性 | 无 Schema | Schema 可选 |
|
|
463
|
+
| **范例用途** | 财务系统、ERP、CRM | 内容管理、用户档案、产品目录 | 缓存、Session、速率限制 | 社交网络、推荐、欺诈检测 |
|
|
464
|
+
| **范例数据库** | PostgreSQL、MySQL、SQL Server | MongoDB、CouchDB、DynamoDB | Redis、Memcached、DynamoDB | Neo4j、Amazon Neptune |
|
|
465
|
+
|
|
466
|
+
### 决策指南
|
|
467
|
+
|
|
468
|
+
```
|
|
469
|
+
你的数据是否高度关联且需要复杂查询?
|
|
470
|
+
├── 是 → 关系型 (SQL)
|
|
471
|
+
└── 否 → 你的数据是否为简单的键值配对?
|
|
472
|
+
├── 是 → 键值型存储
|
|
473
|
+
└── 否 → 关系是否为主要的查询模式?
|
|
474
|
+
├── 是 → 图形数据库
|
|
475
|
+
└── 否 → 文档型数据库
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
**Polyglot Persistence(多语言持久化):** 许多系统受益于使用多种数据库类型。范例:
|
|
479
|
+
- **PostgreSQL** 用于事务数据(订单、账户)
|
|
480
|
+
- **Redis** 用于缓存与 Session
|
|
481
|
+
- **Elasticsearch** 用于全文搜索
|
|
482
|
+
- **Neo4j** 用于推荐引擎
|
|
483
|
+
|
|
484
|
+
---
|
|
485
|
+
|
|
486
|
+
## 连接管理
|
|
487
|
+
|
|
488
|
+
### 连接池
|
|
489
|
+
|
|
490
|
+
每个应用程序都必须使用连接池。每次请求创建新的数据库连接代价极高(TCP 握手、认证、SSL 协商)。
|
|
491
|
+
|
|
492
|
+
| 参数 | 建议默认值 | 说明 |
|
|
493
|
+
|------|-----------|------|
|
|
494
|
+
| **最小池大小** | 2-5 | 维持的最少空闲连接数 |
|
|
495
|
+
| **最大池大小** | 10-20 | 最大并发连接数 |
|
|
496
|
+
| **连接超时** | 5 秒 | 等待从池获取连接的时间 |
|
|
497
|
+
| **空闲超时** | 10 分钟 | 超过此时间关闭空闲连接 |
|
|
498
|
+
| **最大存活时间** | 30 分钟 | 回收连接以防止状态过时 |
|
|
499
|
+
| **验证查询** | `SELECT 1` | 返回连接前的健康检查 |
|
|
500
|
+
|
|
501
|
+
### 池大小公式
|
|
502
|
+
|
|
503
|
+
最大连接池大小的常用公式:
|
|
504
|
+
|
|
505
|
+
```
|
|
506
|
+
pool_size = (core_count * 2) + effective_spindle_count
|
|
507
|
+
|
|
508
|
+
范例:
|
|
509
|
+
- 4 核心服务器,SSD: (4 * 2) + 1 = 9-10 个连接
|
|
510
|
+
- 8 核心服务器,SSD: (8 * 2) + 1 = 17 个连接
|
|
511
|
+
- 4 核心服务器,4 HDD: (4 * 2) + 4 = 12 个连接
|
|
512
|
+
```
|
|
513
|
+
|
|
514
|
+
**重要:** 更多连接不一定更好。过多连接会导致:
|
|
515
|
+
- 数据库中的线程竞争
|
|
516
|
+
- 内存压力(每个连接使用约 5-10 MB)
|
|
517
|
+
- 增加上下文切换
|
|
518
|
+
|
|
519
|
+
### 健康检查
|
|
520
|
+
|
|
521
|
+
```sql
|
|
522
|
+
-- 基本健康检查
|
|
523
|
+
SELECT 1;
|
|
524
|
+
|
|
525
|
+
-- 进阶健康检查(验证读写能力)
|
|
526
|
+
SELECT NOW();
|
|
527
|
+
|
|
528
|
+
-- 使用前的连接验证
|
|
529
|
+
SET statement_timeout = '2s';
|
|
530
|
+
SELECT 1;
|
|
531
|
+
```
|
|
532
|
+
|
|
533
|
+
---
|
|
534
|
+
|
|
535
|
+
## 数据完整性
|
|
536
|
+
|
|
537
|
+
### 约束
|
|
538
|
+
|
|
539
|
+
务必在数据库层级强制数据完整性。应用程序层级的验证是补充,不是替代。
|
|
540
|
+
|
|
541
|
+
| 约束 | 用途 | 范例 |
|
|
542
|
+
|------|------|------|
|
|
543
|
+
| `NOT NULL` | 防止缺少必要数据 | `email VARCHAR(255) NOT NULL` |
|
|
544
|
+
| `UNIQUE` | 防止重复值 | `UNIQUE (email)` |
|
|
545
|
+
| `CHECK` | 验证值范围/格式 | `CHECK (amount > 0)` |
|
|
546
|
+
| `FOREIGN KEY` | 强制引用完整性 | `REFERENCES user_account(id)` |
|
|
547
|
+
| `DEFAULT` | 提供合理的默认值 | `DEFAULT NOW()` |
|
|
548
|
+
| `EXCLUSION` | 防止范围重叠 | `EXCLUDE USING gist (room WITH =, period WITH &&)` |
|
|
549
|
+
|
|
550
|
+
```sql
|
|
551
|
+
CREATE TABLE order_record (
|
|
552
|
+
id BIGSERIAL PRIMARY KEY,
|
|
553
|
+
user_account_id BIGINT NOT NULL REFERENCES user_account(id),
|
|
554
|
+
amount DECIMAL(19,4) NOT NULL CHECK (amount > 0),
|
|
555
|
+
status VARCHAR(20) NOT NULL DEFAULT 'pending'
|
|
556
|
+
CHECK (status IN ('pending', 'confirmed', 'shipped', 'delivered', 'cancelled')),
|
|
557
|
+
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
558
|
+
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
|
559
|
+
);
|
|
560
|
+
```
|
|
561
|
+
|
|
562
|
+
### 级联规则
|
|
563
|
+
|
|
564
|
+
| 规则 | 行为 | 适用时机 |
|
|
565
|
+
|------|------|----------|
|
|
566
|
+
| `CASCADE` | 自动删除/更新子行 | 强拥有权关系(order → order_items) |
|
|
567
|
+
| `SET NULL` | 父行删除时将 FK 设为 NULL | 可选关系(文章 → 作者删除时) |
|
|
568
|
+
| `SET DEFAULT` | 父行删除时将 FK 设为默认值 | 重新指派至默认值 |
|
|
569
|
+
| `RESTRICT` | 若存在子行则阻止父行删除 | 保护关键引用(user → audit_log) |
|
|
570
|
+
| `NO ACTION` | 与 RESTRICT 相同(可延迟检查) | 默认行为 |
|
|
571
|
+
|
|
572
|
+
**建议:** 默认使用 `RESTRICT`。仅在父子生命周期紧密耦合时使用 `CASCADE`。
|
|
573
|
+
|
|
574
|
+
### 软删除 vs 硬删除
|
|
575
|
+
|
|
576
|
+
| 因素 | 软删除 | 硬删除 |
|
|
577
|
+
|------|--------|--------|
|
|
578
|
+
| 实现方式 | `deleted_at TIMESTAMPTZ` 字段 | `DELETE FROM table` |
|
|
579
|
+
| 数据恢复 | 简单 — 设定 `deleted_at = NULL` | 需从备份还原 |
|
|
580
|
+
| 查询复杂度 | 每处都须加 `WHERE deleted_at IS NULL` | 查询较简单 |
|
|
581
|
+
| 存储 | 随时间增长 | 回收空间 |
|
|
582
|
+
| 合规性 | 保留审计轨迹 | 可能违反保留要求 |
|
|
583
|
+
| 性能 | 大量软删除行的大型数据表 | 更干净的表统计数据 |
|
|
584
|
+
|
|
585
|
+
```sql
|
|
586
|
+
-- 软删除实现
|
|
587
|
+
ALTER TABLE user_account ADD COLUMN deleted_at TIMESTAMPTZ;
|
|
588
|
+
|
|
589
|
+
-- 为活跃记录创建部分索引
|
|
590
|
+
CREATE INDEX idx_user_account_active ON user_account (email) WHERE deleted_at IS NULL;
|
|
591
|
+
|
|
592
|
+
-- 应用程序查询模式
|
|
593
|
+
SELECT * FROM user_account WHERE deleted_at IS NULL AND email = $1;
|
|
594
|
+
```
|
|
595
|
+
|
|
596
|
+
**建议:** 对面向用户的数据和任何需要审计轨迹的内容使用软删除。对暂时性数据(Session、临时 Token、超过保留期的日志)使用硬删除。
|
|
597
|
+
|
|
598
|
+
---
|
|
599
|
+
|
|
600
|
+
## 备份与恢复
|
|
601
|
+
|
|
602
|
+
### 备份策略类型
|
|
603
|
+
|
|
604
|
+
| 策略 | 说明 | 备份速度 | 还原速度 | 存储成本 |
|
|
605
|
+
|------|------|----------|----------|----------|
|
|
606
|
+
| **完整备份** | 整个数据库的完整副本 | 最慢 | 最快 | 最高 |
|
|
607
|
+
| **增量备份** | 自上次备份以来的变更 | 最快 | 最慢(需完整链) | 最低 |
|
|
608
|
+
| **差异备份** | 自上次完整备份以来的变更 | 中等 | 中等(完整 + 差异) | 中等 |
|
|
609
|
+
|
|
610
|
+
### RPO 与 RTO
|
|
611
|
+
|
|
612
|
+
| 指标 | 定义 | 目标范例 |
|
|
613
|
+
|------|------|----------|
|
|
614
|
+
| **RPO**(恢复点目标) | 可接受的最大数据损失(时间) | 1 小时:每小时备份;0:持续复写 |
|
|
615
|
+
| **RTO**(恢复时间目标) | 可接受的最大停机时间 | 15 分钟:自动容错切换;4 小时:手动还原 |
|
|
616
|
+
|
|
617
|
+
### 备份排程建议
|
|
618
|
+
|
|
619
|
+
| 层级 | RPO | RTO | 策略 |
|
|
620
|
+
|------|-----|-----|------|
|
|
621
|
+
| **关键**(财务、医疗) | < 1 分钟 | < 15 分钟 | 同步复写 + 持续 WAL 归档 |
|
|
622
|
+
| **重要**(电子商务、SaaS) | < 1 小时 | < 1 小时 | 流式复写 + 每小时 WAL 归档 |
|
|
623
|
+
| **标准**(内部工具) | < 24 小时 | < 4 小时 | 每日完整 + 每小时增量 |
|
|
624
|
+
| **低**(开发、预备环境) | < 1 周 | < 1 天 | 每周完整备份 |
|
|
625
|
+
|
|
626
|
+
### 备份测试
|
|
627
|
+
|
|
628
|
+
| 要求 | 频率 |
|
|
629
|
+
|------|------|
|
|
630
|
+
| 还原测试至独立环境 | 每月 |
|
|
631
|
+
| 还原后验证数据完整性 | 每次还原测试 |
|
|
632
|
+
| 量测实际 RTO vs 目标 | 每季 |
|
|
633
|
+
| 测试时间点恢复 | 每半年 |
|
|
634
|
+
| 更新文档与执行手册 | 每次测试后 |
|
|
635
|
+
|
|
636
|
+
---
|
|
637
|
+
|
|
638
|
+
## 敏感数据处理
|
|
639
|
+
|
|
640
|
+
### 字段层级加密
|
|
641
|
+
|
|
642
|
+
```sql
|
|
643
|
+
-- 写入时加密
|
|
644
|
+
INSERT INTO user_account (email, ssn_encrypted)
|
|
645
|
+
VALUES ($1, pgp_sym_encrypt($2, $encryption_key));
|
|
646
|
+
|
|
647
|
+
-- 读取时解密(仅授权角色可执行)
|
|
648
|
+
SELECT email, pgp_sym_decrypt(ssn_encrypted, $encryption_key) AS ssn
|
|
649
|
+
FROM user_account WHERE id = $1;
|
|
650
|
+
```
|
|
651
|
+
|
|
652
|
+
### 数据分级
|
|
653
|
+
|
|
654
|
+
| 等级 | 说明 | 范例 | 处理方式 |
|
|
655
|
+
|------|------|------|----------|
|
|
656
|
+
| **公开** | 无敏感性 | 营销内容、公开 API | 无特殊处理 |
|
|
657
|
+
| **内部** | 业务敏感 | 营收数据、产品路线图 | 访问控制、禁止公开暴露 |
|
|
658
|
+
| **机密** | 个人识别信息 | Email、电话、地址 | 静态加密、访问日志 |
|
|
659
|
+
| **限制** | 高度敏感 | 身份证号、信用卡、密码 | 字段加密、数据遮蔽、严格审计 |
|
|
660
|
+
|
|
661
|
+
### 数据遮蔽
|
|
662
|
+
|
|
663
|
+
```sql
|
|
664
|
+
-- 基于 View 的遮蔽,供客服人员使用
|
|
665
|
+
CREATE VIEW user_account_masked AS
|
|
666
|
+
SELECT
|
|
667
|
+
id,
|
|
668
|
+
LEFT(email, 2) || '***@' || SPLIT_PART(email, '@', 2) AS email,
|
|
669
|
+
'***-**-' || RIGHT(ssn, 4) AS ssn_masked,
|
|
670
|
+
first_name,
|
|
671
|
+
created_at
|
|
672
|
+
FROM user_account;
|
|
673
|
+
|
|
674
|
+
-- 仅授予客服团队访问遮蔽 View 的权限
|
|
675
|
+
GRANT SELECT ON user_account_masked TO support_role;
|
|
676
|
+
```
|
|
677
|
+
|
|
678
|
+
### PII 管理检查清单
|
|
679
|
+
|
|
680
|
+
- [ ] 识别并编目所有数据表中的 PII 字段
|
|
681
|
+
- [ ] 静态加密 PII(字段层级或表空间加密)
|
|
682
|
+
- [ ] 传输中加密 PII(所有连接使用 TLS)
|
|
683
|
+
- [ ] 非生产环境实施数据遮蔽
|
|
684
|
+
- [ ] 定义并执行保留策略
|
|
685
|
+
- [ ] 支持数据主体请求(GDPR 删除权、访问权)
|
|
686
|
+
- [ ] 记录所有对 PII 字段的访问
|
|
687
|
+
- [ ] 在开发与预备环境中匿名化数据
|
|
688
|
+
|
|
689
|
+
### 审计日志
|
|
690
|
+
|
|
691
|
+
```sql
|
|
692
|
+
-- 审计日志数据表
|
|
693
|
+
CREATE TABLE audit_log (
|
|
694
|
+
id BIGSERIAL PRIMARY KEY,
|
|
695
|
+
table_name VARCHAR(100) NOT NULL,
|
|
696
|
+
record_id BIGINT NOT NULL,
|
|
697
|
+
action VARCHAR(10) NOT NULL CHECK (action IN ('INSERT', 'UPDATE', 'DELETE')),
|
|
698
|
+
old_values JSONB,
|
|
699
|
+
new_values JSONB,
|
|
700
|
+
changed_by VARCHAR(100) NOT NULL,
|
|
701
|
+
changed_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
|
702
|
+
);
|
|
703
|
+
|
|
704
|
+
-- 基于触发器的审计(PostgreSQL 范例)
|
|
705
|
+
CREATE OR REPLACE FUNCTION audit_trigger_fn()
|
|
706
|
+
RETURNS TRIGGER AS $$
|
|
707
|
+
BEGIN
|
|
708
|
+
INSERT INTO audit_log (table_name, record_id, action, old_values, new_values, changed_by)
|
|
709
|
+
VALUES (
|
|
710
|
+
TG_TABLE_NAME,
|
|
711
|
+
COALESCE(NEW.id, OLD.id),
|
|
712
|
+
TG_OP,
|
|
713
|
+
CASE WHEN TG_OP != 'INSERT' THEN to_jsonb(OLD) END,
|
|
714
|
+
CASE WHEN TG_OP != 'DELETE' THEN to_jsonb(NEW) END,
|
|
715
|
+
current_user
|
|
716
|
+
);
|
|
717
|
+
RETURN COALESCE(NEW, OLD);
|
|
718
|
+
END;
|
|
719
|
+
$$ LANGUAGE plpgsql;
|
|
720
|
+
```
|
|
721
|
+
|
|
722
|
+
---
|
|
723
|
+
|
|
724
|
+
## 性能监控
|
|
725
|
+
|
|
726
|
+
### 慢查询日志
|
|
727
|
+
|
|
728
|
+
| 数据库 | 配置 | 建议阈值 |
|
|
729
|
+
|--------|------|----------|
|
|
730
|
+
| PostgreSQL | `log_min_duration_statement` | 200ms(开发)、1000ms(生产) |
|
|
731
|
+
| MySQL | `slow_query_log`、`long_query_time` | 1 秒 |
|
|
732
|
+
| SQL Server | Extended Events 或 Query Store | 1 秒 |
|
|
733
|
+
|
|
734
|
+
### 关键监控指标
|
|
735
|
+
|
|
736
|
+
| 指标 | 警告阈值 | 严重阈值 | 工具 |
|
|
737
|
+
|------|----------|----------|------|
|
|
738
|
+
| **活跃连接数** | > 最大值的 70% | > 最大值的 90% | 数据库仪表板 |
|
|
739
|
+
| **缓存/缓冲命中率** | < 95% | < 90% | `pg_stat_bgwriter`、InnoDB buffer pool |
|
|
740
|
+
| **复写延迟** | > 1 秒 | > 10 秒 | 复写监控 |
|
|
741
|
+
| **事务速率** | 偏离基线 > 20% | 偏离 > 50% | 应用程序指标 |
|
|
742
|
+
| **锁定等待时间** | 平均 > 1 秒 | > 5 秒 | 锁定监控查询 |
|
|
743
|
+
| **每分钟死锁数** | > 1 | > 5 | 数据库日志 |
|
|
744
|
+
| **数据表膨胀** | > 20% 死元组 | > 40% 死元组 | `pg_stat_user_tables` |
|
|
745
|
+
| **查询执行时间 (p95)** | > 500ms | > 2 秒 | APM 工具 |
|
|
746
|
+
|
|
747
|
+
### 查询计划分析工作流程
|
|
748
|
+
|
|
749
|
+
```
|
|
750
|
+
1. 识别慢查询(慢查询日志或 APM)
|
|
751
|
+
↓
|
|
752
|
+
2. 在预备环境以类生产数据执行 EXPLAIN ANALYZE
|
|
753
|
+
↓
|
|
754
|
+
3. 查找:
|
|
755
|
+
- 大型数据表的顺序扫描 → 加入索引
|
|
756
|
+
- 实际与估计行数差距大 → 执行 ANALYZE(更新统计数据)
|
|
757
|
+
- 大量迭代的 Nested Loop → 重构查询或加入索引
|
|
758
|
+
- 高成本的排序操作 → 加入符合排序顺序的索引
|
|
759
|
+
↓
|
|
760
|
+
4. 套用修正(加入索引、改写查询、更新统计数据)
|
|
761
|
+
↓
|
|
762
|
+
5. 重新执行 EXPLAIN ANALYZE 验证改善
|
|
763
|
+
↓
|
|
764
|
+
6. 部署并在生产环境监控执行时间
|
|
765
|
+
```
|
|
766
|
+
|
|
767
|
+
---
|
|
768
|
+
|
|
769
|
+
## 快速参考卡
|
|
770
|
+
|
|
771
|
+
### Schema 设计
|
|
772
|
+
|
|
773
|
+
```
|
|
774
|
+
✅ 所有标识符使用 snake_case
|
|
775
|
+
✅ 单数数据表名称(user_account,非 user_accounts)
|
|
776
|
+
✅ 外键使用 _id 后缀
|
|
777
|
+
✅ 必有 id、created_at、updated_at 字段
|
|
778
|
+
✅ 在数据库层级强制约束
|
|
779
|
+
✅ 正规化至 3NF,反正规化须有文档记载的理由
|
|
780
|
+
```
|
|
781
|
+
|
|
782
|
+
### 查询
|
|
783
|
+
|
|
784
|
+
```
|
|
785
|
+
✅ 务必使用参数化查询
|
|
786
|
+
✅ 明确列出所需字段,而非 SELECT *
|
|
787
|
+
✅ 使用 EXPLAIN ANALYZE 进行查询最优化
|
|
788
|
+
✅ 大型数据集优先使用 Keyset 分页而非 Offset
|
|
789
|
+
✅ 批次操作以预防 N+1 查询
|
|
790
|
+
✅ 设定 statement_timeout 以防止失控查询
|
|
791
|
+
```
|
|
792
|
+
|
|
793
|
+
### 运维
|
|
794
|
+
|
|
795
|
+
```
|
|
796
|
+
✅ 使用连接池(绝不创建每次请求的连接)
|
|
797
|
+
✅ 版本控制所有迁移
|
|
798
|
+
✅ 以类生产数据测试迁移
|
|
799
|
+
✅ 使用 Expand-Contract 模式进行零停机 Schema 变更
|
|
800
|
+
✅ 监控慢查询、连接数、缓存命中率
|
|
801
|
+
✅ 定期测试备份还原
|
|
802
|
+
```
|
|
803
|
+
|
|
804
|
+
---
|
|
805
|
+
|
|
806
|
+
## 相关标准
|
|
807
|
+
|
|
808
|
+
- [安全标准](../../../core/security-standards.md) — 数据加密、访问控制、PII 处理
|
|
809
|
+
- [性能标准](../../../core/performance-standards.md) — 应用程序层级的性能最优化
|
|
810
|
+
- [日志标准](../../../core/logging-standards.md) — 数据库操作的结构化日志
|
|
811
|
+
- [部署标准](../../../core/deployment-standards.md) — 数据库迁移作为部署管线的一部分
|
|
812
|
+
|
|
813
|
+
---
|
|
814
|
+
|
|
815
|
+
## 版本历程
|
|
816
|
+
|
|
817
|
+
| 版本 | 日期 | 变更 |
|
|
818
|
+
|------|------|------|
|
|
819
|
+
| 1.0.0 | 2026-03-18 | 初始发布 |
|
|
820
|
+
|
|
821
|
+
---
|
|
822
|
+
|
|
823
|
+
## 参考资源
|
|
824
|
+
|
|
825
|
+
- [ISO/IEC 9075 — SQL 标准](https://www.iso.org/standard/76583.html)
|
|
826
|
+
- [Use The Index, Luke](https://use-the-index-luke.com/) — SQL 索引与调校
|
|
827
|
+
- [SQL Style Guide](https://www.sqlstyle.guide/) — 一致的 SQL 格式
|
|
828
|
+
- [Martin Fowler — 演进式数据库设计](https://martinfowler.com/articles/evodb.html)
|
|
829
|
+
- [Designing Data-Intensive Applications](https://dataintensive.net/) — Martin Kleppmann
|
|
830
|
+
- [PostgreSQL 文档](https://www.postgresql.org/docs/)
|
|
831
|
+
|
|
832
|
+
---
|
|
833
|
+
|
|
834
|
+
## 授权
|
|
835
|
+
|
|
836
|
+
本标准以 [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/) 授权发布。
|