Skip to content
Advertisement

What are the differences between QueryRow and Exec in Golang SQL package?

In Golang SQL package there are QueryRow and Exec for executing query. If I’m executing insertion query inside transaction, which is better to be used in term of performance?

err = tx.QueryRow(query, params).Scan(&id)

vs

result, err = tx.Exec(query, params)

Advertisement

Answer

If you’re inserting a single row use QueryRow – it is designed for, at most, one row update.

If you are performing multi-command updates/queries with multiple results, use Exec. The row results from Exec need to be closed after row iteration has completed.

So for ease of use & if you know you’re only manipulating one row, go with QueryRow.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement