Junior — Middle
What methods can be used to implement indexing in a database?
sobes.tech AI
Answer from AI
Database indexing is a way to speed up data search and sorting. The main methods of implementing indexing are:
- B-trees (B-Tree) — the most common type of index, suitable for range searches and exact lookups.
- Hash indexes — effective for exact key searches but do not support range searches.
- Full-text indexes — used for quick text searches.
- Composite key indexes — index multiple columns simultaneously.
In Java, when working with databases (e.g., via JDBC or ORM), indexing is created at the DBMS level using SQL commands:
CREATE INDEX idx_name ON table_name(column_name);
Or using annotations in ORM, for example, in Hibernate:
@Entity
@Table(name = "users", indexes = {
@Index(name = "idx_username", columnList = "username")
})
public class User {
// fields
}
Indexes improve query performance but increase the time for inserting and updating data, so they should be used thoughtfully.