> For the complete documentation index, see [llms.txt](https://docs.verge.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.verge.io/knowledge-base/zh/system-administration/database-best-practices.md).

# 数据库最佳实践

在 VergeOS 上运行数据库，经过正确配置后可提供出色的性能。以下是优化数据库性能的推荐最佳实践，特别适用于运行在以下环境中的数据库： **Microsoft Windows** VergeOS 环境内。

***

## 性能优化的关键调整

* **备份位置**：始终将数据库备份直接存储在 VergeOS 内。这可确保更快的读写访问，并为备份操作带来更好的整体性能。
* **迁移前解密数据库**：如果您的数据库已加密，建议在将其迁移到 VergeOS 之前 **解密** 数据库。这样可以防止初始传输期间出现潜在的性能下降，并确保其在该环境中以最佳状态运行。
* **禁用自动碎片整理**：Windows 内置的 **自动碎片整理任务** 应被禁用。对虚拟磁盘进行碎片整理可能导致性能问题，尤其是在 SSD 或 VergeOS 等高度碎片化的虚拟环境中。
* **禁用 Windows Defender**：Windows Defender 虽然有助于保障安全，但由于实时扫描，可能会显著影响数据库性能。请禁用 Defender，或者至少 **排除** 以下项目，使其不被扫描：
  * 存储 **数据库文件** 的路径或驱动器
  * **日志文件** 以及 **事务日志驱动器**
* **关闭非必要的 Windows 服务**：许多 Windows 服务对于数据库性能并非必需，并且会消耗宝贵资源。禁用所有非必要服务，以便为数据库操作释放 CPU、RAM 和磁盘 I/O 资源。
* **CPU 核心超额分配**：避免超额分配 CPU 核心，尤其是在多个数据库 VM 共享相同物理 CPU 核心时。超额分配会导致资源争用，从而降低每个 VM 的性能。
* **在日志驱动器上禁用 Fsync**：对于用于存储日志的虚拟磁盘，请考虑禁用 **Fsync**。这可以通过降低日志写入时强制数据同步的频率来提高 I/O 性能。您可以通过编辑 VergeOS 中特定虚拟磁盘的设置来完成此操作。

  <div data-gb-custom-block data-tag="hint" data-style="warning" class="hint hint-warning"><p><strong>重要提示</strong></p><p>禁用 Fsync 可能会在发生崩溃时导致数据丢失。请确保这符合您的数据库在 I/O 性能与可靠性之间取舍的容忍度。</p></div>
* **核心分配限制**：限制分配给数据库 VM 的核心数量，使其不超过 **单个 CPU 插槽上可用的物理核心数**。计算时不要将超线程核心计入其中。

  **示例**：在具有两个 CPU 插槽的系统中，每个插槽有 8 个物理核心（共 16 核、32 线程），请将 VM 限制为使用 **8 个核心** （1 个物理插槽），避免跨多个插槽超额分配。
* **内存分配**：确保分配给数据库 VM 的内存不超过单个 CPU 插槽中可用的物理内存。这可确保内存访问本地化，并降低内存访问延迟。

***

## 网络和存储优化

* **使用 VirtIO 驱动程序**：为获得更好的网络性能，建议尽可能使用 **VirtIO 网络驱动程序** 。这些驱动程序针对虚拟环境中的性能进行了优化，与模拟驱动程序（例如 e1000）相比，可提供显著更高的吞吐量。

  您可以通过以下链接下载最新稳定版 VirtIO 驱动程序： [下载 VirtIO 驱动程序](https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/stable-virtio/virtio-win.iso)
* **监控磁盘 I/O**：定期监控磁盘 I/O 性能，尤其是日志文件或 tempdb 文件等高写入活动卷；如有需要，请考虑分配高性能 SSD 或 NVMe 驱动器。

遵循这些最佳实践，您可以优化在 VergeOS 上运行的数据库系统的性能和可靠性。如果需要进一步微调，VergeOS 支持其他功能，可帮助实时监控和改善 VM 性能。

***

{% hint style="info" %}
**文档信息**

* 最后更新：2024-08-29
* vergeOS 版本：4.12.6
  {% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.verge.io/knowledge-base/zh/system-administration/database-best-practices.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
