Junior — Middle
What methods can be used to improve the performance of data read and write operations in a database?
sobes.tech AI
Answer from AI
To improve the performance of read and write operations in a database, the following methods can be used:
- Indexing: creating indexes on frequently used columns speeds up searches.
- Caching: storing frequently requested data in memory (e.g., Redis).
- Batching: combining multiple write operations into a single transaction.
- Using prepared statements for recurring queries.
- Query optimization: avoid SELECT *, use only necessary fields.
- Horizontal scaling and replication to distribute load.
Example in Go using prepared statements:
stmt, err := db.Prepare("INSERT INTO users(name, age) VALUES(?, ?)")
if err != nil {
log.Fatal(err)
}
_, err = stmt.Exec("Alice", 30)
if err != nil {
log.Fatal(err)
}