英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论