concatenated index
https://use-the-index-luke.com/sql/where-clause/the-equals-operator/concatenated-keys
this is also called multi-column, composite or combined index
the order ot a concatenated index has great impact on its usability os it must be chosed carefully.
The database does not use the index because it cannot use single columns from a concatenated index arbitrarily. A closer look at the index structure makes this clear.

the second index is based on the first so that's why the order of the composite index is so important.
Table EMMPLOYEE
employee_id is not unique but it's unique in each department so we can change the primary key with two column
CONSTRAINT constraint_name PRIMARY KEY(column_1, column_2,...);
it's very similar to
CREATE UNIQUE INDEX employees_pk
ON employees (employee_id, department_id)
the datastructure for concatenated index behind the scene is B-tree like the others index.
the first column is the primary sort criterion, if the first is the same it will go on to compare the second column
so for more than two columns, the concept is the same!
An index with three columns can be used when searching for the first column, when searching with the first two columns together, and when searching using all columns
some example postgresql sql to add index
CREATE INDEX CONCURRENTLY idx_transfer_status_from_to ON user_transferrecord (status, from_provider_id, to_provider_id);
DROP INDEX CONCURRENTLY idx_transfer_status_from_to;
Another use case analyze
PostgreSQL 查詢效能分析報告
針對 `game_betrecord` 表的高 CPU 成本查詢進行分析
1. 執行摘要
本報告分析了一筆針對 game_betrecord 表的 SQL 查詢,該查詢在 Google Cloud Query Insight 中顯示出極高的 CPU 成本(Cost),主要歸因於兩個低效率的 Bitmap Index Scan。儘管查詢最終返回 0 行,但資料庫仍消耗大量資源來掃描並合併索引位圖。
核心問題: 資料庫試圖合併兩個獨立的單欄索引(user_id 和 created_at),導致了昂貴的 BitmapAnd 操作。
建議方案: 建立一個複合索引(Composite Index),以允許查詢計畫(Query Plan)轉為高效的 Index Scan,從而大幅降低 CPU 消耗。
2. 問題查詢 (Problematic Query)
以下是從 Query Insight 識別出的高成本 SQL 查詢:
SELECT
"game_betrecord"."game_id",
MAX("game_betrecord"."start_time") AS "max_start_time"
FROM
"game_betrecord"
WHERE
("game_betrecord"."created_at" BETWEEN $1::timestamptz
AND $2::timestamptz
AND "game_betrecord"."start_time" BETWEEN $3::timestamptz
AND $4::timestamptz
AND "game_betrecord"."user_id" = $5)
GROUP BY
"game_betrecord"."game_id"
ORDER BY
"max_start_time" DESC
3. 效能瓶頸:高成本的 Bitmap Scans
Query Insight 顯示,CPU 成本主要來自兩個 Bitmap Index Scan 操作,即使它們最終並未返回任何數據行。
瓶頸 1: game_betrecord_created_at_idx
- 操作: Bitmap Index Scan
- 索引:
game_betrecord_created_at_idx - 返回行數: 0
- 成本 (Cost): 68,938.97
瓶頸 2: game_betrecord_user_id_1a25edc4
- 操作: Bitmap Index Scan
- 索引:
game_betrecord_user_id_1a25edc4 - 返回行數: 0
- 成本 (Cost): 321.57
4. 根本原因分析
這個現象在 PostgreSQL 處理多重篩選條件時非常常見。您看到的「Bitmap Index Scan 成本很高,但返回行數 = 0」意味著 PostgreSQL 正在執行以下低效率的操作:
- PostgreSQL 發現
WHERE子句中有多個條件,並且這些條件各自對應到不同的索引。 - (高成本) 它首先使用
game_betrecord_created_at_idx索引,掃描created_at範圍內的所有行的「位圖(bitmap)」。 - (中成本) 接著,它使用
game_betrecord_user_id_1a25edc4索引,掃描符合user_id的所有行的「位圖」。 - (高成本) 然後,它執行 BitmapAnd 操作,計算這兩個巨大位圖的「交集」,以找出同時滿足兩個條件的行。
- 在本案例中,交集為空,但資料庫仍然支付了「掃描並合併兩個位圖」的全部 CPU 成本。
當單個索引(尤其是日期範圍)的選擇性不高時,產生的位圖會非常大,導致合併成本(CPU cost)激增。
5. 建議解決方案:建立複合索引
這是最有效且最推薦的解決方案。我們需要建立一個「複合索引」(Composite Index),該索引包含查詢中最具選擇性(最能縮小範圍)的欄位,並將其他 WHERE 條件欄位也包含進去。
由於 user_id = $5 是等值查詢,通常最具選擇性,應放在索引的第一位。
建議的索引:
CREATE INDEX CONCURRENTLY idx_betrecord_user_created_start
ON game_betrecord (user_id, created_at, start_time);
預期效益:
建立此索引後,PostgreSQL 的查詢計畫器(Planner)將不再需要掃描兩個獨立的索引並進行昂貴的位圖合併。
相反,它可以直接使用這個新的複合索引執行一次高效的 Index Scan(或 Index-Only Scan),直接定位到同時滿足 user_id, created_at, 和 start_time 範圍的數據,CPU 成本將大幅下降。
註: CONCURRENTLY 關鍵字允許在不鎖定寫入操作的情況下建立索引,這對生產環境至關重要。
6. 附錄:監控索引建立進度
使用 CREATE INDEX CONCURRENTLY 時,psql 終端看起來會「卡住」,因為它涉及多個事務階段。您可以在另一個 psql session 中執行以下查詢來監控其實際進度:
SELECT
pid,
query,
state,
wait_event_type,
wait_event
FROM
pg_stat_activity
WHERE
query LIKE '%CREATE INDEX CONCURRENTLY%';
您將會看到類似 state = active 以及 wait_event_type = IO 的活動,表示索引正在被積極建立。