> 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/the-sql-language/indexes/indexes-bitmap-scans.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# 11.5. 組合多個索引 \#

### 11.5. 組合多個索引 [#](#indexes-bitmap-scans) <a href="#indexes-bitmap-scans" id="indexes-bitmap-scans"></a>

單一索引掃描只能使用那些以其運算子類別之運算子作用在索引欄位上、並以 `AND` 連接的查詢子句。例如，給定一個建立在 `(a, b)` 上的索引，像 `WHERE a = 5 AND b = 6` 這樣的查詢條件可以使用該索引，但像 `WHERE a = 5 OR b = 6` 這樣的查詢則無法直接使用該索引。

幸運的是，PostgreSQL 能夠組合多個索引（包括多次使用同一個索引），來處理無法以單一索引掃描實作的情況。系統可以在多個索引掃描之間形成 `AND` 與 `OR` 條件。例如，像 `WHERE x = 42 OR x = 47 OR x = 53 OR x = 99` 這樣的查詢，可以拆解成對 `x` 上之索引的四次個別掃描，每次掃描使用其中一個查詢子句。接著將這些掃描的結果以 OR 合併，產生最終結果。另一個例子是，如果我們在 `x` 與 `y` 上各有獨立的索引，那麼像 `WHERE x = 5 AND y = 6` 這樣的查詢，一種可能的實作方式，是將每個索引搭配適當的查詢子句使用，然後將索引結果以 AND 合併，找出結果資料列。

為了組合多個索引，系統會掃描每個需要的索引，並在記憶體中準備一個*點陣圖*（bitmap），標示出被回報為符合該索引條件之資料表資料列的位置。接著依查詢的需要，將這些點陣圖以 AND 與 OR 合併。最後才實際走訪並回傳資料表的資料列。資料表的資料列是依實體順序走訪的，因為點陣圖就是這樣排列的；這表示原本索引的任何順序都會遺失，因此如果查詢有 `ORDER BY` 子句，就需要額外的排序步驟。由於這個原因，再加上每多一次索引掃描都會增加額外的時間，即使還有其他可用的索引，規劃器有時仍會選擇只使用簡單的索引掃描。

除了最簡單的應用之外，都會有各種可能有用的索引組合，資料庫開發人員必須做出取捨，決定要提供哪些索引。有時多欄位索引最好，但有時建立個別的索引並依賴索引組合功能會比較好。例如，如果你的工作負載混合了有時只涉及欄位 `x`、有時只涉及欄位 `y`、有時同時涉及兩個欄位的查詢，你可以選擇在 `x` 與 `y` 上各建立一個獨立的索引，並依賴索引組合來處理同時使用兩個欄位的查詢。你也可以建立一個 `(x, y)` 上的多欄位索引。對於同時涉及兩個欄位的查詢，這個索引通常會比索引組合更有效率，但如[第 11.3 節](https://docs.postgresql.tw/19/the-sql-language/indexes/indexes-multicolumn.md)所述，對於只涉及 `y` 的查詢，它的用處就比較小。到底有多少用處，取決於 B-tree 索引跳躍掃描最佳化的效果；如果 `x` 的相異值不超過數百個，跳躍掃描就能讓搜尋特定 `y` 值的查詢以相當有效率的方式執行。將 `(x, y)` 上的多欄位索引與 `y` 上的獨立索引組合使用，可能也相當不錯。對於只涉及 `x` 的查詢，可以使用多欄位索引，不過它會比只建立在 `x` 上的索引更大，因此也更慢。最後一種做法是三個索引全部建立，但這大概只有在資料表被搜尋的頻率遠高於被更新的頻率，而且三種查詢都很常見時才合理。如果其中一種查詢比其他兩種少見得多，你大概會只建立最符合常見查詢類型的那兩個索引。

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/indexes-bitmap-scans.html)（原文版本：18.6；核對日期：2026-09-11）


---

# 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/the-sql-language/indexes/indexes-bitmap-scans.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.
