Sobes.tech
Middle

How often have you had to delve into databases — writing and optimizing queries?

sobes.tech AI

Answer from AI

In my practice with Go, I often worked with databases, including writing and optimizing SQL queries. I usually use ORM or libraries like database/sql with drivers, but for performance enhancement and precise control, I write queries manually.

Optimization includes:

  • Using indexes and analyzing query execution plans (EXPLAIN).
  • Minimizing the number of queries through joins and aggregations.
  • Caching frequently requested data.

An example of a simple query using database/sql:

rows, err := db.Query("SELECT id, name FROM users WHERE active = ?", true)
if err != nil {
    log.Fatal(err)
}
for rows.Next() {
    var id int
    var name string
    err := rows.Scan(&id, &name)
    if err != nil {
        log.Fatal(err)
}
    fmt.Println(id, name)
}
How often have you had to delve into databases… - sobes.tech