最新的MySQL和PostgreSQL是否会自动准备每个查询?

huangapple go评论83阅读模式
英文:

Does latest Mysql and Postgres prepare every query automatically?

问题

我在sqlx文档中找到了这段内容:


在大多数数据库中,当执行查询时,语句实际上会在后台进行准备。但是,你也可以使用sqlx.DB.Prepare()显式地准备语句以便在其他地方重复使用。

虽然我找不到数据库实际上准备每个查询的证据。
所以,这是真的吗?我应该手动准备吗?

英文:

I come across this in sqlx docs:


On most databases, statements will actually be prepared behind the scenes whenever a query is executed. However, you may also explicitly prepare statements for reuse elsewhere with sqlx.DB.Prepare():


Although I can't find proof that databases actually prepare every query.
So is it true, should I use prepare manually?

答案1

得分: 3

MySQL和PostgreSQL肯定不会为每个查询做准备。您可以直接执行查询,而无需进行准备和执行序列。

sqlx驱动程序中的Go代码可能会这样做,但这是可选的,是为了在传递参数时更容易编写Go接口。

除非您想重用准备好的查询并使用不同的参数多次执行它,否则不需要手动使用Prepare()函数。

英文:

MySQL and PostgreSQL definitely don't prepare every query. You can execute queries directly, without doing a prepare & execute sequence.

The Go code in the sqlx driver probably does this, but it's elective, done to make it simpler to code the Go interface when you pass args.

You don't need to use the Prepare() func manually, unless you want to reuse the prepared query, executing it multiple times with different args.

huangapple
  • 本文由 发表于 2021年9月14日 05:52:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/69169488.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定