Quick Reference
selective predicate + usable index -> index access candidate
large share of rows needed -> full/sequential scan can be cheaper
PostgreSQL -> Index Scan, Index Only Scan, Bitmap Index Scan + Bitmap Heap Scan
SQL Server -> Index Seek, Index ScanSeek는 SQL Server에서 자주 보이는 실행 계획 용어이고 PostgreSQL은 Index Scan, Index Only Scan, Bitmap Heap Scan 같은 이름을 씁니다. 제품별 노드 이름은 다르지만, index로 후보 범위를 좁힐지 전체를 순차 읽을지 비교한다는 원리는 같습니다.
읽기와 쓰기 비용
B-tree 계열 index는 정렬 가능한 key의 equality와 range 탐색에 흔히 사용됩니다. Hash, inverted, spatial 계열은 제품과 operator에 따라 다른 조회를 지원합니다.
Index는 조회를 빠르게 할 수 있지만 insert, update, delete 때 index entry도 유지해야 합니다. 자주 바뀌는 column에 중복 index를 많이 만들면 write latency와 storage가 증가합니다.
Composite Index
Composite index는 column 순서가 중요합니다. (team_id, created_at) index는 team_id equality 뒤 시간 범위와 잘 맞지만 created_at만 조회할 때 같은 효율을 보장하지 않습니다. 제품별 skip scan과 optimizer 기능은 별도 확인합니다.
자주 틀리는 점
- Seek나 index access는 항상 O(1)이라고 말하지 않습니다. Index 구조와 결과 row 수가 영향을 줍니다.
- Function이나 implicit cast가 index 사용을 막을 수 있습니다.
- 낮은 선택도의 boolean column 단독 index가 항상 유용하지는 않습니다.
- 실행 plan은 실제 parameter와 통계를 기준으로 확인합니다.
참고 링크
2 sources