Senior
What indexes would you add to a table and why?
sobes.tech AI
Answer from AI
The choice of indexes depends on the table structure and the queries performed on it. Typically, the following are added:
- Indexes on columns involved in WHERE — to speed up filtering.
- Indexes on columns involved in JOIN — for quick table joins.
- Unique indexes — to ensure data uniqueness and speed up searches.
- Composite indexes — if filtering by multiple columns simultaneously is common.
For example, if there is an orders table with columns order_id, customer_id, order_date, and orders are often searched by customer and date, it is advisable to add an index on (customer_id, order_date).
It is important not to create too many indexes, as they slow down insert and update operations.