Explain what an index is and why it's used.
Instruction: Describe the concept of an index in databases and its importance.
Updated
Official answer
An index is an additional data structure that can help a database find or order relevant rows without scanning all table data. For example, a suitable index on a customer identifier can help locate a small set of matching orders.
The benefit depends on the query and data. An index may support filtering, joins or ordering, but the planner can reasonably choose a scan when much of the table is needed. An index does not guarantee a speedup or guarantee the order of query output.
Indexes consume space and introduce maintenance work when affected data changes. A composite index also has an ordering of keys; an index on (customer_id, order_date) is not generally interchangeable with one on (order_date, customer_id). Index types and capabilities depend on the database engine.
Choose indexes from real query patterns, inspect the plan, and measure representative workloads, including writes. Avoid adding an index to every column or claiming an improvement from a single tiny test. A unique index can also enforce a uniqueness rule under the engine's semantics, which is a separate purpose from speed.
Assessment: Explain the read benefit, maintenance cost and conditions under which the index may not be used.