Open your query tool and run a simple SELECT * FROM orders WHERE customer_id = 4821 against a table with a few million rows. If customer_id has no index, the engine reads every row on every page of that table to find the handful you asked for. That single missing index is the most common reason a query that felt instant during development crawls in production.
Database indexing best practices for beginners come down to a handful of decisions: which columns deserve an index, in what order to compose a composite one, and — just as important — when to leave a column alone. Get those right and you avoid most of the performance pain that sends people hunting through execution plans at midnight.
Key Takeaways
- An index is a separate structure (usually a B-tree) that lets the engine jump to rows instead of scanning the whole table.
- Index the columns you filter, join and sort on — not every column you can think of.
- Every index adds write cost and storage; a table with too many indexes slows down INSERT and UPDATE.
- Composite index order matters: the leftmost column must appear in your query to make the index useful.
- Low-cardinality columns (a boolean flag, a status with three values) rarely benefit from a standalone index.
- Read the execution plan before and after you add anything — guessing is how you accumulate dead indexes.
Why indexing decides whether your query is fast or hopeless
A table without an index is a pile of pages the engine must read front to back. That is called a full table scan, and it works fine on a few thousand rows. On a few million it does not. An index is a sorted structure the engine can binary-search, so instead of touching every row it walks a tree of a few levels and lands directly on the matching entries.
Here is the trade-off nobody tells beginners clearly enough: the index has to be maintained. Every INSERT, UPDATE and DELETE that touches an indexed column also has to update the index. So the cost you pay for faster reads is paid on writes.
How indexing works in a database, in plain terms
Picture a textbook with two tables at the back: one sorted by topic, one sorted by page number. The topic list is your index. You look up "indexing," get a page number, and flip straight there. The book itself (the actual rows) hasn't moved. That's the whole idea — a small, sorted side structure pointing back into the main data.
Most relational engines use a B-tree for this. It stays balanced as rows are added, which keeps lookups predictable. Some engines also offer hash indexes, full-text indexes, or specialized types for JSON and geospatial data, but the B-tree is the default and the one worth understanding first.
Which columns actually deserve an index
Start with the queries you run most often, not with the schema. Open your slow query log, or ask the database what it's been spending time on. The columns that show up repeatedly in WHERE, JOIN and ORDER BY clauses are your candidates.
Columns that usually earn their keep
- Foreign keys — the join column between two tables is the classic case. Without an index on it, joins get expensive fast.
- Columns in frequent
WHEREfilters, especially with equality conditions. - Columns used for
ORDER BYwhen you're sorting large result sets. - Columns with high selectivity — an email address, a UUID, an order reference. These narrow the result set dramatically.
Columns you should probably leave alone
Low-cardinality columns are the trap. A is_active flag with two possible values divides your table roughly in half — the engine still reads half the rows, so the index barely helps. A status column with five values is similar.
And then there's over-indexing. I've seen tables with a dozen indexes, added one query at a time by different people, none of them documented. Writes had slowed to a crawl. Nobody could tell which indexes were still in use. Cleaning that up took an afternoon of checking plans and dropping candidates, one at a time, with a rollback plan ready.
Composite indexes: order is not a detail
When a query filters on more than one column, a composite index on those columns can do the job — but only if the order is right, because the engine can use a leftmost prefix of the index. An index on (last_name, first_name) helps a query that filters on last_name, or on both columns. It does not help a query that filters on first_name alone.
Practical rule: put the column used in equality filters first, the one used in range conditions after it. If a query says WHERE customer_id = ? AND created_at > ?, an index on (customer_id, created_at) is the shape you want.
Covering indexes and the read-everything problem
An index that contains every column a query needs is called covering — the engine answers straight from the index without touching the table. It's the fastest case, but it fattens the index and increases write overhead. Use it for hot queries, not everything.
Clustered and non-clustered: what the difference costs you
In SQL Server and similar engines, the clustered index defines the physical order of the rows. There can be only one per table, and the primary key is usually it. A non-clustered index is a separate structure with its own sorted keys and pointers back to the rows.
Why it matters practically: a non-clustered index lookup on a large table often means a second trip to fetch the actual row, called a key lookup. If your query returns many columns, that trip repeats per row and the index stops feeling fast. That's exactly when a covering index or a different index design helps.
| Index type | Physical order changed | How many per table | Typical use |
|---|---|---|---|
| Clustered | Yes — rows sorted by key | One | Primary key, range scans on that key |
| Non-clustered | No — separate structure | Many | Filtering and joining on other columns |
| Composite (clustered or non) | Depends on type | Depends | Multi-column filters, sort order |
| Covering | No | Many | Hot queries needing no table trip |
Diagnose before you add anything
The step beginners skip most often is looking at the execution plan. In PostgreSQL it's EXPLAIN or EXPLAIN ANALYZE; in SQL Server it's the graphical plan in your client. Look for a sequential scan on a large table, or a sort operation the engine had to do manually. Those are your signals.
Write down the query's timing before the change. Add the index. Time it again. If the plan still shows a full scan, the index isn't matching the query — usually a composite order issue or a function wrapped around the indexed column. WHERE UPPER(email) = '[email protected]' will not use a plain index on email, because the engine can't match the expression. Either store normalized data or index the expression itself.
Should you use a query index hint?
SQL Server lets you force an index with a hint like WITH (INDEX(idx_name)). My advice: treat it as a last resort, not a design tool. A hint freezes the optimizer's choice, and months later when the data distribution shifts, the forced plan can become the slow one — with no warning. Fix the index or the query instead.
Does indexing work the same way in MongoDB?
Same principle, different syntax. You call createIndex() on a collection, and it builds a B-tree under the hood. The rules transfer: index the fields you query and sort on, keep composite order correct, and watch the write cost. MongoDB's explain() shows whether a query used an index or did a collection scan — the same diagnostic habit applies.
Keeping indexes healthy over time
Indexes fragment as rows are inserted and updated. Some engines rebuild automatically; others need scheduled maintenance. If you're on a system that requires it, a periodic rebuild or reorganize keeps things from degrading quietly. Set it up once and check it works.
A routine that's served well: review index usage statistics monthly, drop indexes that show zero reads, and document why each remaining index exists. A one-line comment next to a non-obvious index saves the next person an hour of detective work.
The habit that separates beginners who cope from beginners who don't
Anyone can add an index. The harder skill is knowing which ones to drop, and being disciplined enough to measure before and after every change. That discipline is what keeps a database responsive as it grows — not a clever trick, just consistent diagnosis. And honestly, the first time you watch a query drop from seconds to milliseconds after fixing a composite order, you'll never guess at indexing again.