> For the complete documentation index, see [llms.txt](https://gunkim.gitbook.io/undefined/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gunkim.gitbook.io/undefined/mysql-secondary-index.md).

# MySQL 세컨더리 인덱스 (Secondary Index)

## 개념

* PK 외의 컬럼(예: 이름, 가입일 등)을 기준으로 빠르게 검색하기 위해 추가로 만든 인덱스.

## 핵심 구조 (PK 간접 참조)

* 세컨더리 인덱스의 B+Tree 리프 노드에는 실제 데이터의 물리 디스크 주소(RowID)가 아닌, \*\*'해당 데이터의 PK 값'\*\*이 저장되어 있음.

## 왜 물리 주소 대신 PK 값을 저장할까? (트레이드오프)

* 클러스터링 인덱스 쪽에서 데이터의 삽입/변경으로 인한 페이지 스플릿(데이터 물리적 이동)이 발생할 때, 수많은 세컨더리 인덱스들의 주소값을 일일이 다 업데이트해야 하는 **'쓰기(Write) 폭탄'을 방지하기 위함**.
* 즉, **읽기 시의 오버헤드를 약간 감수하고, 데이터 변경 시의 유지보수 안정성과 성능을 취한 구조**임.

## 조회 오버헤드: 북마크 룩업 (Bookmark Lookup)

* 세컨더리 인덱스 탐색 → PK 획득 → 클러스터링 인덱스 재탐색 → 실제 데이터 추출
* 두 번 트리를 타야 하며, 두 번째 탐색 시 디스크 곳곳을 점프하는 \*\*'랜덤 I/O'\*\*가 발생하여 성능이 저하될 수 있음.

## 한계 극복: 커버링 인덱스 (Covering Index)

* 북마크 룩업의 랜덤 I/O를 원천 차단하는 튜닝 기법.
* 쿼리의 `SELECT`나 `WHERE` 절에서 필요한 모든 컬럼을 세컨더리 인덱스에 포함(복합 인덱스)시킴.
* 클러스터링 인덱스로의 2차 탐색 없이 세컨더리 인덱스 단계에서 바로 데이터를 반환하므로 압도적으로 빠름.

## 오답 및 주의사항

> \[!WARNING] 초기 오개념: "세컨더리 인덱스 탐색 후 본 테이블 접근은 순차 I/O를 위함이다." 정정: 오히려 세컨더리 인덱스 기반의 원본 테이블 룩업은 **랜덤 I/O**를 발생시킨다. 순차 I/O를 위해서가 아니라, 데이터 이동 시의 막대한 갱신 부하를 막기 위해 랜덤 I/O라는 오버헤드를 *감수한 것*이다.

## 관련 개념

* \[\[mysql-clustered-index]]


---

# 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://gunkim.gitbook.io/undefined/mysql-secondary-index.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.
