Lexicon
⌘K
Explore
ExploreGuidesNotes
WriteMy Library
Sign in
Browse

Networking

  • OSI Model
  • TCP vs UDP
  • TCP/IP Model
  • Layer 2 & Layer 3 Networking

Programming

  • Start Here — How to Solve a Problem
  • From if/else to DSA — The Whole Path
  • Problem Solving
  • DSA Practice — Learn by Solving
  • C Programming
  • Python

Bug Bounty

  • Only Bug Bounty Checklist You'll Ever Need!!!
  • Only Bug Bounty Checklist You'll Ever Need!!! V2

My Notes

  • Python Conditional Statements - The Complete Breakdown
  • Python Data Types - The Complete Breakdown
  • Python Dictionaries - The Complete Breakdown
  • Python Exception Handling - The Complete Breakdown
  • Python Functions - The Complete Breakdown
  • Python Lists vs Arrays — The Real Story
  • Python Loops - The Complete Breakdown
  • Python OOP - The Complete Breakdown
  • The f Thing in Python - f-strings

Python

  • Python String Manipulation Functions - Full Reference
New noteSuggest a guide

Lexicon

a personal knowledge base

← Back to post

Version history

No earlier versions yet — history builds up each time this note is edited.

Current version1 of 1

Databases

Database indexing basics

An index is a sorted lookup structure that lets the database find rows without scanning the whole table. It is the single biggest lever for query speed, and the most commonly overlooked.

The book analogy

Finding a topic by flipping every page is a full table scan. Using the index at the back of the book to jump straight to the page is an index lookup. Same data, wildly different effort.

When an index helps

Indexes speed up:

  • WHERE filters on the indexed column
  • JOIN conditions
  • ORDER BY and GROUP BY on indexed columns
CREATE INDEX idx_users_email ON users (email);

-- Now this is a fast lookup instead of a scan
SELECT * FROM users WHERE email = 'a@example.com';

Composite indexes and column order

A multi-column index is ordered left to right. An index on (status, created_at) helps queries that filter on status, or on status and created_at together, but not one that filters on created_at alone.

CREATE INDEX idx_orders_status_date ON orders (status, created_at);

Rule of thumb: put the most selective column, or the one you always filter on, first.

The cost

Indexes are not free:

  • Every INSERT, UPDATE, and DELETE must also update the index
  • Indexes take disk space
  • Too many indexes slow down writes and confuse the query planner

Reading the plan

Ask the database what it is doing rather than guessing.

EXPLAIN ANALYZE SELECT * FROM users WHERE email = 'a@example.com';

Look for a Seq Scan on a large table where you expected an Index Scan. That gap is usually where your slow query lives.

Covering indexes

If an index contains every column a query needs, the database can answer from the index alone and never touch the table. That is a covering index, and it is a nice win for hot read paths.