Home>Article>Backend Development> Comparison of the use of Query, Exec and Prepare in Go SQL (with network packet capture)
The following columngolang tutorialwill introduce to you the comparison of the use of Query, Exec and Prepare in Comparison of the use of Query, Exec and Prepare in Comparison of the use of Query, Exec and Prepare in Comparison of the use of Query, Exec and Prepare in Comparison of the use of Query, Exec and Prepare in Comparison of the use of Query, Exec and Prepare in Comparison of the use of Query, Exec and Prepare in Go SQL (with network packet capture) SQL (with network packet capture) SQL (with network packet capture) SQL (with network packet capture) SQL (with network packet capture) SQL (with network packet capture) SQL (with network packet capture). I hope it will be helpful to friends in need. !
The database/sql library that comes with Comparison of the use of Query, Exec and Prepare in Comparison of the use of Query, Exec and Prepare in Comparison of the use of Query, Exec and Prepare in Comparison of the use of Query, Exec and Prepare in Comparison of the use of Query, Exec and Prepare in Comparison of the use of Query, Exec and Prepare in Go SQL (with network packet capture) SQL (with network packet capture) SQL (with network packet capture) SQL (with network packet capture) SQL (with network packet capture) SQL (with network packet capture) can talk to any relational database. To abstract some links to the database and manage the connection pool The underlying complexity, exposing a little more APIs than actually expected, ultimately leads to some imagination on how to use these APIs.
So, I will try to analyze the behavior of using the main APIs that were previously known.
I experimented with a Comparison of the use of Query, Exec and Prepare in Comparison of the use of Query, Exec and Prepare in Comparison of the use of Query, Exec and Prepare in Comparison of the use of Query, Exec and Prepare in Comparison of the use of Query, Exec and Prepare in Comparison of the use of Query, Exec and Prepare in Go SQL (with network packet capture) SQL (with network packet capture) SQL (with network packet capture) SQL (with network packet capture) SQL (with network packet capture) SQL (with network packet capture) application running on a virtual machine on the host machine (ubuntu) and amysqlserver.
Query tried: insert into items (name, price, description) values ('brownie',240,'sizzling')
Every time we You should always usedb.Querywhen doingselect, and we should never take care of the rows returned by Query, but iterate over them (otherwise we will leak the database link)
ExecutionQuery(query)Will not use prepared statements (see wireshark capture below)
View wireshark capture(query)
ExecutionQuery(queryTemplate, params)willuse prepared statementsbehind the scenes
db.Exec# whenever we want to executeinsertorupdateordelete##.Execution
Exec(query)Will not use prepared statements, so there are fewer TCP calls to SQL Server
Note from client to server Only 1 TCP request was sent (minus login)##Wireshark capture of Exec(query)
ExecutionExec(queryTemplate, params)
Will use prepared statements behind the scenes, so there will be more TCP calls to SQL Server.Note that 3 TCPs are sent from the client to the server Request (minus login)
Conclusion
First time One of the main confusions we encounter when using the sql package is that we are not aware that it creates prepared statements behind the scenes, even if we are not explicitly instructed to do so.
Hopefully the points above clarify, when are prepared statements invoked and how we can avoid them.
Hopefully the points above clarify, when are prepared statements invoked and how we can avoid them.
The above is the detailed content of Comparison of the use of Query, Exec and Prepare in Go SQL (with network packet capture). For more information, please follow other related articles on the PHP Chinese website!