> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/19/internals/geqo/geqo-intro.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# 61.1. 將查詢處理視為複雜最佳化問題 \#

### 61.1. 將查詢處理視為複雜最佳化問題 [#](#geqo-intro) <a href="#geqo-intro" id="geqo-intro"></a>

在所有關聯運算子中，最難處理與最佳化的是 *連接*（join）。可能的查詢計畫數量會隨著 查詢中連接的數量呈指數成長。此外，由於支援多種*連接 方法*（例如 PostgreSQL 中的巢狀迴圈、雜湊連接、合併連接） 來處理個別連接，以及多種*索引*（例如 PostgreSQL 中的 B-tree、hash、GiST 與 GIN）作為 關聯的存取路徑，也帶來了更多的最佳化負擔。

一般的 PostgreSQL 查詢最佳化器 會在替代策略空間中執行*近乎窮舉的搜尋*。此演算法最早 出現在 IBM 的 System R 資料庫中，能產生近乎最佳的連接順序， 但當查詢中的連接數量變多時，可能耗費極大量的時間與記憶體空間。 這使得一般的 PostgreSQL 查詢最佳化器 不適合用於連接大量資料表的查詢。

德國弗賴貝格工業大學（Freiberg）的自動控制研究所， 在想要使用 PostgreSQL 作為 電網維護用決策支援知識庫系統的 後端時，遇到了一些問題。該 DBMS 需要為 知識庫系統的推論機處理 大型連接查詢。這些查詢中的連接數量使得 使用一般查詢最佳化器變得不可行。

以下我們說明一種 *遺傳演算法*的實作，它能以對 涉及大量連接的查詢而言有效率的方式，解決連接 順序問題。

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/geqo-intro.html)（原文版本：18.6；核對日期：2026-10-17）


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.postgresql.tw/19/internals/geqo/geqo-intro.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
