SQL Practice Logo

SQLPractice Online

Index Types Deep Dive: Performance

Module: Query Optimization & Performance

**Performance Characteristics by Index Type:**

```mermaid

graph TD

A[Performance Comparison] --> B[Lookup Speed]

A --> C[Range Query]

A --> D[Storage Size]

A --> E[Write Cost]

B --> B1[Hash: O1 - Fastest]

B --> B2[B-tree: O log n - Fast]

B --> B3[GIN: O log n - Fast]

B --> B4[GiST: O log n - Medium]

C --> C1[B-tree: Excellent]

C --> C2[GiST: Good]

C --> C3[Hash: Not Supported]

C --> C4[GIN: Not Supported]

D --> D1[Hash: Smallest]

D --> D2[B-tree: Medium]

D --> D3[GIN: Large]

D --> D4[GiST: Medium]

E --> E1[Hash: Low]

E --> E2[B-tree: Low]

E --> E3[GIN: High]

E --> E4[GiST: Medium]

style B1 fill:#c8e6c9

style C1 fill:#c8e6c9

style D1 fill:#c8e6c9

style E1 fill:#c8e6c9

```

**1. B-tree Performance:**

- Lookup: O(log n) - 3-4 disk reads for millions of rows

- Range: Excellent - sequential leaf scan

- Insert: O(log n) - may cause page splits

- Storage: ~10-15% of table size

- Best for: 90% of use cases

**2. Hash Performance:**

- Lookup: O(1) - single disk read (30% faster than B-tree)

- Range: Not supported

- Insert: O(1) - fast

- Storage: ~5-10% of table size (smaller)

- Best for: High-cardinality equality lookups