Database indexing creates an alternative access path to stored data. Instead of inspecting every row or document, the database can search an index and retrieve matching records. Some queries can be answered entirely from an index when it contains all required values, although engine-specific visibility checks may still require access to the underlying table. Indexes can also enforce uniqueness where supported.
Choose indexes from observed access patterns rather than indexing every column. Identify frequent filters, join conditions, ordering requirements, query frequency, and latency targets. A customer-support application that repeatedly retrieves an order by order_id benefits from an indexed lookup. A report that reads most orders from the past year may perform better with a scan, especially in a columnar analytical database. Read-intensive workloads often justify more indexes; write-intensive ingestion systems usually need a smaller, carefully selected set.
Index structure matters. B-tree indexes commonly support equality searches, ordered traversal, and ranges. Hash indexes generally target equality lookups rather than ordered ranges, while inverted indexes support searches over terms or document attributes. Availability and behavior vary by database engine. An index does not guarantee that the optimizer will use it: estimated selectivity, table size, statistics, and the cost of fetching matching records all influence the plan.
Composite indexes must reflect how queries constrain their columns. For example, an index on customer_id followed by created_at can support retrieving one customer's orders within a date range, and may avoid a separate sort when the requested ordering matches. A query filtering only on created_at generally cannot use that index as effectively as one that constrains its leading column, although some engines support additional access strategies. Functions, implicit type conversions, and leading-wildcard searches can also prevent efficient use of a conventional index unless a suitable engine-specific index exists.
Every maintained index consumes storage and adds work to inserts, deletes, and updates that affect indexed values. Wider keys and unnecessary included attributes increase these costs and can reduce cache efficiency. In cloud databases, this can translate into higher I/O consumption, longer write latency, additional storage charges, or greater provisioned capacity requirements. Distributed secondary indexes may also introduce propagation delay or separate throughput limits, so consistency requirements must be evaluated alongside speed.
Indexing complements partitioning and compression rather than replacing them. Partitioning divides data into subsets; partition pruning can eliminate irrelevant subsets before indexes locate records within those that remain. Time-based partitions can simplify retention for event data, while suitable distribution keys help avoid hot partitions in distributed stores. Compression reduces storage and often I/O, but requires CPU to encode or decode data. Index compression support varies by engine, and compressing a table does not necessarily compress its indexes. Analytical systems frequently rely on columnar compression, sort order, and block elimination rather than conventional row-store indexes.
Apply index changes as production schema changes. Review execution plans and query statistics, benchmark representative parameter values, and measure both read improvements and write regressions. Creating or rebuilding an index on a large table can consume substantial I/O, CPU, temporary space, and locks; online or concurrent options have engine-specific restrictions. When evolving a schema, deploy required indexes before applications depend on new access patterns, validate their use, and remove redundant indexes only after checking constraints and less frequent workloads.
A common misconception is that more indexes always mean better performance. Low-selectivity predicates, small tables, and large result sets may favor scans, and stale statistics can lead to poor choices. Indexes also do not fix excessive network round trips, unbounded result sets, or badly distributed keys. Select the smallest useful set of indexes and revisit it as data volume and application behavior change.