SQL Practice Logo

SQLPractice Online

SELECT & Data Retrieval: Performance

Module: SQL Fundamentals

**Best Practices:**

1. **Never use SELECT * in production code** - specify columns explicitly

2. **Select only needed columns** - reduces I/O, network traffic, memory usage

3. **Use covering indexes** - when all selected columns are in an index

4. **Avoid large text/blob columns** unless absolutely necessary

5. **Use LIMIT** when testing queries on large tables

6. **Understand projection cost** - database reads full rows, projects in memory

**Production Performance Rules:**

1. **Filter Early with WHERE**: Reduces rows before projection phase

- WHERE filters → fewer rows → less projection work → faster query

- For 1M rows: WHERE reduces to 10K rows = 99% less data to project

2. **Avoid Functions in WHERE**: Breaks index usage (non-sargable)

- ❌ WHERE YEAR(order_date) = 2024 -- Full table scan

- ✓ WHERE order_date >= '2024-01-01' AND order_date < '2025-01-01' -- Index seek

3. **Index Filtered Columns**: 100-1000x performance improvement

- Without index: Full table scan (reads all 1M rows)

- With index: Index seek (reads only matching rows)

4. **Specific Columns Save 90%+ Bandwidth**:

- 1M rows, 50 columns: SELECT * = 10GB transfer

- 1M rows, 5 columns: SELECT specific = 1GB transfer (90% savings)

**Performance Example with Real Metrics:**

Table: 1M rows, 50 columns (200 bytes/column), 10GB total size

**Scenario 1: SELECT * (all 50 columns)**

- Disk I/O: 10GB read

- Network transfer: 10GB

- Memory: 10GB buffered

- Time: 30 seconds

- Cost: $0.90 bandwidth (AWS)

**Scenario 2: SELECT id, name, email (3 columns)**

- Disk I/O: 10GB read (full rows)

- Projection: 600MB (3 columns)

- Network transfer: 600MB

- Memory: 600MB buffered

- Time: 5 seconds

- Cost: $0.05 bandwidth

- **6x faster, 18x cost savings**

**Scenario 3: Covering index on (id, name, email)**

- Disk I/O: 600MB read (index only)

- Network transfer: 600MB

- Memory: 600MB buffered